public Dictionary <string, string> ToDictionary() { if (type == Type.OBJECT) { Dictionary <string, string> result = new Dictionary <string, string>(); for (int i = 0; i < list.Count; i++) { JSONObject val = (JSONObject)list[i]; switch (val.type) { case Type.STRING: result.Add((string)keys[i], val.str); break; case Type.NUMBER: result.Add((string)keys[i], val.n + ""); break; case Type.BOOL: result.Add((string)keys[i], val.b + ""); break; default: OKLog.Warn("Omitting object: " + (string)keys[i] + " in dictionary conversion"); break; } } return(result); } else { OKLog.Warn("Tried to turn non-Object JSONObject into a dictionary"); } return(null); }
public static void GetLeaderboards(String leaderboardListTag, Action <List <OKLeaderboard>, OKException> requestHandler) { Dictionary <string, object> requestParams = new Dictionary <string, object>(); requestParams.Add("tag", leaderboardListTag); OKCloudAsyncRequest.Get("/leaderboards", requestParams, (JSONObject responseObj, OKCloudException e) => { if (e != null) { OKLog.Error("Getting leaderboards failed with error: " + e); requestHandler(null, e); } else { Debug.Log("Got leaderboards"); if (responseObj.type == JSONObject.Type.ARRAY) { List <OKLeaderboard> leaderboardList = new List <OKLeaderboard>(responseObj.list.Count); for (int x = 0; x < responseObj.list.Count; x++) { OKLeaderboard leaderboard = new OKLeaderboard(responseObj[x]); leaderboardList.Add(leaderboard); } requestHandler(leaderboardList, null); } else { OKLog.Error("Expected an array of leaderboards but did not get back an Array JSON"); requestHandler(null, new OKException("Expected an array of leaderboards but did not get back an Array JSON")); } } }); }
public static void SetCtx(System.Threading.SynchronizationContext c) { Ctx = c; if (Ctx != null) { OKLog.Info("SynchronizationContext is set."); } }
public void _AuthenticateLocalPlayerWithGameCenterAndShowGameCenterUIIfNecessary() { #if UNITY_IPHONE && !UNITY_EDITOR ((OpenKitIOS)nativeBridge).AuthenticateLocalPlayerToGCAndShowUIIfNecessary(); #else OKLog.Info("AuthenticateLocalPlayerWithGameCenterAndShowGameCenterUIIfNecessary ONLY supported on iOS"); #endif }
public void CancelMetadataRequest() { if (MetadataRequestInProgress()) { OKLog.Info("Cancelling the request for metadata of score: " + ScoreID); _metadataRequest.Cancel(); } }
public void asyncCallSucceeded(string paramString) { OKLog.Info("asyncCallSucceeded"); if (functionCallback != null) { functionCallback(true, paramString); } GameObject.Destroy(this.gameObject); }
public bool _IsPlayerAuthenticatedWithGameCenter() { #if UNITY_IPHONE && !UNITY_EDITOR return(((OpenKitIOS)nativeBridge).IsPlayerAuthenticatedWithGameCenter()); #else OKLog.Info("_IsPlayerAuthenticatedWithGameCenter ONLY supported on iOS"); return(false); #endif }
public void asyncCallFailed(string errorString) { OKLog.Error("asyncCallFailed: " + errorString); if (functionCallback != null) { functionCallback(false, errorString); } GameObject.Destroy(this.gameObject); }
public void Add(JSONObject obj) { if (obj != null) //Don't do anything if the object is null { if (type != JSONObject.Type.ARRAY) { type = JSONObject.Type.ARRAY; //Congratulations, son, you're an ARRAY now OKLog.Warn("tried to add an object to a non-array JSONObject. We'll do it for you, but you might be doing something wrong."); } list.Add(obj); } }
public static void ShowLoginUIWithCallback(Action callback) { OKLog.Info("ShowLoginUI on OKLoginRequest called"); GameObject gameObject = new GameObject("ShowOpenKitLoginUITempObject" + DateTime.Now.Ticks); OKLoginRequest loginRequest = gameObject.AddComponent <OKLoginRequest>(); Action <bool, string> wrapperCallback = (success, stringRetVal) => { OKLog.Info("Wrapper callback called"); callback(); }; loginRequest.callFunction(wrapperCallback); }
public void AddField(string name, JSONObject obj) { if (obj != null) //Don't do anything if the object is null { if (type != JSONObject.Type.OBJECT) { type = JSONObject.Type.OBJECT; //Congratulations, son, you're an OBJECT now OKLog.Warn("tried to add a field to a non-object JSONObject. We'll do it for you, but you might be doing something wrong."); } keys.Add(name); list.Add(obj); } }
///////////////////////////////////////////////////////////////////////////////////////////////////// #region Instance ///////////////////////////////////////////////////////////////////////////////////////////////////// public void _Configure(string appKey, string secretKey, string endpoint) { _appKey = appKey; _secretKey = secretKey; if (endpoint != null) { _endpoint = endpoint; } else { _endpoint = DEFAULT_ENDPOINT; } OKLog.Info("OpenKit configured with endpoint: " + _endpoint); nativeBridge.Configure(_appKey, _secretKey, _endpoint); }
static public void Main() { Console.WriteLine(CommandHelp); while (true) { Console.Write(">> "); string line = Console.ReadLine(); switch (line) { case "l": OKServiceRequest.Get("/leaderboards", null, response => { OKLog.Info("Response Json Object: " + response.Obj); }); break; case "h": Console.WriteLine(CommandHelp); break; case "sh": Console.Write("Enter the new host (e.g. api.openkit.io): "); string h = Console.ReadLine(); var u = new Uri(String.Format("http://{0}", h)); OKServiceRequest.Host = u.Host; OKServiceRequest.Port = u.Port; break; case "sa": Console.Write("Enter the new app key: "); string a = Console.ReadLine(); OKServiceRequest.AppKey = a; break; case "ss": Console.Write("Enter the new secret key: "); string s = Console.ReadLine(); OKServiceRequest.SecretKey = s; break; case "p": Console.WriteLine("Not implemented"); break; } } // System.Threading.Thread.Sleep(Timeout.Infinite); }
// Helper function for getting scores from OpenKit, internal use only private void GetScores(ScoreRequestType rt, Dictionary <string, object> requestParams, Action <List <OKScore>, OKException> requestHandler) { Action <JSONObject, OKCloudException> internalHandler = (responseObj, e) => { if (e == null) { if (responseObj.type == JSONObject.Type.ARRAY) { OKLog.Info("Successfully got " + responseObj.list.Count + " scores"); // OKLog.Info("GetScores Response json: " + responseObj.ToString()); List <OKScore> scoresList = new List <OKScore>(responseObj.list.Count); for (int x = 0; x < responseObj.list.Count; x++) { OKScore score = new OKScore(responseObj[x]); scoresList.Add(score); } requestHandler(scoresList, null); } else { requestHandler(null, new OKException("Expected an array of scores but did not get back an Array JSON")); } } else { requestHandler(null, e); } }; switch (rt) { case ScoreRequestType.Global: OKCloudAsyncRequest.Get("/best_scores", requestParams, internalHandler); break; case ScoreRequestType.Social: OKCloudAsyncRequest.Post("/best_scores/social", requestParams, internalHandler); break; } }
public void mSet(object o, String key, Action <OKCloudException> handler) { OKUser u = GetUser(); if (u == null) { throw new Exception("You need a user to perform cloud set."); } Dictionary <string, object> reqParams = new Dictionary <string, object>(); reqParams.Add("user_id", u.OKUserID.ToString()); reqParams.Add("field_key", key); reqParams.Add("field_value", o); OKCloudAsyncRequest.Post("/developer_data", reqParams, (JSONObject responseObj, OKCloudException e) => { if (e != null) { OKLog.Error("Async post failed with error " + e); } handler(e); }); }
public OKManager() { #if UNITY_ANDROID && !UNITY_EDITOR nativeBridge = new OpenKitAndroid(); #elif UNITY_IPHONE && !UNITY_EDITOR nativeBridge = new OpenKitIOS(); #else nativeBridge = new OpenKitDummyObject(); #endif syncContext = SynchronizationContext.Current; if (syncContext == null) { OKLog.Info("SynchronizationContext.Current is null."); } else { OKLog.Info("SynchronizationContext is set."); } _endpoint = DEFAULT_ENDPOINT; }
public RestRequest GetHttpRequest() { RestRequest httpRequest = null; switch (_verb) { case OKRestVerb.Get: httpRequest = BuildGetRequest(_path, _params); break; case OKRestVerb.Post: httpRequest = (_upload == null) ? BuildPostRequest(_path, _params) : BuildMultipartPostRequest(_path, _params, _upload); break; case OKRestVerb.Put: httpRequest = BuildPutRequest(_path, _params); break; default: OKLog.Error("Doing it wrong!"); break; } return(httpRequest); }
public string print(int depth) //Convert the JSONObject into a stiring { if (depth++ > MAX_DEPTH) { OKLog.Info("reached max depth!"); return(""); } string str = ""; switch (type) { case Type.STRING: str = "\"" + this.str + "\""; break; case Type.NUMBER: str += n; break; case JSONObject.Type.OBJECT: if (list.Count > 0) { str = "{"; #if (READABLE) //for a bit more readability, comment the define above to save space str += "\n"; depth++; #endif for (int i = 0; i < list.Count; i++) { string key = (string)keys[i]; JSONObject obj = (JSONObject)list[i]; if (obj != null) { #if (READABLE) for (int j = 0; j < depth; j++) { str += "\t"; //for a bit more readability } #endif str += "\"" + key + "\":"; str += obj.print(depth) + ","; #if (READABLE) str += "\n"; #endif } } #if (READABLE) str = str.Substring(0, str.Length - 1); #endif str = str.Substring(0, str.Length - 1); str += "}"; } else { str += "null"; } break; case JSONObject.Type.ARRAY: str = "["; if (list.Count > 0) { #if (READABLE) str += "\n"; //for a bit more readability depth++; #endif foreach (JSONObject obj in list) { if (obj != null) { #if (READABLE) for (int j = 0; j < depth; j++) { str += "\t"; //for a bit more readability } #endif str += obj.print(depth) + ","; #if (READABLE) str += "\n"; //for a bit more readability #endif } } #if (READABLE) str = str.Substring(0, str.Length - 1); #endif str = str.Substring(0, str.Length - 1); } str += "]"; break; case Type.BOOL: if (b) { str += "true"; } else { str += "false"; } break; case Type.NULL: str = "null"; break; } return(str); }
public static JSONObject jsonObjectify(object o) { JSONObject j = null; Type ot = o.GetType(); if (ot == typeof(String)) { j = new JSONObject { type = JSONObject.Type.STRING, str = (string)o }; } else if (ot == typeof(ArrayList)) { j = new JSONObject { type = JSONObject.Type.ARRAY }; foreach (object element in (ArrayList)o) { j.Add(jsonObjectify(element)); } } else if (ot.IsGenericType && (ot.GetGenericTypeDefinition() == typeof(List <>))) { j = new JSONObject { type = JSONObject.Type.ARRAY }; // There must be a better way to do this at runtime. Right now we get // the type T in List<T> and cast 'o' to it. Type lt = ot.GetGenericArguments()[0]; if (lt == typeof(string)) { foreach (object element in (List <string>)o) { j.Add(jsonObjectify(element)); } } else { OKLog.Error("Lists of that type are not supported by JSONObjectExt! Fix me."); } } else if (ot == typeof(Dictionary <string, object>)) { j = new JSONObject { type = JSONObject.Type.OBJECT }; foreach (KeyValuePair <string, object> entry in (Dictionary <string, object>)o) { j.AddField(entry.Key, jsonObjectify(entry.Value)); } } else if (ot.IsPrimitive) { if (typeof(bool) == ot) { j = new JSONObject { type = JSONObject.Type.BOOL, b = (bool)o }; } else { double d = Convert.ToDouble(o); j = new JSONObject { type = JSONObject.Type.NUMBER, n = d }; } } return(j); }
public JSONObject(string str) //create a new JSONObject from a string (this will also create any children, and parse the whole string) //OKLog.Info(str); { if (str != null) { #if (READABLE) str = str.Replace("\\n", ""); str = str.Replace("\\t", ""); str = str.Replace("\\r", ""); str = str.Replace("\t", ""); str = str.Replace("\n", ""); str = str.Replace("\\", ""); #endif if (str.Length > 0) { if (string.Compare(str, "true", true) == 0) { type = Type.BOOL; b = true; } else if (string.Compare(str, "false", true) == 0) { type = Type.BOOL; b = false; } else if (str == "null") { type = Type.NULL; } else if (str[0] == '"') { type = Type.STRING; this.str = str.Substring(1, str.Length - 2); } else { try { n = System.Convert.ToDouble(str); type = Type.NUMBER; } catch (System.FormatException) { int token_tmp = 0; /* * Checking for the following formatting (www.json.org) * object - {"field1":value,"field2":value} * array - [value,value,value] * value - string - "string" * - number - 0.0 * - bool - true -or- false * - null - null */ switch (str[0]) { case '{': type = Type.OBJECT; keys = new ArrayList(); list = new ArrayList(); break; case '[': type = JSONObject.Type.ARRAY; list = new ArrayList(); break; default: type = Type.NULL; OKLog.Warn("improper JSON formatting:" + str); return; } int depth = 0; bool openquote = false; bool inProp = false; for (int i = 1; i < str.Length; i++) { if (str[i] == '\\') { i++; continue; } if (str[i] == '"') { openquote = !openquote; } if (str[i] == '[' || str[i] == '{') { depth++; } if (depth == 0 && !openquote) { if (str[i] == ':' && !inProp) { inProp = true; try { keys.Add(str.Substring(token_tmp + 2, i - token_tmp - 3)); } catch { OKLog.Info(i + " - " + str.Length + " - " + str); } token_tmp = i; } if (str[i] == ',') { inProp = false; list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1))); token_tmp = i; } if (str[i] == ']' || str[i] == '}') { if (i - token_tmp > 1) { list.Add(new JSONObject(str.Substring(token_tmp + 1, i - token_tmp - 1))); } } } if (str[i] == ']' || str[i] == '}') { depth--; } } } } } } else { type = Type.NULL; //If the string is missing, this is a null } }