Skip to content

Instantly share code, notes, and snippets.

@magicien
Created May 24, 2020 10:23
Show Gist options
  • Save magicien/a892304c22574d59923dccc95571b990 to your computer and use it in GitHub Desktop.
Save magicien/a892304c22574d59923dccc95571b990 to your computer and use it in GitHub Desktop.
Epic Online Services Log in sample for Swift
import QuartzCore
import EOSSDK
var platformHandle: EOS_HPlatform? = nil
var displayLink: CVDisplayLink? = nil
func logFunc(message: Optional<UnsafePointer<_tagEOS_LogMessage>>) {
message?.withMemoryRebound(to: EOS_LogMessage.self, capacity: 100) {
let str = String(cString: $0.pointee.Message, encoding: .ascii)
Swift.print(str ?? "")
}
}
func loginFunc(data: Optional<UnsafePointer<_tagEOS_Auth_LoginCallbackInfo>>) {
guard let info = data?.pointee else {
Swift.print("Error: Login data is nil")
return
}
if info.ResultCode != EOS_Success {
Swift.print("Error: Login failed")
return
}
Swift.print("Login succeeded")
}
func authExpirationFunc(data: Optional<UnsafePointer<_tagEOS_Connect_AuthExpirationCallbackInfo>>) {
guard let info = data?.pointee else {
Swift.print("Error: Connect data is nil")
return
}
// Do something
}
func renderCallback(
displayLink: CVDisplayLink,
inNow: UnsafePointer<CVTimeStamp>,
inOutputTime: UnsafePointer<CVTimeStamp>,
flagsIn: UInt64,
flagsOut: UnsafeMutablePointer<UInt64>,
displayLinkContext: Optional<UnsafeMutableRawPointer>
) -> Int32 {
if let handle = platformHandle {
EOS_Platform_Tick(handle);
}
return 0
}
/*
* 1. Initiallize App
*/
var initOptions = EOS_InitializeOptions()
initOptions.ApiVersion = EOS_INITIALIZE_API_LATEST
initOptions.AllocateMemoryFunction = nil
initOptions.ReallocateMemoryFunction = nil
initOptions.ReleaseMemoryFunction = nil
let productName = "Test".cString(using: .ascii)
productName?.withUnsafeBufferPointer {
initOptions.ProductName = $0.baseAddress
}
let productVersion = "1.0".cString(using: .ascii)
productVersion?.withUnsafeBufferPointer {
initOptions.ProductVersion = $0.baseAddress
}
initOptions.Reserved = nil
initOptions.SystemInitializeOptions = nil
let result = EOS_Initialize(&initOptions)
guard result == EOS_Success {
Swift.print("Error: Init Failed")
return
}
/*
* 2. Set Logging callback
*/
EOS_Logging_SetCallback(logFunc)
EOS_Logging_SetLogLevel(EOS_LC_ALL_CATEGORIES, EOS_LOG_Verbose)
/*
* 3. Initiallize Platform
*/
var options = EOS_Platform_Options()
options.ApiVersion = EOS_PLATFORM_OPTIONS_API_LATEST
options.bIsServer = EOS_FALSE
options.EncryptionKey = nil
options.OverrideCountryCode = nil
options.OverrideLocaleCode = nil
let key = String(repeating: "1", count: 64).cString(using: .ascii)
key?.withUnsafeBufferPointer {
options.EncryptionKey = $0.baseAddress
}
options.Flags = 0
let cachePath = "/private/var/tmp".cString(using: .ascii)
cachePath?.withUnsafeBufferPointer {
options.CacheDirectory = $0.baseAddress
}
let productId = "### Product ID ###".cString(using: .ascii)
productId?.withUnsafeBufferPointer {
options.ProductId = $0.baseAddress
}
let sandboxId = "### Sandbox ID ###".cString(using: .ascii)
sandboxId?.withUnsafeBufferPointer {
options.SandboxId = $0.baseAddress
}
let deploymentId = "### Deployment ID ###".cString(using: .ascii)
deploymentId?.withUnsafeBufferPointer {
options.DeploymentId = $0.baseAddress
}
var credentials = EOS_Platform_ClientCredentials()
let clientId = "### Client ID ###".cString(using: .ascii)
clientId?.withUnsafeBufferPointer {
credentials.ClientId = $0.baseAddress
}
let clientSecret = "### Client Secret ###".cString(using: .ascii)
clientSecret?.withUnsafeBufferPointer {
credentials.ClientSecret = $0.baseAddress
}
options.ClientCredentials = credentials
options.Reserved = nil
guard let handle = EOS_Platform_Create(&options) else {
Swift.print("Error: EOS_Platform_Create failed")
return
}
platformHandle = handle;
/*
* 4. Set tick callback
*/
let displayID = CGMainDisplayID()
let error = CVDisplayLinkCreateWithCGDisplay(displayID, &displayLink)
if (error != kCVReturnSuccess) {
Swift.print("Error: CVDisplayLinkCreateWithCGDisplay error")
return
}
guard let link = displayLink else {
Swift.print("Error: Failed to get displayLink")
return
}
CVDisplayLinkSetOutputCallback(link, renderCallback, nil)
CVDisplayLinkStart(link)
/*
* 5. Log In via Account Portal
*/
guard let authHandle = EOS_Platform_GetAuthInterface(handle) else {
Swift.print("Error: EOS_Platform_GetAuthInterface failed")
return
}
guard let connectHandle = EOS_Platform_GetConnectInterface(handle) else {
Swift.print("Error: EOS_Platform_GetConnectInterface failed")
return
}
var loginOptions = EOS_Auth_LoginOptions()
loginOptions.ApiVersion = EOS_AUTH_LOGIN_API_LATEST
loginOptions.ScopeFlags = EOS_EAuthScopeFlags(EOS_AS_BasicProfile.rawValue | EOS_AS_FriendsList.rawValue | EOS_AS_Presence.rawValue)
var authCredentials = EOS_Auth_Credentials()
authCredentials.ApiVersion = EOS_AUTH_CREDENTIALS_API_LATEST
authCredentials.Type = EOS_LCT_AccountPortal
authCredentials.Id = nil
authCredentials.Token = nil
loginOptions.Credentials = UnsafePointer<EOS_Auth_Credentials>(&authCredentials)
EOS_Auth_Login(authHandle, &loginOptions, nil, loginFunc)
var connectOptions = EOS_Connect_AddNotifyAuthExpirationOptions()
connectOptions.ApiVersion = EOS_CONNECT_ADDNOTIFYAUTHEXPIRATION_API_LATEST
EOS_Connect_AddNotifyAuthExpiration(connectHandle, &connectOptions, nil, authExpirationFunc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment