Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/gamecenter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
`request_achievement_descriptions()` - Downloads the achievement descriptions from iOS `GameCenter`. Generates new event with `achievement_descriptions` type.
`show_game_center(Dictionary screen_dictionary)` - Displays Game Center information of your game. Generates new event with `show_game_center` type when information screen closes.
`request_identity_verification_signature()` - Creates a signature for a third-party server to authenticate the local player. Generates new event with `identity_verification_signature` type.
`save_game_data(Dictionary save_dictionary)` - Save data to iCloud. Generates new event with `save_game_data` type.
`fetch_saved_games()` - Fetch saved games. Generates new event with `fetch_saved_games` type containing an array of `saved_games`. To load one of the games' data, call `load_data` and wait for the event with `saved_game_loaded` type.
`delete_saved_games(String name)` - Delete saved games that match the specified name. Generates new event with `delete_saved_games` type.
`resolve_conflicting_saved_games(Dictionary resolve_conflict_dictionary)` - Resolve conflicting saved games by passing an array of `saved_games` and the `data` that resolves the conflict. Call this if you receive an event of type `conflicting_saved_games`. Generates new event with `resolve_conflicting_saved_games` type.

## Properties

Expand Down
12 changes: 12 additions & 0 deletions plugins/gamecenter/game_center.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@

#if VERSION_MAJOR == 4
#include "core/object/class_db.h"
typedef PackedByteArray GodotByteArray;
#else
#include "core/object.h"
typedef PoolByteArray GodotByteArray;
#endif

class GameCenterSavedGame;

