2015-09-28 01:54:20 +02:00
/*************************************************************************/
2017-09-01 16:07:55 +02:00
/* icloud.mm */
2015-09-28 01:54:20 +02:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
2017-08-27 14:16:55 +02:00
/* https://godotengine.org */
2015-09-28 01:54:20 +02:00
/*************************************************************************/
2020-01-01 11:16:22 +01:00
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
2015-09-28 01:54:20 +02:00
/* */
/* 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. */
/*************************************************************************/
2018-01-05 00:50:27 +01:00
2015-09-28 01:54:20 +02:00
#ifdef ICLOUD_ENABLED
#include "icloud.h"
2015-11-16 02:31:44 +01:00
#ifndef __IPHONE_9_0
extern "C" {
#endif
2015-09-28 01:54:20 +02:00
#import "app_delegate.h"
2017-04-09 14:18:49 +02:00
#import <Foundation/Foundation.h>
2015-11-16 02:31:44 +01:00
#ifndef __IPHONE_9_0
};
#endif
2017-04-09 14:18:49 +02:00
ICloud *ICloud::instance = NULL;
2015-09-28 01:54:20 +02:00
void ICloud::_bind_methods() {
2017-04-09 13:22:40 +02:00
ClassDB::bind_method(D_METHOD("remove_key"), &ICloud::remove_key);
2020-07-15 20:59:57 +02:00
2017-04-09 13:22:40 +02:00
ClassDB::bind_method(D_METHOD("set_key_values"), &ICloud::set_key_values);
ClassDB::bind_method(D_METHOD("get_key_value"), &ICloud::get_key_value);
2020-07-15 20:59:57 +02:00
2017-04-09 13:22:40 +02:00
ClassDB::bind_method(D_METHOD("synchronize_key_values"), &ICloud::synchronize_key_values);
ClassDB::bind_method(D_METHOD("get_all_key_values"), &ICloud::get_all_key_values);
ClassDB::bind_method(D_METHOD("get_pending_event_count"), &ICloud::get_pending_event_count);
ClassDB::bind_method(D_METHOD("pop_pending_event"), &ICloud::pop_pending_event);
2015-09-28 01:54:20 +02:00
};
int ICloud::get_pending_event_count() {
return pending_events.size();
};
Variant ICloud::pop_pending_event() {
Variant front = pending_events.front()->get();
pending_events.pop_front();
return front;
};
2017-04-09 14:18:49 +02:00
ICloud *ICloud::get_singleton() {
2015-09-28 01:54:20 +02:00
return instance;
};
//convert from apple's abstract type to godot's abstract type....
2017-04-09 14:18:49 +02:00
Variant nsobject_to_variant(NSObject *object) {
2015-09-28 01:54:20 +02:00
if ([object isKindOfClass:[NSString class]]) {
2017-04-09 14:18:49 +02:00
const char *str = [(NSString *)object UTF8String];
2015-09-28 01:54:20 +02:00
return String::utf8(str != NULL ? str : "");
2017-04-09 13:22:40 +02:00
} else if ([object isKindOfClass:[NSData class]]) {
2020-02-22 06:01:34 +01:00
PackedByteArray ret;
2017-04-09 14:18:49 +02:00
NSData *data = (NSData *)object;
2015-09-28 01:54:20 +02:00
if ([data length] > 0) {
ret.resize([data length]);
{
2020-02-22 06:01:34 +01:00
// PackedByteArray::Write w = ret.write();
copymem((void *)ret.ptr(), [data bytes], [data length]);
2015-09-28 01:54:20 +02:00
}
}
return ret;
2017-04-09 13:22:40 +02:00
} else if ([object isKindOfClass:[NSArray class]]) {
2015-09-28 01:54:20 +02:00
Array result;
2017-04-09 14:18:49 +02:00
NSArray *array = (NSArray *)object;
2020-07-15 20:59:57 +02:00
for (NSUInteger i = 0; i < [array count]; ++i) {
2017-04-09 14:18:49 +02:00
NSObject *value = [array objectAtIndex:i];
2015-09-28 01:54:20 +02:00
result.push_back(nsobject_to_variant(value));
}
return result;
2017-04-09 13:22:40 +02:00
} else if ([object isKindOfClass:[NSDictionary class]]) {
2015-09-28 01:54:20 +02:00
Dictionary result;
2017-04-09 14:18:49 +02:00
NSDictionary *dic = (NSDictionary *)object;
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
NSArray *keys = [dic allKeys];
2015-09-28 01:54:20 +02:00
int count = [keys count];
2017-04-09 14:18:49 +02:00
for (int i = 0; i < count; ++i) {
NSObject *k = [keys objectAtIndex:i];
NSObject *v = [dic objectForKey:k];
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
result[nsobject_to_variant(k)] = nsobject_to_variant(v);
}
return result;
2017-04-09 13:22:40 +02:00
} else if ([object isKindOfClass:[NSNumber class]]) {
2015-09-28 01:54:20 +02:00
//Every type except numbers can reliably identify its type. The following is comparing to the *internal* representation, which isn't guaranteed to match the type that was used to create it, and is not advised, particularly when dealing with potential platform differences (ie, 32/64 bit)
//To avoid errors, we'll cast as broadly as possible, and only return int or float.
//bool, char, int, uint, longlong -> int
//float, double -> float
2017-04-09 14:18:49 +02:00
NSNumber *num = (NSNumber *)object;
if (strcmp([num objCType], @encode(BOOL)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant((int)[num boolValue]);
2017-04-09 14:18:49 +02:00
} else if (strcmp([num objCType], @encode(char)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant((int)[num charValue]);
2017-04-09 14:18:49 +02:00
} else if (strcmp([num objCType], @encode(int)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant([num intValue]);
2017-04-09 14:18:49 +02:00
} else if (strcmp([num objCType], @encode(unsigned int)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant((int)[num unsignedIntValue]);
2017-04-09 14:18:49 +02:00
} else if (strcmp([num objCType], @encode(long long)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant((int)[num longValue]);
2017-04-09 14:18:49 +02:00
} else if (strcmp([num objCType], @encode(float)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant([num floatValue]);
2017-04-09 14:18:49 +02:00
} else if (strcmp([num objCType], @encode(double)) == 0) {
2015-09-28 01:54:20 +02:00
return Variant((float)[num doubleValue]);
2018-02-25 00:11:03 +01:00
} else {
return Variant();
2015-09-28 01:54:20 +02:00
}
2017-04-09 13:22:40 +02:00
} else if ([object isKindOfClass:[NSDate class]]) {
2015-09-28 01:54:20 +02:00
//this is a type that icloud supports...but how did you submit it in the first place?
2016-04-02 20:26:12 +02:00
//I guess this is a type that *might* show up, if you were, say, trying to make your game
2015-09-28 01:54:20 +02:00
//compatible with existing cloud data written by another engine's version of your game
2019-06-11 14:49:34 +02:00
WARN_PRINT("NSDate unsupported, returning null Variant");
2015-09-28 01:54:20 +02:00
return Variant();
2017-04-09 13:22:40 +02:00
} else if ([object isKindOfClass:[NSNull class]] or object == nil) {
2015-09-28 01:54:20 +02:00
return Variant();
2017-04-09 13:22:40 +02:00
} else {
2015-09-28 01:54:20 +02:00
WARN_PRINT("Trying to convert unknown NSObject type to Variant");
return Variant();
}
}
2017-04-09 14:18:49 +02:00
NSObject *variant_to_nsobject(Variant v) {
2015-09-28 01:54:20 +02:00
if (v.get_type() == Variant::STRING) {
return [[[NSString alloc] initWithUTF8String:((String)v).utf8().get_data()] autorelease];
2020-07-15 20:59:57 +02:00
} else if (v.get_type() == Variant::FLOAT) {
2015-09-28 01:54:20 +02:00
return [NSNumber numberWithDouble:(double)v];
2017-04-09 13:22:40 +02:00
} else if (v.get_type() == Variant::INT) {
2015-09-28 01:54:20 +02:00
return [NSNumber numberWithLongLong:(long)(int)v];
2017-04-09 13:22:40 +02:00
} else if (v.get_type() == Variant::BOOL) {
2015-09-28 01:54:20 +02:00
return [NSNumber numberWithBool:BOOL((bool)v)];
2017-04-09 13:22:40 +02:00
} else if (v.get_type() == Variant::DICTIONARY) {
2017-04-09 14:18:49 +02:00
NSMutableDictionary *result = [[[NSMutableDictionary alloc] init] autorelease];
2015-09-28 01:54:20 +02:00
Dictionary dic = v;
Array keys = dic.keys();
2020-07-15 20:59:57 +02:00
for (int i = 0; i < keys.size(); ++i) {
2017-04-09 14:18:49 +02:00
NSString *key = [[[NSString alloc] initWithUTF8String:((String)(keys[i])).utf8().get_data()] autorelease];
NSObject *value = variant_to_nsobject(dic[keys[i]]);
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
if (key == NULL || value == NULL) {
return NULL;
}
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
[result setObject:value forKey:key];
2016-04-02 20:26:12 +02:00
}
2015-09-28 01:54:20 +02:00
return result;
2017-04-09 13:22:40 +02:00
} else if (v.get_type() == Variant::ARRAY) {
2017-04-09 14:18:49 +02:00
NSMutableArray *result = [[[NSMutableArray alloc] init] autorelease];
2015-09-28 01:54:20 +02:00
Array arr = v;
2020-07-15 20:59:57 +02:00
for (int i = 0; i < arr.size(); ++i) {
2017-04-09 14:18:49 +02:00
NSObject *value = variant_to_nsobject(arr[i]);
2015-09-28 01:54:20 +02:00
if (value == NULL) {
//trying to add something unsupported to the array. cancel the whole array
return NULL;
}
[result addObject:value];
}
return result;
2020-02-22 06:01:34 +01:00
} else if (v.get_type() == Variant::PACKED_BYTE_ARRAY) {
PackedByteArray arr = v;
// PackedByteArray::Read r = arr.read();
NSData *result = [NSData dataWithBytes:arr.ptr() length:arr.size()];
2015-09-28 01:54:20 +02:00
return result;
}
2017-04-09 14:18:49 +02:00
WARN_PRINT(String("Could not add unsupported type to iCloud: '" + Variant::get_type_name(v.get_type()) + "'").utf8().get_data());
2015-09-28 01:54:20 +02:00
return NULL;
}
2020-07-15 20:59:57 +02:00
Error ICloud::remove_key(String p_param) {
NSString *key = [[[NSString alloc] initWithUTF8String:p_param.utf8().get_data()] autorelease];
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
if (![[store dictionaryRepresentation] objectForKey:key]) {
return ERR_INVALID_PARAMETER;
}
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
[store removeObjectForKey:key];
return OK;
}
//return an array of the keys that could not be set
2020-07-15 20:59:57 +02:00
Array ICloud::set_key_values(Dictionary p_params) {
Array keys = p_params.keys();
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
Array error_keys;
2016-04-02 20:26:12 +02:00
2020-07-15 20:59:57 +02:00
for (int i = 0; i < keys.size(); ++i) {
2015-09-28 01:54:20 +02:00
String variant_key = keys[i];
2020-07-15 20:59:57 +02:00
Variant variant_value = p_params[variant_key];
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
NSString *key = [[[NSString alloc] initWithUTF8String:variant_key.utf8().get_data()] autorelease];
2015-09-28 01:54:20 +02:00
if (key == NULL) {
error_keys.push_back(variant_key);
continue;
}
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
NSObject *value = variant_to_nsobject(variant_value);
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
if (value == NULL) {
error_keys.push_back(variant_key);
continue;
}
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
[store setObject:value forKey:key];
}
return error_keys;
}
2020-07-15 20:59:57 +02:00
Variant ICloud::get_key_value(String p_param) {
NSString *key = [[[NSString alloc] initWithUTF8String:p_param.utf8().get_data()] autorelease];
2015-09-28 01:54:20 +02:00
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
if (![[store dictionaryRepresentation] objectForKey:key]) {
return Variant();
}
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
Variant result = nsobject_to_variant([[store dictionaryRepresentation] objectForKey:key]);
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
return result;
}
Variant ICloud::get_all_key_values() {
Dictionary result;
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
NSDictionary *store_dictionary = [store dictionaryRepresentation];
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
NSArray *keys = [store_dictionary allKeys];
2015-09-28 01:54:20 +02:00
int count = [keys count];
2017-04-09 14:18:49 +02:00
for (int i = 0; i < count; ++i) {
NSString *k = [keys objectAtIndex:i];
NSObject *v = [store_dictionary objectForKey:k];
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
const char *str = [k UTF8String];
2015-09-28 01:54:20 +02:00
if (str != NULL) {
result[String::utf8(str)] = nsobject_to_variant(v);
}
}
2016-04-02 20:26:12 +02:00
2015-09-28 01:54:20 +02:00
return result;
}
Error ICloud::synchronize_key_values() {
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
BOOL result = [store synchronize];
if (result == YES) {
return OK;
2017-04-09 13:22:40 +02:00
} else {
2015-09-28 01:54:20 +02:00
return FAILED;
}
}
2020-05-14 14:29:06 +02:00
2015-09-28 01:54:20 +02:00
/*
Error ICloud::initial_sync() {
//you sometimes have to write something to the store to get it to download new data. go apple!
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
if ([store boolForKey:@"isb"])
2017-04-09 13:22:40 +02:00
{
[store setBool:NO forKey:@"isb"];
}
else
{
[store setBool:YES forKey:@"isb"];
}
return synchronize();
2015-09-28 01:54:20 +02:00
}
2020-05-14 14:29:06 +02:00
2015-09-28 01:54:20 +02:00
*/
ICloud::ICloud() {
ERR_FAIL_COND(instance != NULL);
instance = this;
//connected = false;
2016-04-02 20:26:12 +02:00
2017-04-09 14:18:49 +02:00
[[NSNotificationCenter defaultCenter]
addObserverForName:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:[NSUbiquitousKeyValueStore defaultStore]
queue:nil
usingBlock:^(NSNotification *notification) {
NSDictionary *userInfo = [notification userInfo];
NSInteger change = [[userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey] integerValue];
Dictionary ret;
ret["type"] = "key_value_changed";
2020-02-22 06:01:34 +01:00
//PackedStringArray result_keys;
2017-04-09 14:18:49 +02:00
//Array result_values;
Dictionary keyValues;
String reason = "";
if (change == NSUbiquitousKeyValueStoreServerChange) {
reason = "server";
} else if (change == NSUbiquitousKeyValueStoreInitialSyncChange) {
reason = "initial_sync";
} else if (change == NSUbiquitousKeyValueStoreQuotaViolationChange) {
reason = "quota_violation";
} else if (change == NSUbiquitousKeyValueStoreAccountChange) {
reason = "account";
}
ret["reason"] = reason;
NSUbiquitousKeyValueStore *store = [NSUbiquitousKeyValueStore defaultStore];
NSArray *keys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
for (NSString *key in keys) {
const char *str = [key UTF8String];
if (str == NULL) {
continue;
}
NSObject *object = [store objectForKey:key];
//figure out what kind of object it is
Variant value = nsobject_to_variant(object);
keyValues[String::utf8(str)] = value;
}
ret["changed_values"] = keyValues;
pending_events.push_back(ret);
}];
2015-09-28 01:54:20 +02:00
}
2020-05-12 17:01:17 +02:00
ICloud::~ICloud() {}
2015-09-28 01:54:20 +02:00
#endif