<commit_before>/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *******************************************************************************/
 
#include <dmsdk/sdk.h>
#include <vector>
#include <map>
#include <string>

#include "gamecenter_private.h"
#include <Foundation/Foundation.h>
#include <GameKit/GameKit.h>

#if defined(DM_PLATFORM_IOS)
#include <UIKit/UIKit.h>
#endif

using namespace std;

#if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_OSX)
NSString *const PresentAuthenticationViewController = @"present_authentication_view_controller";

@protocol GameCenterManagerDelegate <GKGameCenterControllerDelegate>
@end

#if defined(DM_PLATFORM_IOS)
@interface GameKitManager : UIViewController <GameCenterManagerDelegate>
{
@private UIViewController *m_authenticationViewController;
#else
@interface GameKitManager : NSViewController <GameCenterManagerDelegate>
{
@private NSViewController *m_authenticationViewController;
#endif

@private id<GameCenterManagerDelegate, NSObject> m_delegate;
}
+ (instancetype)sharedGameKitManager;
@end


@implementation GameKitManager

+ (instancetype)sharedGameKitManager
{
    static GameKitManager *sharedGameKitManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedGameKitManager = [[GameKitManager alloc] init];
    });
    return sharedGameKitManager;
}

- (id)init
{
    self = [super init];
    if (self) {
    	m_delegate = self;
    }
    return self;
}

- (void)authenticateLocalPlayer:(CallbackFn) fn withCallbackInfo:(CallbackInfo*) cbk
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    
#if defined(DM_PLATFORM_IOS)
    localPlayer.authenticateHandler  =
    ^(UIViewController *viewController, NSError *error) {
#else        
    localPlayer.authenticateHandler  =
    ^(NSViewController *viewController, NSError *error) {
#endif  
		if (error != nil)
        {
            cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
        }
              
        if(viewController != nil) {
            [self setAuthenticationViewController:viewController];
        } else if([GKLocalPlayer localPlayer].isAuthenticated) {
        	fn(cbk);
        } else {
        	cbk->m_Error = new GKError(GKErrorUnknown, "Unknown");
        	fn(cbk);
        }
    };
}

#if defined(DM_PLATFORM_IOS)
- (void)setAuthenticationViewController:(UIViewController *)authenticationViewController
#else
- (void)setAuthenticationViewController:(NSViewController *)authenticationViewController
#endif 
	{   
    m_authenticationViewController = authenticationViewController;
    [[NSNotificationCenter defaultCenter]
     postNotificationName:PresentAuthenticationViewController
     object:self];
}


- (void) login:(CallbackFn) fn withCallbackInfo:(CallbackInfo*) cbk
{
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(showAuthenticationViewController)
     name:PresentAuthenticationViewController
     object:nil];
    
    [self authenticateLocalPlayer:fn withCallbackInfo:cbk];
}

- (void) reportScore:(NSString*)leaderboardId score:(int)score withCallback:(CallbackFn) fn withCallbackInfo:(CallbackInfo*) cbk
{
    GKScore* scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier:leaderboardId];
    scoreReporter.value = (int64_t)score;
    [GKScore reportScores:@[scoreReporter] withCompletionHandler:^(NSError *error) {
        if (error)
        {
            cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
        }
        fn(cbk);
    }];
}
    
- (void)submitAchievement:(NSString*)identifier withPercentComplete:(double)percentComplete withCallback:(CallbackFn)fn withCallbackInfo:(CallbackInfo*) cbk
{
    GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier:identifier]  autorelease];
    [achievement setPercentComplete:percentComplete];
    achievement.showsCompletionBanner = YES;
    [achievement reportAchievementWithCompletionHandler:^(NSError  *error) {
        if (error)
        {
            cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
        }
        fn(cbk);
    }];
}