class GameCenter : public Object {

GDCLASS(GameCenter, Object);
Expand All @@ -64,7 +68,15 @@ class GameCenter : public Object {
Error show_game_center(Dictionary p_params);
Error request_identity_verification_signature();

Error save_game_data(Dictionary p_params);
Error fetch_saved_games();
Error delete_saved_games(String p_name);
Error resolve_conflicting_saved_games(Dictionary p_params);

void game_center_closed();
void game_center_saved_game_loaded(GameCenterSavedGame *saved_game, const GodotByteArray& data, int64_t error_code, const char *error_description);
void player_has_conflicting_saved_games(const Array& saved_games);
void player_did_modify_saved_game(GameCenterSavedGame *saved_game);

int get_pending_event_count();
Variant pop_pending_event();
Expand Down
161 changes: 161 additions & 0 deletions plugins/gamecenter/game_center.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "game_center.h"

#import "game_center_delegate.h"
#import "game_center_saved_game.h"

#if VERSION_MAJOR == 4
#import "platform/ios/app_delegate.h"
Expand All @@ -42,14 +43,22 @@

#import <GameKit/GameKit.h>

static void *_get_ptr(const GodotByteArray& arr);

#if VERSION_MAJOR == 4
typedef PackedStringArray GodotStringArray;
typedef PackedInt32Array GodotIntArray;
typedef PackedFloat32Array GodotFloatArray;
static void *_get_ptr(const GodotByteArray& arr) {
return (void *) arr.ptr();
}
#else
typedef PoolStringArray GodotStringArray;
typedef PoolIntArray GodotIntArray;
typedef PoolRealArray GodotFloatArray;
static void *_get_ptr(const GodotByteArray& arr) {
return (void *) arr.read().ptr();
}
#endif

GameCenter *GameCenter::instance = NULL;
Expand All @@ -66,6 +75,11 @@
ClassDB::bind_method(D_METHOD("request_achievement_descriptions"), &GameCenter::request_achievement_descriptions);
ClassDB::bind_method(D_METHOD("show_game_center"), &GameCenter::show_game_center);
ClassDB::bind_method(D_METHOD("request_identity_verification_signature"), &GameCenter::request_identity_verification_signature);

ClassDB::bind_method(D_METHOD("save_game_data"), &GameCenter::save_game_data);
ClassDB::bind_method(D_METHOD("fetch_saved_games"), &GameCenter::fetch_saved_games);
ClassDB::bind_method(D_METHOD("delete_saved_games", "name"), &GameCenter::delete_saved_games);
ClassDB::bind_method(D_METHOD("resolve_conflicting_saved_games"), &GameCenter::resolve_conflicting_saved_games);

ClassDB::bind_method(D_METHOD("get_pending_event_count"), &GameCenter::get_pending_event_count);
ClassDB::bind_method(D_METHOD("pop_pending_event"), &GameCenter::pop_pending_event);
Expand Down Expand Up @@ -367,13 +381,158 @@
return OK;
};

Error GameCenter::save_game_data(Dictionary p_params) {
ERR_FAIL_COND_V(![GKLocalPlayer instancesRespondToSelector:@selector(saveGameData:withName:completionHandler:)], ERR_UNAVAILABLE);
ERR_FAIL_COND_V(!p_params.has("name") || !p_params.has("data"), ERR_INVALID_PARAMETER);

String name = p_params["name"];
GodotByteArray data = p_params["data"];

NSString *nsname = [[NSString alloc] initWithUTF8String:name.utf8().get_data()];
NSData *nsdata = [[NSData alloc] initWithBytes:_get_ptr(data) length:data.size()];
[GKLocalPlayer.localPlayer saveGameData:nsdata withName:nsname completionHandler:^(GKSavedGame * _Nullable savedGame, NSError * _Nullable error) {
Dictionary ret;
ret["type"] = "save_game_data";
ret["name"] = name;
if (savedGame) {
ret["result"] = "ok";
ret["saved_game"] = memnew(GameCenterSavedGame(savedGame));
}
else {
ret["result"] = "error";
ret["error_code"] = (int64_t)error.code;
ret["error_description"] = [error.localizedDescription UTF8String];
}
pending_events.push_back(ret);
}];

return OK;
}

Error GameCenter::fetch_saved_games() {
ERR_FAIL_COND_V(![GKLocalPlayer instancesRespondToSelector:@selector(fetchSavedGamesWithCompletionHandler:)], ERR_UNAVAILABLE);

[GKLocalPlayer.localPlayer fetchSavedGamesWithCompletionHandler:^(NSArray<GKSavedGame *> * _Nullable savedGames, NSError * _Nullable error) {
Dictionary ret;
ret["type"] = "fetch_saved_games";
if (savedGames) {
ret["result"] = "ok";
Array array;
for (GKSavedGame *savedGame in savedGames) {
array.append(memnew(GameCenterSavedGame(savedGame)));
}
ret["saved_games"] = array;
}
else {
ret["result"] = "error";
ret["error_code"] = (int64_t)error.code;
ret["error_description"] = [error.localizedDescription UTF8String];
}
pending_events.push_back(ret);
}];

return OK;
}

Error GameCenter::delete_saved_games(String p_name) {
ERR_FAIL_COND_V(![GKLocalPlayer instancesRespondToSelector:@selector(deleteSavedGamesWithName:completionHandler:)], ERR_UNAVAILABLE);

NSString *nsname = [[NSString alloc] initWithUTF8String:p_name.utf8().get_data()];
[GKLocalPlayer.localPlayer deleteSavedGamesWithName:nsname completionHandler:^(NSError * _Nullable error) {
Dictionary ret;
ret["type"] = "delete_saved_games";
ret["name"] = p_name;
if (!error) {
ret["result"] = "ok";
}
else {
ret["result"] = "error";
ret["error_code"] = (int64_t)error.code;
ret["error_description"] = [error.localizedDescription UTF8String];
}
pending_events.push_back(ret);
}];

return OK;
}

Error GameCenter::resolve_conflicting_saved_games(Dictionary p_params) {
ERR_FAIL_COND_V(![GKLocalPlayer instancesRespondToSelector:@selector(resolveConflictingSavedGames:withData:completionHandler:)], ERR_UNAVAILABLE);
ERR_FAIL_COND_V(!p_params.has("saved_games") || !p_params.has("data"), ERR_INVALID_PARAMETER);

Array saved_games = p_params["saved_games"];
GodotByteArray data = p_params["data"];

NSMutableArray *nssaved_games = [[NSMutableArray alloc] init];
for (int i = 0; i < saved_games.size(); i++) {
if (GameCenterSavedGame *saved_game = Object::cast_to<GameCenterSavedGame>(saved_games[i])) {
[nssaved_games addObject:saved_game->get_saved_game()];
}
}

NSData *nsdata = [[NSData alloc] initWithBytes:_get_ptr(data) length:data.size()];
[GKLocalPlayer.localPlayer resolveConflictingSavedGames:nssaved_games withData:nsdata completionHandler:^(NSArray<GKSavedGame *> * _Nullable savedGames, NSError * _Nullable error) {
Dictionary ret;
ret["type"] = "resolve_conflicting_saved_games";
if (savedGames) {
ret["result"] = "ok";
Array array;
for (GKSavedGame *savedGame in savedGames) {
array.append(memnew(GameCenterSavedGame(savedGame)));
}
ret["saved_games"] = array;
}
else {
ret["result"] = "error";
ret["error_code"] = (int64_t)error.code;
ret["error_description"] = [error.localizedDescription UTF8String];
}
pending_events.push_back(ret);
}];

return OK;
}

void GameCenter::game_center_closed() {
Dictionary ret;
ret["type"] = "show_game_center";
ret["result"] = "ok";
pending_events.push_back(ret);
}

void GameCenter::game_center_saved_game_loaded(GameCenterSavedGame *saved_game, const GodotByteArray& data, int64_t error_code, const char *error_description) {
Dictionary ret;
ret["type"] = "saved_game_loaded";
ret["name"] = saved_game->get_name();
ret["saved_game"] = saved_game;
if (error_code == 0) {
ret["result"] = "ok";
ret["data"] = data;
}
else {
ret["result"] = "error";
ret["error_code"] = error_code;
ret["error_description"] = error_description;
}
pending_events.push_back(ret);
}

void GameCenter::player_has_conflicting_saved_games(const Array& saved_games) {
Dictionary ret;
ret["type"] = "conflicting_saved_games";
ret["result"] = "ok";
ret["saved_games"] = saved_games;
pending_events.push_back(ret);
}

void GameCenter::player_did_modify_saved_game(GameCenterSavedGame *saved_game) {
Dictionary ret;
ret["type"] = "player_did_modify_saved_game";
ret["result"] = "ok";
ret["saved_game"] = saved_game;
pending_events.push_back(ret);
}

int GameCenter::get_pending_event_count() {
return pending_events.size();
};
Expand All @@ -395,10 +554,12 @@
authenticated = false;

gameCenterDelegate = [[GodotGameCenterDelegate alloc] init];
[GKLocalPlayer.localPlayer registerListener:gameCenterDelegate];
};

