Exemplo n.º 1
0
        /// <summary>
        /// This will check the server connection, check any version or liscence settings.
        /// If log_view is true and version/license settings pass, a view event will be logged on the server.
        /// </summary>
        /// <param name="log_view">Set to false to skip logging a view event.</param>
        /// <param name="client_version">Set to the version number of your current build.</param>
        /// <param name="callback">An optional callback handler that will fire when the server responds.</param>
        public void connect(bool log_view = true, string client_version = null, Action <connectStatus> callback = null)
        {
            connectStatus status = new connectStatus();

            components.App.getHostLicense license = new components.App.getHostLicense();

            components.App.getCurrentVersion version = new components.App.getCurrentVersion();
            version.version = client_version;

            components.App.logView view = new components.App.logView();

            int completed_calls = 0;
            int total_calls     = 2;

            ResultModel connect_result = null;

            Action onConnectResult = () =>
            {
                if (completed_calls >= total_calls)
                {
                    return;
                }

                status.success = connect_result.success;
                status.error   = connect_result.error;

                if (connect_result.success)
                {
                    completed_calls++;
                }
                else
                {
                    completed_calls = total_calls;
                }

                if (completed_calls >= total_calls)
                {
                    if (log_view)
                    {
                        view.callWith(this);
                    }
                    if (callback != null)
                    {
                        callback(status);
                    }
                }
            };

            license.queueWith(this, (results.App.getHostLicense r) =>
            {
                status.host_approved = r.host_approved;
                connect_result       = (ResultModel)r;
                onConnectResult();
            }
                              );

            version.queueWith(this, (results.App.getCurrentVersion r) =>
            {
                status.current_version   = r.current_version;
                status.client_deprecated = r.client_deprecated;
                connect_result           = (ResultModel)r;
                onConnectResult();
            }
                              );

            executeQueuedComponents();
        }
Exemplo n.º 2
0
        /// 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);

                // 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 + "'.";

                using (UnityWebRequest json_results = UnityWebRequest.Post(GATEWAY_URI, webform))
                {
                    yield return(json_results.SendWebRequest());

                    debug_log("Server Response:\n" + json_results.downloadHandler.text);

                    // 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.downloadHandler.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);
                }
            }
        }