- (void)loadAchievements:(CallbackFn)fn withCallbackInfo:(CallbackInfo*) cbk {
        [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
            if (error == NULL)
            {
                NSArray *achievements = [NSArray array];
                GKAchievement *a = [[GKAchievement alloc] initWithIdentifier:@"com.siligame.example.defoldgamecenter.achievementA"];
   				a.percentComplete = 65.0;
   				[achievements arrayByAddingObject:a];
   				
   				GKAchievement *b = [[GKAchievement alloc] initWithIdentifier:@"com.siligame.example.defoldgamecenter.achievementB"];
   				b.percentComplete = 75.0;
   				[achievements arrayByAddingObject:b];
   				
   				GKAchievement *c = [[GKAchievement alloc] initWithIdentifier:@"com.siligame.example.defoldgamecenter.achievementC"];
   				c.percentComplete = 85.0;
   				[achievements arrayByAddingObject:c];
				
                if(achievements != NULL && achievements.count > 0) {
                	cbk->m_achievements.OffsetCapacity(achievements.count);
	                for (GKAchievement *achievement in achievements)
	                {
	                   cbk->m_achievements.Push(SAchievement([achievement.identifier UTF8String], achievement.percentComplete));
	                }
                }
            } else {
                cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
            }
            fn(cbk);<commit_msg>Remove unused codes<commit_after>/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *******************************************************************************/
 
#include <dmsdk/sdk.h>
#include <vector>
#include <map>
#include <string>

#include "gamecenter_private.h"
#include <Foundation/Foundation.h>
#include <GameKit/GameKit.h>

#if defined(DM_PLATFORM_IOS)
#include <UIKit/UIKit.h>
#endif

using namespace std;

#if defined(DM_PLATFORM_IOS) || defined(DM_PLATFORM_OSX)
NSString *const PresentAuthenticationViewController = @"present_authentication_view_controller";

@protocol GameCenterManagerDelegate <GKGameCenterControllerDelegate>
@end

#if defined(DM_PLATFORM_IOS)
@interface GameKitManager : UIViewController <GameCenterManagerDelegate>
{
@private UIViewController *m_authenticationViewController;
#else
@interface GameKitManager : NSViewController <GameCenterManagerDelegate>
{
@private NSViewController *m_authenticationViewController;
#endif

@private id<GameCenterManagerDelegate, NSObject> m_delegate;
}
+ (instancetype)sharedGameKitManager;
@end


@implementation GameKitManager

+ (instancetype)sharedGameKitManager
{
    static GameKitManager *sharedGameKitManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedGameKitManager = [[GameKitManager alloc] init];
    });
    return sharedGameKitManager;
}

- (id)init
{
    self = [super init];
    if (self) {
    	m_delegate = self;
    }
    return self;
}

- (void)authenticateLocalPlayer:(CallbackFn) fn withCallbackInfo:(CallbackInfo*) cbk
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    
#if defined(DM_PLATFORM_IOS)
    localPlayer.authenticateHandler  =
    ^(UIViewController *viewController, NSError *error) {
#else        
    localPlayer.authenticateHandler  =
    ^(NSViewController *viewController, NSError *error) {
#endif  
		if (error != nil)
        {
            cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
        }
              
        if(viewController != nil) {
            [self setAuthenticationViewController:viewController];
        } else if([GKLocalPlayer localPlayer].isAuthenticated) {
        	fn(cbk);
        } else {
        	cbk->m_Error = new GKError(GKErrorUnknown, "Unknown");
        	fn(cbk);
        }
    };
}

#if defined(DM_PLATFORM_IOS)
- (void)setAuthenticationViewController:(UIViewController *)authenticationViewController
#else
- (void)setAuthenticationViewController:(NSViewController *)authenticationViewController
#endif 
	{   
    m_authenticationViewController = authenticationViewController;
    [[NSNotificationCenter defaultCenter]
     postNotificationName:PresentAuthenticationViewController
     object:self];
}


- (void) login:(CallbackFn) fn withCallbackInfo:(CallbackInfo*) cbk
{
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(showAuthenticationViewController)
     name:PresentAuthenticationViewController
     object:nil];
    
    [self authenticateLocalPlayer:fn withCallbackInfo:cbk];
}

- (void) reportScore:(NSString*)leaderboardId score:(int)score withCallback:(CallbackFn) fn withCallbackInfo:(CallbackInfo*) cbk
{
    GKScore* scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier:leaderboardId];
    scoreReporter.value = (int64_t)score;
    [GKScore reportScores:@[scoreReporter] withCompletionHandler:^(NSError *error) {
        if (error)
        {
            cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
        }
        fn(cbk);
    }];
}
    
- (void)submitAchievement:(NSString*)identifier withPercentComplete:(double)percentComplete withCallback:(CallbackFn)fn withCallbackInfo:(CallbackInfo*) cbk
{
    GKAchievement *achievement = [[[GKAchievement alloc] initWithIdentifier:identifier]  autorelease];
    [achievement setPercentComplete:percentComplete];
    achievement.showsCompletionBanner = YES;
    [achievement reportAchievementWithCompletionHandler:^(NSError  *error) {
        if (error)
        {
            cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
        }
        fn(cbk);
    }];
}

- (void)loadAchievements:(CallbackFn)fn withCallbackInfo:(CallbackInfo*) cbk {
        [GKAchievement loadAchievementsWithCompletionHandler:^(NSArray *achievements, NSError *error) {
            if (error == NULL)
            {
                if(achievements != NULL && achievements.count > 0) {
                	cbk->m_achievements.OffsetCapacity(achievements.count);
	                for (GKAchievement *achievement in achievements)
	                {
	                   cbk->m_achievements.Push(SAchievement([achievement.identifier UTF8String], achievement.percentComplete));
	                }
                }
            } else {
                cbk->m_Error = new GKError([error code], [[error localizedDescription] UTF8String]);
            }
            fn(cbk);
{'fraction_non_alphanumeric': 0.11982091982091982, 'fraction_numerical': 0.002686202686202686, 'mean_word_length': 3.9821573398215735, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6316770', 'n_tokens_mistral': 3599, 'n_tokens_neox': 3344, 'n_words': 750}