Skip to content

Commit

Permalink
Fixing package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvinHV committed Apr 21, 2024
1 parent 9a1598a commit 65fd98d
Show file tree
Hide file tree
Showing 46 changed files with 24,261 additions and 53 deletions.
12 changes: 10 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/build
/target
/.gradle
/src/debug
/src/release
/**/rust/protobuf/*
Expand All @@ -10,4 +9,13 @@
/bridge/stremio/
/bridge/wrapper.hpp
.DS_Store
/Cargo.lock
/Cargo.lock
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
/Package.resolved
10 changes: 4 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,14 @@ iossim:
target/x86_64-apple-ios/release/$(LIBRARY_NAME).a

framework:
@mkdir -p build/
@$(RM) -rf build/$(FRAMEWORK_NAME).xcframework
@mkdir -p .build/
@$(RM) -rf .build/$(FRAMEWORK_NAME).xcframework
@xcodebuild -create-xcframework \
-library target/universal/$(LIBRARY_NAME)-ios.a \
-library target/universal/$(LIBRARY_NAME)-ios-sim.a \
-library target/universal/$(LIBRARY_NAME)-macabi.a \
-output build/$(FRAMEWORK_NAME).xcframework
-output .build/$(FRAMEWORK_NAME).xcframework

package: framework
@$(RM) -rf build/StremioCore
@cp -rf bridge/StremioCore build/
@$(RM) -rf Sources/StremioCore/stremio
@./buildBridge.command
@mv build/$(FRAMEWORK_NAME).xcframework build/StremioCore/Frameworks/$(FRAMEWORK_NAME).xcframework
3 changes: 2 additions & 1 deletion bridge/StremioCore/Package.swift → Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let package = Package(
"Wrapper",
.product(name: "SwiftProtobuf", package: "swift-protobuf")
]),
.binaryTarget(name: "XCFramework", path: "Frameworks/StremioCore.xcframework")
//.binaryTarget(name: "XCFramework", path: ".build/StremioCore.xcframework")
.binaryTarget(name: "XCFramework", url: "https://github.com/Stremio/stremio-core-swift/releases/download/1.2.4/StremioCore.zip", checksum: "6a51b06b8734ddb9d7121813cddb8b5c8f1ee7929cfc8aa16f9ecef4f785a795")
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,59 @@ import SwiftProtobuf
import Wrapper

public class Core {
//MARK: - callback

private static var fieldListener : [Stremio_Core_Runtime_Field : (Any) -> Void] = [:]
private static var eventListener : [Int : (Any) -> Void] = [:]

///Make sure to remove listener before function gets deallocated to avoid undefined behaviour
public static func addEventListener(type: Stremio_Core_Runtime_Field, _ function: @escaping (Any) -> Void) {
Core.fieldListener[type] = function
}

///Make sure to remove listener before function gets deallocated to avoid undefined behaviour
public static func addEventListener(type: Int, _ function: @escaping (Any) -> Void) {
Core.eventListener[type] = function
}

public static func removeEventListener(type: Stremio_Core_Runtime_Field) {
Core.fieldListener.removeValue(forKey: type)
print(fieldListener)
}

public static func removeEventListener(type: Int) {
Core.eventListener.removeValue(forKey: type)
}

@objc private static func onRuntimeEvent(_ eventProtobuf: ByteArray){
do {
let swiftData = convertToData(eventProtobuf, shouldFree: false)!;
let event = try Stremio_Core_Runtime_RuntimeEvent(serializedData: swiftData)
var function : ((Any) -> Void)?
var argument : Any?
if case .coreEvent(_:) = event.event{
function = Core.eventListener.first(where: {event.coreEvent.getMessageTag == $0.key})?.value
argument = event.coreEvent
}
else {
for field in event.newState.fields{
print(event)
function = Core.fieldListener[field]
argument = field
}
}
if let function = function, let argument = argument{
DispatchQueue.main.sync {
function(argument)
}
}
}
catch{
print("Swift Error onRuntimeEvent: \(error)")
}
}

//MARK: - rust calls
public static func initialize() -> Stremio_Core_Runtime_EnvError? {
initialize_rust()
do {
Expand Down Expand Up @@ -51,6 +104,13 @@ public class Core {
}
}

public static func getVersion() -> String? {
if let swiftData = convertToData(getVersionNative(), shouldFree: false){
return String(data: swiftData, encoding: .utf8)
}
return nil
}

public static func decodeStreamData(streamData: String) -> Stremio_Core_Types_Stream? {
do {
if let swiftData = convertToData(decodeStreamDataNative(streamData))
Expand All @@ -63,24 +123,6 @@ public class Core {
return nil
}

public static func getVersion() -> String? {
if let swiftData = convertToData(getVersionNative(), shouldFree: false){
return String(data: swiftData, encoding: .utf8)
}
return nil
}

@objc private static func onRuntimeEvent(_ eventProtobuf: ByteArray){
do {
let swiftData = convertToData(eventProtobuf, shouldFree: false)!;
let event = try Stremio_Core_Runtime_Event(serializedData: swiftData)
print(event)
}
catch{
print("Swift Error onRuntimeEvent: \(error)")
}
}

///Converts Swift Data to C byte array but needs to handle deallocation otherwise memory will leak.
private static func convertToByteArray(_ data: Data) -> ByteArray {
let length = data.count
Expand All @@ -105,3 +147,13 @@ public class Core {
return swiftData
}
}
//TODO: Find a way to get tag properly
extension SwiftProtobuf.Message {
var getMessageTag: Int {
let def = try! SwiftProtobuf.Google_Protobuf_MessageOptions(serializedData: self.serializedData())
var messageText = def.textFormatString().components(separatedBy: "\n").first
messageText = messageText?.replacingOccurrences(of: " {", with: "")
return Int(messageText!) ?? 0
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import Foundation
import SwiftProtobuf

public class StremioApi {
public class CallbackType {
public static var error : Int = {
var type = Stremio_Core_Runtime_Event()
type.error.error = ""
type.error.source = Stremio_Core_Runtime_Event()
return type.getMessageTag
}()
}

public static func SetLoadRange(field: Stremio_Core_Runtime_Field?, start: UInt32, end: UInt32) {
var action = Stremio_Core_Runtime_Action()
Expand Down Expand Up @@ -123,19 +131,11 @@ public class StremioApi {
Core.dispatch(action: action, field: .metaDetails)
}

public static func MetaItemGet(interval: TimeInterval) -> Stremio_Core_Models_MetaDetails?{
var result : Stremio_Core_Models_MetaDetails
repeat {
Thread.sleep(forTimeInterval: interval)
guard let myMessage: Stremio_Core_Models_MetaDetails = Core.getState(.metaDetails) else {return nil}
result = myMessage
if case .loading = result.metaItem.content {
continue
}
break
public static func MetaItemGet() -> Stremio_Core_Models_MetaDetails?{
if let myMessage: Stremio_Core_Models_MetaDetails = Core.getState(.metaDetails) {
return myMessage
}
while (true)
return result
return nil
}

public static func Load() {
Expand Down Expand Up @@ -173,12 +173,12 @@ public class StremioApi {
var action = Stremio_Core_Runtime_Action()
action.load.player.stream = stream
//If url contains info about meta then load it also
if urlPath.count >= 6{
if urlPath.indices.contains(5) {
let addonURL = urlPath[2]
let metaURL = urlPath[3]
let contentType = urlPath[4]
let contentID = urlPath[5]
let streamID = urlPath.count >= 7 ? urlPath[6] : contentID
let streamID = urlPath.indices.contains(6) ? urlPath[6] : contentID

action.load.player.streamRequest.base = addonURL
action.load.player.streamRequest.path.resource = "stream"
Expand Down Expand Up @@ -245,6 +245,27 @@ public class StremioApi {
Core.dispatch(action: action)
}

//MARK: - Adding, removing Library

public static func AddToLibrary(metaPreview: Stremio_Core_Types_MetaItemPreview) {
var action = Stremio_Core_Runtime_Action()
action.ctx.addToLibrary = metaPreview
Core.dispatch(action: action)
}

public static func RemoveFromLibrary(metaID: String) {
var action = Stremio_Core_Runtime_Action()
action.ctx.removeFromLibrary = metaID
Core.dispatch(action: action)
}

public static func RewindLibraryItem(metaID: String?){
guard let metaID = metaID else { return }
var action = Stremio_Core_Runtime_Action()
action.ctx.rewindLibraryItem = metaID
Core.dispatch(action: action)
}

//MARK: - For account related functions

public static func LoginWithToken(token: String) {
Expand Down
Loading

0 comments on commit 65fd98d

Please sign in to comment.