Пример #1
0
        //add a login callback associated with an instance of the API
        internal static void add_login_callback(KerbalXAPI api, AfterLoginCallback callback)
        {
            string key = api.client + "-" + api.client_version;

            after_login_callbacks.Remove(key);
            after_login_callbacks.Add(key, callback);
        }
Пример #2
0
        //Public Authentication methods

        //Login
        //usage:
        //KerbalXAPI api = new KerbalXAPI("mod name", "mod version");
        //api.login((login_successful) => {
        //  if(login_successful){
        //      //some action to run once logged in
        //  }
        //});
        //Can also be called without a callback:
        //api.login()
        //
        //If the API is already logged in, then the callback is called instantly with the argument given as True
        //If the API is not yet logged in, but a KerbalX.key (token) file exists, it authenticates the token with KerbalX and if it's valid the callback is called with True
        //If either the token is invalid or not present it opens the login UI. Once the user has logged in the callback is called with True.
        //The only time the callback will be called with False as the argument is if the user cancels the login process.
        public void login(AfterLoginCallback callback)
        {
            if (KerbalXAPIHelper.instance.on_main_menu)
            {
                if (logged_in)
                {
                    callback(true);
                }
                else
                {
                    KerbalXLoginUI.add_login_callback(this, callback); //callback is stashed in a Dictionary on the loginUI and will be called once login has completed or been canclled.
                }
                check_api_helper_state();
                KerbalXLoginUI.open();
            }
            else
            {
                if (logged_in)
                {
                    callback(true);  //call the callback instantly if the API is already logged in
                }
                else
                {
                    KerbalXAPI kxapi = null;
                    if (instances.ContainsKey("KerbalXAPI"))
                    {
                        kxapi = instances["KerbalXAPI"];
                    }
                    else
                    {
                        kxapi = new KerbalXAPI("KerbalXAPI", KXAPI.version);
                    }

                    kxapi.login((resp, code) => { //validate the user's authentication token with KerbalX
                        if (code == 200)
                        {
                            callback(true); //If the token is valid then call the callback
                        }
                        else
                        {
                            //If the token is either invalid or not present, trigger the LoginUI
                            check_api_helper_state();
                            KerbalXLoginUI.add_login_callback(this, callback); //callback is stashed in a Dictionary on the loginUI and will be called once login has completed or been canclled.
                            KerbalXLoginUI.open();                             //Open the LoginUI (request made via the APIHelper which needs to have been started before this point).
                        }
                    });
                }
            }
        }