GameCenter::~GameCenter() {
if (gameCenterDelegate) {
[GKLocalPlayer.localPlayer unregisterListener:gameCenterDelegate];
gameCenterDelegate = nil;
}
}
2 changes: 1 addition & 1 deletion plugins/gamecenter/game_center_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@

#import <GameKit/GameKit.h>

@interface GodotGameCenterDelegate : NSObject <GKGameCenterControllerDelegate>
@interface GodotGameCenterDelegate : NSObject <GKGameCenterControllerDelegate, GKLocalPlayerListener>

@end
17 changes: 17 additions & 0 deletions plugins/gamecenter/game_center_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#import "game_center_delegate.h"

#include "game_center.h"
#include "game_center_saved_game.h"

@implementation GodotGameCenterDelegate

Expand All @@ -42,4 +43,20 @@ - (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCent
[gameCenterViewController dismissViewControllerAnimated:YES completion:nil];
}

- (void)player:(GKPlayer *)player hasConflictingSavedGames:(NSArray<GKSavedGame *> *)savedGames {
if (GameCenter::get_singleton()) {
Array gsaved_games;
for (GKSavedGame *savedGame in savedGames) {
gsaved_games.append(memnew(GameCenterSavedGame(savedGame)));
}
GameCenter::get_singleton()->player_has_conflicting_saved_games(gsaved_games);
}
}

- (void)player:(GKPlayer *)player didModifySavedGame:(GKSavedGame *)savedGame {
if (GameCenter::get_singleton()) {
GameCenter::get_singleton()->player_did_modify_saved_game(memnew(GameCenterSavedGame(savedGame)));
}
}

@end
69 changes: 69 additions & 0 deletions plugins/gamecenter/game_center_saved_game.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*************************************************************************/
/* game_center_saved_game.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef GAME_CENTER_SAVED_GAME_H
#define GAME_CENTER_SAVED_GAME_H

#include "core/version.h"

#if VERSION_MAJOR == 4
#include "core/object/ref_counted.h"
#else
#include "core/reference.h"
typedef Reference RefCounted;
#endif

@class GKSavedGame;

class GameCenterSavedGame : public RefCounted {

GDCLASS(GameCenterSavedGame, RefCounted);

static void _bind_methods();

GKSavedGame *saved_game;

public:
String get_name() const;
int64_t get_modification_date() const;
String get_device_name() const;
bool is_current_device() const;

GKSavedGame *get_saved_game() const;

void load_data();

virtual String to_string() override;

GameCenterSavedGame(GKSavedGame *saved_game);
~GameCenterSavedGame();
};

#endif // GAME_CENTER_SAVED_GAME_H
Loading