#import #import #include #import "Unity/UnityInterface.h" #if DEBUG #define FFGameCenterLog(...) NSLog(@"[Fateforge GC] " __VA_ARGS__) #else #define FFGameCenterLog(...) ((void)0) #endif static UIViewController *Presenter() { UIViewController *view = UnityGetGLViewController(); while (view.presentedViewController) view = view.presentedViewController; return view; } @interface FFGameCenterDelegate : NSObject @end @implementation FFGameCenterDelegate - (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)controller { [controller dismissViewControllerAnimated:YES completion:nil]; } @end extern "C" void FFAuthenticate() { static dispatch_once_t once; dispatch_once(&once, ^{ GKLocalPlayer.localPlayer.authenticateHandler = ^(UIViewController *controller, NSError *error) { FFGameCenterLog(@"auth bundle=%@ authenticated=%d needsUI=%d error=%@/%ld %@", NSBundle.mainBundle.bundleIdentifier, GKLocalPlayer.localPlayer.isAuthenticated, controller != nil, error.domain, (long)error.code, error.localizedDescription); if (controller) dispatch_async(dispatch_get_main_queue(), ^{ [Presenter() presentViewController:controller animated:YES completion:nil]; }); }; }); } extern "C" char *FFPlayer() { GKLocalPlayer *player = GKLocalPlayer.localPlayer; return player.isAuthenticated ? strdup(player.gamePlayerID.UTF8String) : nullptr; } extern "C" int FFShowRanking(const char *board) { if (!GKLocalPlayer.localPlayer.isAuthenticated || !board || !*board) return 0; static FFGameCenterDelegate *delegate = [FFGameCenterDelegate new]; GKGameCenterViewController *controller = [[GKGameCenterViewController alloc] initWithLeaderboardID:[NSString stringWithUTF8String:board] playerScope:GKLeaderboardPlayerScopeGlobal timeScope:GKLeaderboardTimeScopeAllTime]; controller.gameCenterDelegate = delegate; [Presenter() presentViewController:controller animated:YES completion:nil]; return 1; } // Cache the current player's server record so a fresh install can display it on Home. static NSString *bestOwner; static int64_t serverBest; static NSTimeInterval nextFetch; extern "C" int64_t FFRemoteBest(const char *board,const char *playerID) { if (!board || !*board || !playerID || !*playerID) return 0; NSString *owner = [NSString stringWithUTF8String:playerID]; if (![GKLocalPlayer.localPlayer.gamePlayerID isEqual:owner] || !GKLocalPlayer.localPlayer.isAuthenticated) return 0; if (![bestOwner isEqual:owner]) { bestOwner = owner; serverBest = 0; nextFetch = 0; } NSTimeInterval now = NSDate.timeIntervalSinceReferenceDate; if (now < nextFetch) return serverBest; nextFetch = now + 60; NSString *boardID = [NSString stringWithUTF8String:board]; [GKLeaderboard loadLeaderboardsWithIDs:@[boardID] completionHandler:^(NSArray *boards,NSError *error) { FFGameCenterLog(@"lookup board=%@ count=%lu error=%@/%ld %@", boardID, (unsigned long)boards.count, error.domain, (long)error.code, error.localizedDescription); if (error || boards.count == 0) return; [boards.firstObject loadEntriesForPlayerScope:GKLeaderboardPlayerScopeGlobal timeScope:GKLeaderboardTimeScopeAllTime range:NSMakeRange(1,1) completionHandler:^(GKLeaderboardEntry *entry,NSArray *entries,NSInteger count,NSError *failure) { FFGameCenterLog(@"entries board=%@ localRank=%ld localScore=%ld error=%@/%ld %@", boardID, (long)entry.rank, (long)entry.score, failure.domain, (long)failure.code, failure.localizedDescription); dispatch_async(dispatch_get_main_queue(), ^{ if (!failure && [bestOwner isEqual:owner] && [entry.player.gamePlayerID isEqual:owner]) serverBest = MAX(serverBest,entry.score); }); }]; }]; return serverBest; } // Unity polls this on its main thread. Deliver completions on the same queue. static int generation, submitStatus; extern "C" int FFSubmit(const char *board, const char *playerID, int64_t score) { GKLocalPlayer *player = GKLocalPlayer.localPlayer; if (!player.isAuthenticated || ![player.gamePlayerID isEqualToString:[NSString stringWithUTF8String:playerID]]) return 0; int token = ++generation; submitStatus = 0; NSString *boardID = [NSString stringWithUTF8String:board]; [GKLeaderboard submitScore:(NSInteger)score context:0 player:player leaderboardIDs:@[boardID] completionHandler:^(NSError *error) { FFGameCenterLog(@"submit board=%@ score=%lld success=%d error=%@/%ld %@", boardID, (long long)score, error == nil, error.domain, (long)error.code, error.localizedDescription); dispatch_async(dispatch_get_main_queue(), ^{ if (token == generation) submitStatus = error ? -1 : 1; }); }]; return token; } extern "C" int FFSubmissionStatus(int token) { return token == generation ? submitStatus : -1; }