FateforgeGameCenter.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #import <GameKit/GameKit.h>
  2. #import <UIKit/UIKit.h>
  3. #include <string.h>
  4. #import "Unity/UnityInterface.h"
  5. #if DEBUG
  6. #define FFGameCenterLog(...) NSLog(@"[Fateforge GC] " __VA_ARGS__)
  7. #else
  8. #define FFGameCenterLog(...) ((void)0)
  9. #endif
  10. static UIViewController *Presenter() {
  11. UIViewController *view = UnityGetGLViewController();
  12. while (view.presentedViewController) view = view.presentedViewController;
  13. return view;
  14. }
  15. @interface FFGameCenterDelegate : NSObject<GKGameCenterControllerDelegate>
  16. @end
  17. @implementation FFGameCenterDelegate
  18. - (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)controller {
  19. [controller dismissViewControllerAnimated:YES completion:nil];
  20. }
  21. @end
  22. extern "C" void FFAuthenticate() {
  23. static dispatch_once_t once;
  24. dispatch_once(&once, ^{
  25. GKLocalPlayer.localPlayer.authenticateHandler = ^(UIViewController *controller, NSError *error) {
  26. FFGameCenterLog(@"auth bundle=%@ authenticated=%d needsUI=%d error=%@/%ld %@",
  27. NSBundle.mainBundle.bundleIdentifier, GKLocalPlayer.localPlayer.isAuthenticated, controller != nil,
  28. error.domain, (long)error.code, error.localizedDescription);
  29. if (controller) dispatch_async(dispatch_get_main_queue(), ^{
  30. [Presenter() presentViewController:controller animated:YES completion:nil];
  31. });
  32. };
  33. });
  34. }
  35. extern "C" char *FFPlayer() {
  36. GKLocalPlayer *player = GKLocalPlayer.localPlayer;
  37. return player.isAuthenticated ? strdup(player.gamePlayerID.UTF8String) : nullptr;
  38. }
  39. extern "C" int FFShowRanking(const char *board) {
  40. if (!GKLocalPlayer.localPlayer.isAuthenticated || !board || !*board) return 0;
  41. static FFGameCenterDelegate *delegate = [FFGameCenterDelegate new];
  42. GKGameCenterViewController *controller = [[GKGameCenterViewController alloc]
  43. initWithLeaderboardID:[NSString stringWithUTF8String:board]
  44. playerScope:GKLeaderboardPlayerScopeGlobal timeScope:GKLeaderboardTimeScopeAllTime];
  45. controller.gameCenterDelegate = delegate;
  46. [Presenter() presentViewController:controller animated:YES completion:nil];
  47. return 1;
  48. }
  49. // Cache the current player's server record so a fresh install can display it on Home.
  50. static NSString *bestOwner;
  51. static int64_t serverBest;
  52. static NSTimeInterval nextFetch;
  53. extern "C" int64_t FFRemoteBest(const char *board,const char *playerID) {
  54. if (!board || !*board || !playerID || !*playerID) return 0;
  55. NSString *owner = [NSString stringWithUTF8String:playerID];
  56. if (![GKLocalPlayer.localPlayer.gamePlayerID isEqual:owner] || !GKLocalPlayer.localPlayer.isAuthenticated) return 0;
  57. if (![bestOwner isEqual:owner]) {
  58. bestOwner = owner;
  59. serverBest = 0;
  60. nextFetch = 0;
  61. }
  62. NSTimeInterval now = NSDate.timeIntervalSinceReferenceDate;
  63. if (now < nextFetch) return serverBest;
  64. nextFetch = now + 60;
  65. NSString *boardID = [NSString stringWithUTF8String:board];
  66. [GKLeaderboard loadLeaderboardsWithIDs:@[boardID]
  67. completionHandler:^(NSArray<GKLeaderboard *> *boards,NSError *error) {
  68. FFGameCenterLog(@"lookup board=%@ count=%lu error=%@/%ld %@", boardID,
  69. (unsigned long)boards.count, error.domain, (long)error.code, error.localizedDescription);
  70. if (error || boards.count == 0) return;
  71. [boards.firstObject loadEntriesForPlayerScope:GKLeaderboardPlayerScopeGlobal
  72. timeScope:GKLeaderboardTimeScopeAllTime range:NSMakeRange(1,1)
  73. completionHandler:^(GKLeaderboardEntry *entry,NSArray<GKLeaderboardEntry *> *entries,NSInteger count,NSError *failure) {
  74. FFGameCenterLog(@"entries board=%@ localRank=%ld localScore=%ld error=%@/%ld %@", boardID,
  75. (long)entry.rank, (long)entry.score, failure.domain, (long)failure.code, failure.localizedDescription);
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. if (!failure && [bestOwner isEqual:owner] && [entry.player.gamePlayerID isEqual:owner])
  78. serverBest = MAX(serverBest,entry.score);
  79. });
  80. }];
  81. }];
  82. return serverBest;
  83. }
  84. // Unity polls this on its main thread. Deliver completions on the same queue.
  85. static int generation, submitStatus;
  86. extern "C" int FFSubmit(const char *board, const char *playerID, int64_t score) {
  87. GKLocalPlayer *player = GKLocalPlayer.localPlayer;
  88. if (!player.isAuthenticated || ![player.gamePlayerID isEqualToString:[NSString stringWithUTF8String:playerID]]) return 0;
  89. int token = ++generation;
  90. submitStatus = 0;
  91. NSString *boardID = [NSString stringWithUTF8String:board];
  92. [GKLeaderboard submitScore:(NSInteger)score context:0 player:player
  93. leaderboardIDs:@[boardID] completionHandler:^(NSError *error) {
  94. FFGameCenterLog(@"submit board=%@ score=%lld success=%d error=%@/%ld %@", boardID,
  95. (long long)score, error == nil, error.domain, (long)error.code, error.localizedDescription);
  96. dispatch_async(dispatch_get_main_queue(), ^{
  97. if (token == generation) submitStatus = error ? -1 : 1;
  98. });
  99. }];
  100. return token;
  101. }
  102. extern "C" int FFSubmissionStatus(int token) {
  103. return token == generation ? submitStatus : -1;
  104. }