// PRIVATE METHODS

    IEnumerator LoginAsGuest_Coroutine()
    {
        ShowMessage(MESSAGE_WAIT, false);
        yield return(new WaitForSeconds(0.1f));

#if TEST_IOS_VENDOR_ID
        string userid = string.Empty;
#else
        string userid = SysUtils.GetUniqueDeviceID();
#endif

        string vendorID = string.Empty;
#if UNITY_IPHONE || TEST_IOS_VENDOR_ID
        char deviceType = 'i';

        // it can happen that vendotID is null
        // so we need to try it again
        while (string.IsNullOrEmpty(vendorID) == true)
        {
            vendorID = MFNativeUtils.IOS.VendorId;

            yield return(new WaitForEndOfFrame());
        }

        // should we ask the user to migrate his account?
        if (string.IsNullOrEmpty(userid) == true)
        {
            string storedUsername   = PlayerPrefs.GetString(CloudUser.USERNAME_KEY, string.Empty);
            string storedPrimaryKey = PlayerPrefs.GetString(CloudUser.PRIMARY_KEY_KEY, string.Empty);
            string storedPassword   = PlayerPrefs.GetString(CloudUser.PASSWORD_HASH_KEY, string.Empty);

            if (string.IsNullOrEmpty(storedPrimaryKey) == true)
            {
                storedPrimaryKey = storedUsername;
            }

            if (string.IsNullOrEmpty(storedUsername) == false && storedUsername.StartsWith("guest") == true)
            {
                string id     = string.IsNullOrEmpty(userid) ? vendorID : userid;
                string idtype = string.IsNullOrEmpty(userid) ? CloudServices.LINK_ID_TYPE_IOSVENDOR : CloudServices.LINK_ID_TYPE_DEVICE;

                //Debug.Log(">>>> ID="+id+", IDType="+idtype);

                GetPrimaryKeyLinkedWithID action = new GetPrimaryKeyLinkedWithID(E_UserAcctKind.Guest, id, idtype);
                GameCloudManager.AddAction(action);

                while (action.isDone == false)
                {
                    yield return(new WaitForEndOfFrame());
                }

                //Debug.Log(">>>> action.isSucceeded="+action.isSucceeded+", action.primaryKey="+action.primaryKey+", storedPrimaryKey="+storedPrimaryKey);

                if (action.isSucceeded == true && action.isPrimaryKeyForSHDZ == true && action.primaryKey != storedPrimaryKey)
                {
                    HideMessage();

                    bool migrate = false;
                    GuiPopupMigrateGuest migratePopup = (GuiPopupMigrateGuest)Owner.ShowPopup("MigrateGuest", null, null, (inPopup, inResult) => {
                        migrate = inResult == E_PopupResultCode.Ok;
                    });
                    migratePopup.Usage      = GuiPopupMigrateGuest.E_Usage.QuickPlay;
                    migratePopup.PrimaryKey = storedPrimaryKey;
                    migratePopup.Password   = storedPassword;

                    while (migratePopup.IsVisible == true)
                    {
                        yield return(new WaitForEndOfFrame());
                    }

                    if (migrate == true)
                    {
                        yield break;
                    }
                }
            }
        }
#else
        char deviceType = 'a';
#endif

        string username = null;
        string nickname = null;
        string email    = "";
        string password = null;

        if (string.IsNullOrEmpty(userid) == false)
        {
            username = string.Format("guest{0}{1}", userid.ToLower(), deviceType);
            password = string.Format("pwd{0}!", userid);
        }
        else
        {
            password = string.Format("pwd{0}!", string.Format("VID{0}", utils.CryptoUtils.CalcSHA1Hash(vendorID)));
        }

        List <string> usernames = new List <string>();
        usernames.AddRange(GuiBaseUtils.GenerateUsernames("guest", false));
        if (string.IsNullOrEmpty(username) == false)
        {
            usernames.Add(username);
        }

        //Debug.Log(">>>> GUEST :: username="******", nickname="+nickname+", email="+email+", password="+password);

        yield return
            (StartCoroutine(Login_Coroutine(E_UserAcctKind.Guest, userid, vendorID, username, usernames.ToArray(), nickname, password, email)));
    }
