/// <summary> /// Gets the appropriate call model by the associated component name. /// </summary> /// <param name="component"></param> /// <returns></returns> public static CallModel getByComponentName(string component) { CallModel model = null; Type modelType = null; if (!String.IsNullOrEmpty(component)) { modelType = Type.GetType("io.newgrounds.components." + component); } if (modelType != null) { model = (CallModel)Activator.CreateInstance(modelType); } return(model); }
/// <summary> /// Creates a objects.call object using the provided parameters /// </summary> /// <param name="component"></param> /// <param name="parameters"></param> /// <param name="callback"></param> /// <returns></returns> private objects.call _createCall(string component, object parameters = null, Action <ResultModel> callback = null) { objects.call c = new objects.call(); CallModel cm = CallModel.getByComponentName(component); if (cm == null) { throw new ArgumentException("Unknown component \"" + component + "\""); } if (parameters != null && (parameters.GetType() == typeof(CallProperties) || parameters.GetType().IsSubclassOf(typeof(CallProperties)))) { cm.setProperties((CallProperties)parameters); parameters = cm; } if (parameters != null && parameters.GetType() != cm.GetType()) { throw new ArgumentException("parameters must be set in an io.newgrounds.CallProperties or " + cm.GetType().ToString() + " instance."); } c.component = component; if (cm.secure) { secureCall scall = new secureCall(); scall.component = component; scall.parameters = parameters; string jscall = scall.toJSON(); debug_log("Created secure call from: " + jscall); c.secure = crypto.encrypt(jscall); } else { c.parameters = parameters; } c.callback = callback; return(c); }
/// This handles the actual transactions with the Newgrounds.io server. This runs as a Coroutine in Unity. private IEnumerator _executeCallList(SimpleJSONImportableList call_list) { objects.input input = new objects.input(); input.app_id = app_id; if (!string.IsNullOrEmpty(session_id)) { input.session_id = session_id; } SimpleJSONImportableList calls = new SimpleJSONImportableList(typeof(objects.call)); objects.call vc; CallModel cm; ResultModel vr; for (int c = 0; c < call_list.Count; c++) { vc = (objects.call)call_list[c]; cm = CallModel.getByComponentName(vc.component); if (cm.require_session && string.IsNullOrEmpty(session_id)) { vr = (ResultModel)ResultModel.getByComponentName(vc.component); vr.setIsLocal(true); vr.setCall(vc); vr.component = vc.component; vr.success = false; vr.error.message = vc.component + " requires a valid user session."; vr.ngio_core = this; debug_log("Call failed local validation:\n" + vc.toJSON()); debug_log("Local Response:\n" + vr.toJSON()); if (vc.callback != null) { vc.callback(vr); } vr.dispatchMe(vc.component); continue; } else { calls.Add(vc); } } if (calls.Count > 0) { input.call = calls; string json = input.toJSON(); debug_log("Sent to Server:\n" + json); WWWForm webform = new WWWForm(); webform.AddField("input", json); WWW json_results = new WWW(GATEWAY_URI, webform); yield return(json_results); debug_log("Server Response:\n" + json_results.text); // hopefully, our results will populate to this objects.output _output = new objects.output(); string default_error = "There was a problem connecting to the server at '" + GATEWAY_URI + "'."; // we'll try decoding what the server sent back. If it's valid JSON there should be no problems try { // decode the overall response JObject jobject = JSONDecoder.Decode(json_results.text); // populate the basic info our output model will need to proceed if (jobject.Kind == JObjectKind.Object) { _output.setPropertiesFromSimpleJSON(jobject); } } // catch any exceptions and update the generic error message we'll spit back. catch (Exception e) { Debug.LogWarning("Caught an exception decoding the data:\n" + e.Message); default_error = e.Message; } _output.ngio_core = this; // We'll typically only get here if the was a server or connection error. if (_output.success != true && _output.error == null) { _output.error = new objects.error(); _output.error.message = default_error; } // cheap way to fill debug info even with debug mode being off _output.debug.input = input; ResultModel _rm; objects.result _result; objects.call call; for (int i = 0; i < calls.Count; i++) { call = (objects.call)calls[i]; if (_output.success != true) { _rm = new ResultModel(); _rm.error = _output.error; } else if (_output.result.GetType() == typeof(SimpleJSONImportableList) && ((SimpleJSONImportableList)_output.result).ElementAtOrDefault <object>(i) != null) { _result = (objects.result)((SimpleJSONImportableList)_output.result)[i]; _result.ngio_core = this; if (_result.component != call.component) { _rm = new ResultModel(); _rm.success = false; _rm.error.message = "Unexpected index mismatch in API response!"; } else { _rm = (ResultModel)_result.data; } } else { _rm = new ResultModel(); _rm.success = false; _rm.error.message = "Unexpected index mismatch in API response!"; } _rm.ngio_core = this; _rm.setCall(call); if (call.callback != null) { call.callback(_rm); } _rm.dispatchMe(call.component); } } }