#import #import #import #import #import "TYConfig.h" #import "UnityFramework/UnityFramework-Swift.h" #define TOKEN_ERROR @"-8" extern "C" { // Store last login type for SNS re-login static NSInteger g_snsLoginType = -1; NSString* DicTojsonString(NSDictionary *dict) { try { NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingFragmentsAllowed error:&error]; NSString *jsonString; if (!jsonData) { NSLog(@"%@",error); } else { jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding]; } NSMutableString *mutStr = [NSMutableString stringWithString:jsonString]; return mutStr; } catch (NSException *exception) { return @""; } } void ProcessLoginResult(XYResp *resp) { NSMutableDictionary *resultDict = [@{} mutableCopy]; [resultDict setValue:[NSString stringWithFormat:@"%ld", (long)resp.errCode] forKey:@"code"]; [resultDict setValue:resp.errStr ? resp.errStr : @"" forKey:@"errStr"]; if(resp.errCode == XYSuccess){ NSDictionary *respObjDict = (NSDictionary*)resp.respObj; NSDictionary *resultDictData = (NSDictionary*)[respObjDict objectForKey:@"result"]; NSMutableDictionary *loginData = [resultDictData mutableCopy]; [resultDict setValue:loginData forKey:@"loginData"]; } NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","LoginResult", param); g_snsLoginType = -1; } //更新登录支付域名 void UnityResetServerUrl(const char* url) { NSString* serverUrl = [[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding]; NSLog(@"TYSdkInterface UnityResetServerUrl %@", serverUrl); if(serverUrl != nil && ![serverUrl isEqualToString:@""]) { [XYApi resetServerUrlString:serverUrl]; } } //登录 void UnityLoginByTokenFun(const char* ctoken) { NSLog(@"TYSdkInterface UnityLoginByTokenFun %s", ctoken); NSString* token = [[NSString alloc] initWithCString:ctoken encoding:NSUTF8StringEncoding]; if(token != nil && ![token isEqualToString:@""]) { [XYApi XYLoginByToken:token completionHandler:^(XYResp *resp) { NSLog(@"TYSdkInterface UnityLoginByTokenFun %ld, %@, %@", (long)resp.errCode, resp.respObj, resp.errStr); ProcessLoginResult(resp); }]; }else{ NSMutableDictionary *resultDict = [@{} mutableCopy]; [resultDict setValue:TOKEN_ERROR forKey:@"code"]; [resultDict setValue:@"" forKey:@"errStr"]; NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","LoginResult", param); } } void UnityLoginByGuest() { XYLoginReq *req = [XYLoginReq initWithLoginType:XYLoginWithGuest]; [XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) { NSLog(@"TYSdkInterface UnityLoginByGuest: %@", resp); ProcessLoginResult(resp); }]; } void UnityLoginByGoogle() { XYLoginReq *req = [XYLoginReq initWithLoginType:XYLoginWithGoogle]; [XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) { ProcessLoginResult(resp); }]; } void UnityLoginByFacebook() { XYLoginReq *req = [XYLoginReq initWithLoginType:XYLoginWithFB]; [XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) { NSLog(@"TYSdkInterface UnityLoginByFB: %@", resp); ProcessLoginResult(resp); }]; } void UnityLoginByApple() { XYLoginReq *req = [XYLoginReq initWithLoginType:XYLoginWithApple]; [XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) { NSLog(@"TYSdkInterface UnityLoginByApple: %@", resp); ProcessLoginResult(resp); }]; } void UnityGetIdentityFun(const char* type) { } char* LoginTypeToStr(XYLoginType loginType) { switch (loginType) { case XYLoginWithFB: return AllocCString(@"hwFacebook"); case XYLoginWithApple: return AllocCString(@"Apple"); case XYLoginWithGoogle: return AllocCString(@"hwGoogle"); default: return nil; } } NSInteger BindTypeToLoginType(XYBindType bType) { switch (bType) { case XYBindApple: return XYLoginWithApple; case XYBindFB: return XYLoginWithFB; case XYBindGoodle: return XYLoginWithGoogle; default: return -1; } } NSInteger LoginTypeToBindType(NSInteger loginType) { switch (loginType) { case XYLoginWithApple: return XYBindApple; case XYLoginWithFB: return XYBindFB; case XYLoginWithGoogle: return XYBindGoodle; default: return -1; } } const char* UnityLoginBySnsInfo() { NSLog(@"TYSdkInterface UnityLoginBySnsInfo"); if(g_snsLoginType == -1) { NSLog(@"TYSdkInterface UnityLoginBySnsInfo - No stored login type"); UnitySendMessage("TYSdkFacade","LoginResult", "{\"code\":1}"); // Return empty string return ""; } NSLog(@"TYSdkInterface UnityLoginBySnsInfo - Re-login with type: %s", LoginTypeToStr((XYLoginType)g_snsLoginType)); // Re-login using the stored type XYLoginReq *req = [XYLoginReq initWithLoginType:(XYLoginType)g_snsLoginType]; [XYApi XYLoginWith:req completionHandler:^(XYResp * _Nonnull resp) { NSLog(@"TYSdkInterface UnityLoginBySnsInfo result: %@", resp); ProcessLoginResult(resp); }]; // Return the login type as C string return LoginTypeToStr((XYLoginType)g_snsLoginType); } void UnityLoginOut() { NSLog(@"TYSdkInterface UnityLoginOut"); [XYApi XYLogoutWithCompletion:^(XYResp * _Nonnull resp) { if(resp.errCode == XYSuccess) { NSLog(@"TYSdkInterface UnityLoginOut Success"); NSLog(@"%@", resp.respObj); } else { NSLog(@"TYSdkInterface UnityLoginOut Faild"); NSLog(@"Error Code:%ld msg:%@", resp.errCode, resp.errStr); } }]; } void UnityLogoutGoogle() { NSLog(@"TYSdkInterface UnityLogOut Google"); [XYApi XYLogoutChannelWithType:XYLoginWithGoogle]; } void UnityLogoutFacebook() { NSLog(@"TYSdkInterface UnityLogOut Facebook"); [XYApi XYLogoutChannelWithType:XYLoginWithFB]; } void UnityLogoutGuest() { NSLog(@"TYSdkInterface UnityLogOutGuset"); [XYApi XYLogoutChannelWithType:XYLoginWithGuest]; } void UnityLogoutApple() { NSLog(@"TYSdkInterface UnityLogoutApple"); [XYApi XYLogoutChannelWithType:XYLoginWithApple]; } //支付 void UnityKnow(const char* userId,const char* productId,const char* productPrice,const char* productName,const char* productCount,const char* orderid,const char* appinfo) { NSLog(@"TYSdkInterface UnityKnow "); NSString* pUserId = [[NSString alloc] initWithCString:userId encoding:NSUTF8StringEncoding]; NSString* porderid = [[NSString alloc] initWithCString:orderid encoding:NSUTF8StringEncoding]; NSString* pProductId = [[NSString alloc] initWithCString:productId encoding:NSUTF8StringEncoding]; NSString* pProductPrice = [[NSString alloc] initWithCString:productPrice encoding:NSUTF8StringEncoding]; NSString* pProductName = [[NSString alloc] initWithCString:productName encoding:NSUTF8StringEncoding]; NSString* pProductCount = [[NSString alloc] initWithCString:productCount encoding:NSUTF8StringEncoding]; NSString* pAppInfo = [[NSString alloc] initWithCString:appinfo encoding:NSUTF8StringEncoding]; XYProduct* product = [XYProduct createShoppingDataWith:XYOnlyConsume]; if (porderid != NULL && porderid.length > 0) { product.productOrderId = porderid; } [product XYProductWithAppInfo:pAppInfo productId:pProductId productName:pProductName productPrice:[pProductPrice floatValue] gameId:-1 productCount:[pProductCount integerValue] userId:[pUserId integerValue]]; [XYApi XYCharge:product completionHandler:^(XYResp * _Nonnull resp) { NSMutableDictionary *resultDict = [@{} mutableCopy]; [resultDict setValue:[NSString stringWithFormat:@"%ld", (long)resp.errCode] forKey:@"code"]; [resultDict setValue:resp.errStr ? resp.errStr : @"" forKey:@"errStr"]; NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","PayResult", param); }]; } void GetSKUList() { NSLog(@"TYSdkInterface GetSKUList "); [XYApi XYGetLocalProductPriceWithCompletion:^(XYResp * _Nonnull resp) { NSMutableDictionary *resultDict = [@{} mutableCopy]; [resultDict setValue:[NSString stringWithFormat:@"%ld", (long)resp.errCode] forKey:@"code"]; if(resp.errCode == XYSuccess){ NSLog(@"TYSdkInterface GetSKUList Success"); NSLog(@"SKUList %@", resp.respObj); [resultDict setValue:resp.respObj forKey:@"respObj"]; } else { NSLog(@"TYSdkInterface GetSKUList Faild"); NSLog(@"SKUList %ld %@", resp.errCode, resp.errStr); } NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","SKUListResult", param); }]; } //打点 void SetGaUserInfo(const char* userId) { NSString *userid = [[NSString alloc] initWithCString:userId encoding:NSUTF8StringEncoding]; [[GASDK getGAlog:SDK_PROJECTID] setUserId:userid]; } void SetGaCommonInfo(const char* SetGaCommonInfo) { NSString * jsonString = [NSString stringWithFormat:@"%s", SetGaCommonInfo]; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError* err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(!err){ [[GASDK getGAlog:SDK_PROJECTID] initWithGameId:SDK_GAMEID clientId:SDK_CLIENTID withCommonParams:dic serverUrl:GA_INTERNATIONAL]; } } void GAReportParams(int type, const char* eventstr, const char* paramstr) { NSString * jsonString = [NSString stringWithFormat:@"%s", paramstr]; NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError* err; NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err]; if(!err){ NSString* nsEvent = [NSString stringWithFormat: @"%s", eventstr]; GASDK *gaSDK = [GASDK getGAlog:SDK_PROJECTID]; [gaSDK track:dic type:GAEventType(type) event:nsEvent]; } } void CleanLinkTmpSnsInfo() { g_snsLoginType = -1; } // Helper function to handle account linking typedef void(^LogoutCallback)(void); void LinkAccountWithPlatform(NSString *platformName, XYBindType bindType, const int userId, LogoutCallback logoutCallback) { NSLog(@"TYSdkInterface Link%@ userId:%d", platformName, userId); XYBindReq *bindReq = [XYBindReq initBindWithPlatform:bindType UserId:(NSUInteger)userId]; [XYApi XYBindByBindReq:bindReq isUnbindGuest:@"false" completionHandler:^(XYResp * _Nonnull resp) { NSMutableDictionary *resultDict = [@{} mutableCopy]; if(resp.errCode == XYSuccess) { NSLog(@"TYSdkInterface Link%@ Success", platformName); NSDictionary *respObjDict = (NSDictionary*)resp.respObj; NSDictionary *resultDictData = (NSDictionary*)[respObjDict objectForKey:@"result"]; NSMutableDictionary *loginData = [resultDictData mutableCopy]; [resultDict setValue:[NSString stringWithFormat:@"%ld", (long)resp.errCode] forKey:@"code"]; [resultDict setValue:[NSString stringWithFormat:@"%d", userId] forKey:@"userId"]; [resultDict setValue:loginData forKey:@"loginData"]; NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","LinkAccoutResult", param); } else if(resp.errCode == XYErrCodeHadBind) { [XYApi CheckUserExist:bindType completion:^(XYResp * _Nonnull snscheckResp) { if(snscheckResp.errCode != 0) { [resultDict setValue:@"1" forKey:@"code"]; [resultDict setValue:@"0" forKey:@"userId"]; NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","LinkAccoutResult", param); } else { g_snsLoginType = BindTypeToLoginType(bindType); NSDictionary *respObjDict = (NSDictionary*)snscheckResp.respObj; [resultDict setValue:@"-1" forKey:@"code"]; [resultDict setValue:[respObjDict objectForKey:@"bindInfo"] forKey:@"bindInfo"]; [resultDict setValue:[respObjDict objectForKey:@"userId"] forKey:@"userId"]; NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","LinkAccoutResult", param); } }]; } else { NSLog(@"TYSdkInterface Link%@ Failed errCode:%ld", platformName, (long)resp.errCode); if (logoutCallback) { logoutCallback(); } [resultDict setValue:@"1" forKey:@"code"]; [resultDict setValue:@"0" forKey:@"userId"]; NSString *result = DicTojsonString(resultDict); const char* param = AllocCString(result); UnitySendMessage("TYSdkFacade","LinkAccoutResult", param); } }]; } void LinkGoogle(const int userId) { LinkAccountWithPlatform(@"Google", XYBindGoodle, userId, ^{ UnityLogoutGoogle(); }); } void IsLinkedGoogle() { [XYApi CheckUserExist:XYBindGoodle completion:^(XYResp * _Nonnull resp) { if (resp.errCode == XYSNSExist) { UnitySendMessage("TYSdkFacade","CheckLinkResult", "1"); }else if (resp.errCode == XYSNSNotExist){ UnitySendMessage("TYSdkFacade","CheckLinkResult", "0"); } else { UnitySendMessage("TYSdkFacade","CheckLinkResult", "-1"); } }]; } void LinkFacebook(const int userId) { LinkAccountWithPlatform(@"Facebook", XYBindFB, userId, ^{ UnityLogoutFacebook(); }); } void IsLinkedFacebook() { [XYApi CheckUserExist:XYBindFB completion:^(XYResp * _Nonnull resp) { if (resp.errCode == XYSNSExist) { UnitySendMessage("TYSdkFacade","CheckLinkResult", "1"); }else if (resp.errCode == XYSNSNotExist){ UnitySendMessage("TYSdkFacade","CheckLinkResult", "0"); } else { UnitySendMessage("TYSdkFacade","CheckLinkResult", "-1"); } }]; } void LinkApple(const int userId) { LinkAccountWithPlatform(@"Apple", XYBindApple, userId, ^{ UnityLogoutApple(); }); } void IsLinkedApple() { [XYApi CheckUserExist:XYBindApple completion:^(XYResp * _Nonnull resp) { if (resp.errCode == XYSNSExist) { UnitySendMessage("TYSdkFacade","CheckLinkResult", "1"); }else if (resp.errCode == XYSNSNotExist){ UnitySendMessage("TYSdkFacade","CheckLinkResult", "0"); } else { UnitySendMessage("TYSdkFacade","CheckLinkResult", "-1"); } }]; } void ChangeLinkAccount(const int accoutType, const int oldUserId, const int newUserId) { NSLog(@"TYSdkInterface ChangeLinkAccount accoutType:%d oldUserId:%d newUserId:%d", accoutType, oldUserId, newUserId); if (g_snsLoginType == -1) { UnitySendMessage("TYSdkFacade", "ChangeLinkAccountResult", "{\"code\":1, \"userId\": 0, \"msg\": \"g_snsLoginType is invalid\"}"); return; } NSInteger bindTypeValue = LoginTypeToBindType(g_snsLoginType); if (bindTypeValue == -1) { UnitySendMessage("TYSdkFacade", "ChangeLinkAccountResult", "{\"code\":1, \"userId\": 0, \"msg\": \"unsupported account type\"}"); return; } XYBindType bindType = (XYBindType)bindTypeValue; XYBindReq *unbindReq = [XYBindReq initUnBindWithPlatform:bindType UserId:(NSUInteger)oldUserId]; [XYApi XYUnBindByBindReq:unbindReq completionHandler:^(XYResp * _Nonnull unbindResp) { NSLog(@"TYSdkInterface ChangeLinkAccount unbind code:%ld msg:%@", (long)unbindResp.errCode, unbindResp.errStr); if (unbindResp.errCode == XYSuccess) { XYBindReq *bindReq = [XYBindReq initBindWithPlatform:bindType UserId:(NSUInteger)newUserId]; [XYApi XYBindByBindReq:bindReq isUnbindGuest:@"false" completionHandler:^(XYResp * _Nonnull bindResp) { NSLog(@"TYSdkInterface ChangeLinkAccount bind code:%ld msg:%@", (long)bindResp.errCode, bindResp.errStr); if (bindResp.errCode == XYSuccess) { NSString *msg = [NSString stringWithFormat:@"{\"code\":0, \"userId\": %d, \"msg\": \"success\"}", newUserId]; UnitySendMessage("TYSdkFacade", "ChangeLinkAccountResult", AllocCString(msg)); } else { NSString *errStr = bindResp.errStr ? bindResp.errStr : @"bind failed"; NSString *msg = [NSString stringWithFormat:@"{\"code\":-1, \"userId\": 0, \"msg\": \"%@\"}", errStr]; UnitySendMessage("TYSdkFacade", "ChangeLinkAccountResult", AllocCString(msg)); } }]; } else { NSString *errStr = unbindResp.errStr ? unbindResp.errStr : @"unbind failed"; NSString *msg = [NSString stringWithFormat:@"{\"code\":1, \"userId\": 0, \"msg\": \"%@\"}", errStr]; UnitySendMessage("TYSdkFacade", "ChangeLinkAccountResult", AllocCString(msg)); } }]; } void FBShareLink(const char* title, const char* content, const char* url) { NSLog(@"TYSdkInterface FBShareLink"); NSString *t = [[NSString alloc] initWithCString: title encoding:NSUTF8StringEncoding]; NSString *c = [[NSString alloc] initWithCString: content encoding:NSUTF8StringEncoding]; NSString *l = [[NSString alloc] initWithCString: url encoding:NSUTF8StringEncoding]; FBShareHelper *helper = [[FBShareHelper alloc] init]; [helper FBShareWithTitle:t content:c link:l]; } void MessengerShareLink(const char* title, const char* content, const char* url) { NSLog(@"TYSdkInterface FBShareLink"); NSString *t = [[NSString alloc] initWithCString: title encoding:NSUTF8StringEncoding]; NSString *c = [[NSString alloc] initWithCString: content encoding:NSUTF8StringEncoding]; NSString *l = [[NSString alloc] initWithCString: url encoding:NSUTF8StringEncoding]; FBShareHelper *helper = [[FBShareHelper alloc] init]; [helper MFBShareWithTitle:t content:c link:l]; } }