Exemple #2
0
    IEnumerator Login_Coroutine()
    {
        GuiPopupMessageBox msgBox =
            Owner.ShowPopup("MessageBox", TextDatabase.instance[02040016], TextDatabase.instance[02040017]) as GuiPopupMessageBox;

        // get primary key
        string primaryKey;

        {
            UserGetPrimaryKey action = new UserGetPrimaryKey(m_UserName);
            GameCloudManager.AddAction(action);

            while (action.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            primaryKey = action.primaryKey;
        }

#if UNITY_IPHONE || TEST_IOS_VENDOR_ID
        if (string.IsNullOrEmpty(m_UserName) == false && m_UserName.StartsWith("guest") == true)
        {
            string userid   = SysUtils.GetUniqueDeviceID();
            string vendorID = null;

            while (string.IsNullOrEmpty(vendorID) == true)
            {
                vendorID = MFNativeUtils.IOS.VendorId;

                yield return(new WaitForEndOfFrame());
            }

            string id     = string.IsNullOrEmpty(userid) ? vendorID : userid;
            string idtype = string.IsNullOrEmpty(userid) ? CloudServices.LINK_ID_TYPE_IOSVENDOR : CloudServices.LINK_ID_TYPE_DEVICE;

            //Debug.Log(">>>> ID="+id+", IDType="+idtype);

            GetPrimaryKeyLinkedWithID action = new GetPrimaryKeyLinkedWithID(E_UserAcctKind.Guest, id, idtype);
            GameCloudManager.AddAction(action);

            while (action.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            msgBox.ForceClose();

            //Debug.Log(">>>> action.isSucceeded="+action.isSucceeded+", action.primaryKey="+action.primaryKey+", primaryKey="+primaryKey);

            bool force   = action.isFailed == true || action.isPrimaryKeyForSHDZ == false || action.primaryKey != primaryKey;
            bool migrate = false;
            GuiPopupMigrateGuest migratePopup = (GuiPopupMigrateGuest)Owner.ShowPopup("MigrateGuest", null, null, (inPopup, inResult) => {
                migrate = inResult == E_PopupResultCode.Ok;
            });
            migratePopup.Usage      = force ? GuiPopupMigrateGuest.E_Usage.QuickPlay : GuiPopupMigrateGuest.E_Usage.LoginScreen;
            migratePopup.PrimaryKey = primaryKey;
            migratePopup.Password   = m_PasswordHash;

            while (migratePopup.IsVisible == true)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (migrate == true)
            {
                yield break;
            }
        }
#endif

        if (msgBox.IsVisible == true)
        {
            msgBox.ForceClose();
        }

        CloudUser cloudUser = CloudUser.instance;
        cloudUser.SetLoginData(primaryKey, m_UserName, m_UserName, m_PasswordHash, m_PasswordLength, m_RememberMe, m_AutoLogin);

        CloudUser.instance.AuthenticateLocalUser();
        Owner.ShowPopup("Authentication", TextDatabase.instance[02040016], "", AuthenticationResultHandler);

        // Invoke("AuthenticationDeadLoopFixer", 20);
        // TODO disable Login screen.
    }
    IEnumerator Login_Coroutine(E_UserAcctKind kind,
                                string userid,
                                string vendorID,
                                string legacyUsername,
                                string[] usernames,
                                string nickname,
                                string password,
                                string email)
    {
        if (string.IsNullOrEmpty(userid) == true && string.IsNullOrEmpty(vendorID) == true)
        {
            ShowMessage(MESSAGE_FAILED, true);
            yield break;
        }

        string pwdhash    = CloudServices.CalcPasswordHash(password);
        string username   = null;
        string primaryKey = null;

        ShowMessage(MESSAGE_WAIT, false);
        yield return(new WaitForSeconds(0.1f));

#if UNITY_IPHONE
        // fail when there is invalid guest account based on invalid MAC address generated
        if (legacyUsername == "guest6024a02ea5577bcb6442a70d0bfa791935839db3i")
        {
            Debug.LogWarning("It's not possible to deduce valid guest account. Login failed!");
            ShowMessage(MESSAGE_FAILED, true);
            yield break;
        }
#endif

        // check if username exists
        bool legacyUsernameExists = false;
        if (string.IsNullOrEmpty(legacyUsername) == false)
        {
            // NOTE: this check is obsolete but we need to do it
            //       to support legacy accounts
            UsernameAlreadyExists usernameExists = CloudUser.instance.CheckIfUserNameExist(legacyUsername);
            while (usernameExists.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (usernameExists.isFailed == true)
            {
                ShowMessage(MESSAGE_FAILED, true);
                yield break;
            }

            legacyUsernameExists = usernameExists.userExist;
        }

        if (legacyUsernameExists == true)
        {
            username = legacyUsername;

            // obtain primaryKey using legacyUsername
            UserGetPrimaryKey action = new UserGetPrimaryKey(legacyUsername);
            GameCloudManager.AddAction(action);

            while (action.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (action.isSucceeded == true)
            {
                primaryKey = action.primaryKey;
            }
        }
        else
        {
            // obtain primaryKey using userid
            string idType = kind == E_UserAcctKind.Guest ? CloudServices.LINK_ID_TYPE_DEVICE : CloudServices.LINK_ID_TYPE_FACEBOOK;

            GetPrimaryKeyLinkedWithID action = new GetPrimaryKeyLinkedWithID(kind, userid, idType);
            GameCloudManager.AddAction(action);

            while (action.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            if (action.isSucceeded == true && action.isPrimaryKeyForSHDZ == true)
            {
                primaryKey = action.primaryKey;
            }
        }

#if UNITY_IPHONE || TEST_IOS_VENDOR_ID
        if (kind == E_UserAcctKind.Guest && string.IsNullOrEmpty(primaryKey) == true)
        {
            // obtain username using userid
            GetPrimaryKeyLinkedWithID action = new GetPrimaryKeyLinkedWithID(kind, vendorID, CloudServices.LINK_ID_TYPE_IOSVENDOR);
            GameCloudManager.AddAction(action);

            while (action.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            //Debug.Log(">>>> action.isSucceeded="+action.isSucceeded+", action.primaryKey="+action.primaryKey);

            if (action.isSucceeded == true && action.isPrimaryKeyForSHDZ == true)
            {
                primaryKey = action.primaryKey;
            }
        }
#endif

        // create new account if needed
        if (string.IsNullOrEmpty(primaryKey) == true)
        {
            ShowMessage(MESSAGE_NEW_ACCT, false);

            // check available names
            UsernamesAlreadyExist checkAvailableNames = new UsernamesAlreadyExist(usernames);
            GameCloudManager.AddAction(checkAvailableNames);
            while (checkAvailableNames.isDone == false)
            {
                yield return(new WaitForEndOfFrame());
            }

            List <string> availableNames = new List <string>();
            foreach (var pair in checkAvailableNames.usernames)
            {
                if (pair.Exists == false)
                {
                    availableNames.Add(pair.Username);
                }
            }

            if (availableNames.Count == 0)
            {
                ShowMessage(MESSAGE_FAILED, true);
                yield break;
            }

            username = availableNames[0];
            nickname = string.IsNullOrEmpty(nickname) == true ? username : nickname.RemoveDiacritics();

            //Debug.Log(string.Join(System.Environment.NewLine, availableNames.ToArray()));
            //Debug.Log(">>>> "+kind+" :: username="******", nickname="+nickname+", email="+email+", password="******">>>> LOGIN :: primaryKey="+primaryKey+", username="******", nickname="+nickname+", email="+email+", password="******"Authentication",
                            TextDatabase.instance[02040016],
                            "",
                            (inPopup, inResult) =>
            {
                if (inResult == E_PopupResultCode.Success)
                {
                    Owner.Exit();
                }
            });
        }
    }