Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System;
- using System.Collections.Generic;
- using UnityEngine.UI;
- using CodeStage.AntiCheat.ObscuredTypes;
- public class AS_CanvasUI : MonoBehaviour
- {
- /*
- * --- TODO: CUSTOMIZE ---
- *
- * Called upon Successful Login - Add any custom logic here
- * You could load a level, initialize some characters or
- * download additional info. The latter can be done by an
- * AS_AccountManagementGUI instance.
- */
- private string finalUsername;
- private string finalPassword;
- private ObscuredInt accountID;
- public GameObject loading;
- public InputField clanTagField;
- public static AS_CanvasUI singleton;
- public bool useUI = true;
- public void OnSuccessfulLogin(int id, bool ui = true)
- {
- accountID = id;
- if (useUI)
- {
- //this.Log(LogType.Log, "Successfully Logged In User with id: " + id + " - Add any custom Logic Here!");
- loginState = AS_LoginState.LoginSuccessful;
- }
- ObscuredPrefs.SetInt("loginID", id);
- //Application.LoadLevel ("MultiplayerScene");
- accountInfo.customInfo = new AS_CustomInfo();
- accountInfo.TryToDownload (id, AccountInfoDownloaded);
- /*if (accountManagementGUI)
- {
- accountManagementGUI.enabled = true;
- accountManagementGUI.Init(id);
- }*/
- }
- public void AccountInfoUploaded (string message)
- {
- accountInfo.TryToDownload (accountID, AccountInfoDownloaded);
- }
- void UploadUnlockedCamos(int index, string weaponName)
- {
- List<CamoManager.CamoType> unlockedCamos = new List<CamoManager.CamoType>();
- foreach (CamoManager.CamoType t in System.Enum.GetValues (typeof(CamoManager.CamoType))) {
- if (t != CamoManager.CamoType.None) {
- if (ObscuredPrefs.GetInt(weaponName + "_HASCAMO_" + (int)t) == 1)
- {
- unlockedCamos.Add(t);
- }
- }
- }
- accountInfo.customInfo.weaponInfo[index].camos = new int[unlockedCamos.Count];
- for (int j = 0; j < unlockedCamos.Count; j++)
- {
- accountInfo.customInfo.weaponInfo[index].camos[j] = (int)unlockedCamos[j];
- }
- }
- void UploadUnlockedSights(int index, string weaponName)
- {
- List<AttachmentManager.SightType> unlocked = new List<AttachmentManager.SightType>();
- foreach (AttachmentManager.SightType t in System.Enum.GetValues (typeof(AttachmentManager.SightType))) {
- if (t != AttachmentManager.SightType.None) {
- if (ObscuredPrefs.GetInt(weaponName + "_SIGHT_" + (int)t) == 1)
- {
- unlocked.Add(t);
- }
- }
- }
- accountInfo.customInfo.weaponInfo[index].sights = new int[unlocked.Count];
- for (int j = 0; j < unlocked.Count; j++)
- {
- accountInfo.customInfo.weaponInfo[index].sights[j] = (int)unlocked[j];
- }
- }
- void UploadUnlockedBarrels(int index, string weaponName)
- {
- List<AttachmentManager.BarrelType> unlocked = new List<AttachmentManager.BarrelType>();
- foreach (AttachmentManager.BarrelType t in System.Enum.GetValues (typeof(AttachmentManager.BarrelType))) {
- if (t != AttachmentManager.BarrelType.None) {
- if (ObscuredPrefs.GetInt(weaponName + "_BARREL_" + (int)t) == 1)
- {
- unlocked.Add(t);
- }
- }
- }
- accountInfo.customInfo.weaponInfo[index].barrels = new int[unlocked.Count];
- for (int j = 0; j < unlocked.Count; j++)
- {
- accountInfo.customInfo.weaponInfo[index].barrels[j] = (int)unlocked[j];
- }
- }
- void UploadUnlockedAccessories(int index, string weaponName)
- {
- List<AttachmentManager.AccessoryType> unlocked = new List<AttachmentManager.AccessoryType>();
- foreach (AttachmentManager.AccessoryType t in System.Enum.GetValues (typeof(AttachmentManager.AccessoryType))) {
- if (t != AttachmentManager.AccessoryType.None) {
- if (ObscuredPrefs.GetInt(weaponName + "_ACCESSORY_" + (int)t) == 1)
- {
- unlocked.Add(t);
- }
- }
- }
- accountInfo.customInfo.weaponInfo[index].accessories = new int[unlocked.Count];
- for (int j = 0; j < unlocked.Count; j++)
- {
- accountInfo.customInfo.weaponInfo[index].accessories[j] = (int)unlocked[j];
- }
- }
- void UploadCamosOnLoadouts(int index, string weaponName, bool blank = false)
- {
- // 3 loadouts total
- accountInfo.customInfo.weaponInfo[index].camosOnLoadouts = new int[3];
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- accountInfo.customInfo.weaponInfo[index].camosOnLoadouts[activeLoadout] = blank ? 0 : ObscuredPrefs.GetInt(activeLoadout + "" + weaponName + "_CAMO");
- }
- }
- void UploadSightsOnLoadouts(int index, string weaponName, bool blank = false)
- {
- // 3 loadouts total
- accountInfo.customInfo.weaponInfo[index].sightsOnLoadouts = new int[3];
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- accountInfo.customInfo.weaponInfo[index].sightsOnLoadouts[activeLoadout] = blank ? 0 : ObscuredPrefs.GetInt(activeLoadout + "" + weaponName + "_SIGHT");
- }
- }
- void UploadBarrelsOnLoadouts(int index, string weaponName, bool blank = false)
- {
- // 3 loadouts total
- accountInfo.customInfo.weaponInfo[index].barrelsOnLoadouts = new int[3];
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- accountInfo.customInfo.weaponInfo[index].barrelsOnLoadouts[activeLoadout] = blank ? 0 : ObscuredPrefs.GetInt(activeLoadout + "" + weaponName + "_BARREL");
- }
- }
- void UploadAccessoriesOnLoadouts(int index, string weaponName, bool blank = false)
- {
- // 3 loadouts total
- accountInfo.customInfo.weaponInfo[index].accessoriesOnLoadouts = new int[3];
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- accountInfo.customInfo.weaponInfo[index].accessoriesOnLoadouts[activeLoadout] = blank ? 0 : ObscuredPrefs.GetInt(activeLoadout + "" + weaponName + "_ACCESSORY");
- }
- }
- public void EditClanTag(string tag)
- {
- ObscuredPrefs.SetString("clantag", tag);
- }
- void UploadBlankInfo()
- {
- accountInfo.customInfo.bfAccountInfo.money = 2000;
- accountInfo.customInfo.bfAccountInfo.xp = 0;
- accountInfo.customInfo.bfAccountInfo.deviceID = SystemInfo.deviceUniqueIdentifier;
- // Make sure, when adding new weapons, to ALWAYS add them to the end of the array or it will break
- string[] weapons = new string[18];
- weapons [0] = "M4A1";
- weapons [1] = "AK-12";
- weapons [2] = "FAMAS";
- weapons [3] = "M40A5";
- weapons [4] = "SAIGA 12K";
- weapons [5] = "SCAR-H";
- weapons [6] = "MPX";
- weapons [7] = "AS VAL";
- weapons [8] = "SRR-61";
- weapons [9] = "MG4";
- weapons [10] = "RPG-7V2";
- weapons [11] = "MP412 REX";
- weapons [12] = "COMPACT .45";
- weapons [13] = "BUTTERFLY KNIFE";
- weapons [14] = "G18";
- weapons [15] = "M320 HE";
- weapons [16] = "M320 DART";
- weapons [17] = "870 MCS";
- accountInfo.customInfo.weaponInfo = new BF_WeaponInfo[weapons.Length];
- for (int i = 0; i < weapons.Length; i++)
- {
- accountInfo.customInfo.weaponInfo[i] = new BF_WeaponInfo (0, 0, new int[3], new int[3], new int[3], new int[3], new int[1], new int[1], new int[1], new int[1]);
- PlayerScript.WeaponType weaponType = GunStats.singleton.WeaponNameToWeaponType(weapons[i]);
- accountInfo.customInfo.weaponInfo[i].weapon = (int)weaponType;
- accountInfo.customInfo.weaponInfo[i].unlockedWeapon = 0;
- accountInfo.customInfo.weaponInfo[i].camos = new int[1];
- accountInfo.customInfo.weaponInfo[i].sights = new int[1];
- accountInfo.customInfo.weaponInfo[i].barrels= new int[1];
- accountInfo.customInfo.weaponInfo[i].accessories = new int[1];
- UploadCamosOnLoadouts(i, weapons[i], true);
- UploadSightsOnLoadouts(i, weapons[i], true);
- UploadBarrelsOnLoadouts(i, weapons[i], true);
- UploadAccessoriesOnLoadouts(i, weapons[i], true);
- }
- string[] throwables = new string[4];
- throwables [0] = "M67 FRAG";
- throwables [1] = "M84 FLASHBANG";
- throwables [2] = "M18 SMOKE GRENADE";
- throwables [3] = "THROWING KNIFE";
- accountInfo.customInfo.throwableInfo = new BF_ThrowableInfo[throwables.Length];
- for (int i = 0; i < throwables.Length; i++)
- {
- accountInfo.customInfo.throwableInfo[i] = new BF_ThrowableInfo (0, 0);
- PlayerScript.WeaponType weaponType = GunStats.singleton.WeaponNameToWeaponType(throwables[i]);
- accountInfo.customInfo.throwableInfo[i].weapon = (int)weaponType;
- accountInfo.customInfo.throwableInfo[i].unlockedWeapon = 0;
- }
- DownloadInfoFromServer(false);
- Intro.SetDefaults(true);
- accountInfo.TryToUpload (accountID, FinishedUpdating);
- }
- void UploadCurrentInfo()
- {
- accountInfo.customInfo.bfAccountInfo.money = ObscuredPrefs.GetInt (CreditsManager.creditsPrefsKey);
- accountInfo.customInfo.bfAccountInfo.xp = ObscuredPrefs.GetInt (XPManager.xpSaveKey);
- accountInfo.customInfo.bfAccountInfo.deviceID = SystemInfo.deviceUniqueIdentifier;
- // Make sure, when adding new weapons, to ALWAYS add them to the end of the array or it will break
- string[] weapons = new string[18];
- weapons [0] = "M4A1";
- weapons [1] = "AK-12";
- weapons [2] = "FAMAS";
- weapons [3] = "M40A5";
- weapons [4] = "SAIGA 12K";
- weapons [5] = "SCAR-H";
- weapons [6] = "MPX";
- weapons [7] = "AS VAL";
- weapons [8] = "SRR-61";
- weapons [9] = "MG4";
- weapons [10] = "RPG-7V2";
- weapons [11] = "MP412 REX";
- weapons [12] = "COMPACT .45";
- weapons [13] = "BUTTERFLY KNIFE";
- weapons [14] = "G18";
- weapons [15] = "M320 HE";
- weapons [16] = "M320 DART";
- weapons [17] = "870 MCS";
- accountInfo.customInfo.weaponInfo = new BF_WeaponInfo[weapons.Length];
- for (int i = 0; i < weapons.Length; i++)
- {
- accountInfo.customInfo.weaponInfo[i] = new BF_WeaponInfo (0, 0, new int[3], new int[3], new int[3], new int[3], new int[1], new int[1], new int[1], new int[1]);
- PlayerScript.WeaponType weaponType = GunStats.singleton.WeaponNameToWeaponType(weapons[i]);
- accountInfo.customInfo.weaponInfo[i].weapon = (int)weaponType;
- accountInfo.customInfo.weaponInfo[i].unlockedWeapon = ObscuredPrefs.GetInt("UNLOCKED_" + weapons[i]);
- if (PlayerPrefs.GetInt("streamer") == 1)
- {
- ObscuredPrefs.SetInt(weapons[i] + "_HASCAMO_" + (int)CamoManager.CamoType.Mobcrush, 1);
- }
- else{
- ObscuredPrefs.SetInt(weapons[i] + "_HASCAMO_" + (int)CamoManager.CamoType.Mobcrush, 0);
- }
- if (PlayerPrefs.GetString("clan").ToLower() == "sxc")
- {
- ObscuredPrefs.SetInt(weapons[i] + "_HASCAMO_" + (int)CamoManager.CamoType.SxC, 1);
- }
- else{
- ObscuredPrefs.SetInt(weapons[i] + "_HASCAMO_" + (int)CamoManager.CamoType.SxC, 0);
- }
- UploadUnlockedCamos(i, weapons[i]);
- UploadUnlockedSights(i, weapons[i]);
- UploadUnlockedBarrels(i, weapons[i]);
- UploadUnlockedAccessories(i, weapons[i]);
- UploadCamosOnLoadouts(i, weapons[i]);
- UploadSightsOnLoadouts(i, weapons[i]);
- UploadBarrelsOnLoadouts(i, weapons[i]);
- UploadAccessoriesOnLoadouts(i, weapons[i]);
- }
- string[] throwables = new string[4];
- throwables [0] = "M67 FRAG";
- throwables [1] = "M84 FLASHBANG";
- throwables [2] = "M18 SMOKE GRENADE";
- throwables [3] = "THROWING KNIFE";
- accountInfo.customInfo.throwableInfo = new BF_ThrowableInfo[throwables.Length];
- for (int i = 0; i < throwables.Length; i++)
- {
- accountInfo.customInfo.throwableInfo[i] = new BF_ThrowableInfo (0, 0);
- PlayerScript.WeaponType weaponType = GunStats.singleton.WeaponNameToWeaponType(throwables[i]);
- accountInfo.customInfo.throwableInfo[i].weapon = (int)weaponType;
- accountInfo.customInfo.throwableInfo[i].unlockedWeapon = ObscuredPrefs.GetInt("UNLOCKED_" + weapons[i]);
- }
- accountInfo.TryToUpload (accountID, FinishedUpdating);
- }
- void DownloadUnlockedCamos(int index, string weaponName)
- {
- foreach (CamoManager.CamoType t in System.Enum.GetValues (typeof(CamoManager.CamoType))) {
- if (t != CamoManager.CamoType.None) {
- ObscuredPrefs.SetInt(weaponName + "_HASCAMO_" + (int)t, accountInfo.customInfo.weaponInfo[index].camos.Contains((int)t) ? 1 : 0);
- }
- }
- }
- void DownloadUnlockedSights(int index, string weaponName)
- {
- foreach (AttachmentManager.SightType t in System.Enum.GetValues (typeof(AttachmentManager.SightType))) {
- if (t != AttachmentManager.SightType.None) {
- ObscuredPrefs.SetInt(weaponName + "_SIGHT_" + (int)t, accountInfo.customInfo.weaponInfo[index].sights.Contains((int)t) ? 1 : 0);
- }
- }
- }
- void DownloadUnlockedBarrels(int index, string weaponName)
- {
- foreach (AttachmentManager.BarrelType t in System.Enum.GetValues (typeof(AttachmentManager.BarrelType))) {
- if (t != AttachmentManager.BarrelType.None) {
- ObscuredPrefs.SetInt(weaponName + "_BARREL_" + (int)t, accountInfo.customInfo.weaponInfo[index].barrels.Contains((int)t) ? 1 : 0);
- }
- }
- }
- void DownloadUnlockedAccessories(int index, string weaponName)
- {
- foreach (AttachmentManager.AccessoryType t in System.Enum.GetValues (typeof(AttachmentManager.AccessoryType))) {
- if (t != AttachmentManager.AccessoryType.None) {
- ObscuredPrefs.SetInt(weaponName + "_ACCESSORY_" + (int)t, accountInfo.customInfo.weaponInfo[index].accessories.Contains((int)t) ? 1 : 0);
- }
- }
- }
- void DownloadCamosOnLoadouts(int index, string weaponName)
- {
- // 3 loadouts total
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- int camo = accountInfo.customInfo.weaponInfo[index].camosOnLoadouts[activeLoadout];
- if (camo == (int)CamoManager.CamoType.None || ObscuredPrefs.GetInt(weaponName + "_HASCAMO_" + camo) == 1)
- {
- ObscuredPrefs.SetInt(activeLoadout + "" + weaponName + "_CAMO", camo);
- }
- else
- {
- ObscuredPrefs.SetInt(activeLoadout + "" + weaponName + "_CAMO", 0);
- }
- }
- }
- void DownloadSightsOnLoadouts(int index, string weaponName)
- {
- // 3 loadouts total
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- ObscuredPrefs.SetInt(activeLoadout + "" + weaponName + "_SIGHT", accountInfo.customInfo.weaponInfo[index].sightsOnLoadouts[activeLoadout]);
- }
- }
- void DownloadBarrelsOnLoadouts(int index, string weaponName)
- {
- // 3 loadouts total
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- ObscuredPrefs.SetInt(activeLoadout + "" + weaponName + "_BARREL", accountInfo.customInfo.weaponInfo[index].barrelsOnLoadouts[activeLoadout]);
- }
- }
- void DownloadAccessoriesOnLoadouts(int index, string weaponName)
- {
- // 3 loadouts total
- for (int activeLoadout = 0; activeLoadout < 3; activeLoadout++)
- {
- ObscuredPrefs.SetInt(activeLoadout + "" + weaponName + "_ACCESSORY", accountInfo.customInfo.weaponInfo[index].accessoriesOnLoadouts[activeLoadout]);
- }
- }
- void DownloadInfoFromServer(bool uploadAfter = true)
- {
- ObscuredPrefs.SetInt (CreditsManager.creditsPrefsKey, accountInfo.customInfo.bfAccountInfo.money);
- ObscuredPrefs.SetInt (XPManager.xpSaveKey, accountInfo.customInfo.bfAccountInfo.xp);
- ObscuredPrefs.SetInt("PastXP", accountInfo.customInfo.bfAccountInfo.xp);
- accountInfo.customInfo.bfAccountInfo.deviceID = SystemInfo.deviceUniqueIdentifier;
- string[] weapons = new string[18];
- weapons [0] = "M4A1";
- weapons [1] = "AK-12";
- weapons [2] = "FAMAS";
- weapons [3] = "M40A5";
- weapons [4] = "SAIGA 12K";
- weapons [5] = "SCAR-H";
- weapons [6] = "MPX";
- weapons [7] = "AS VAL";
- weapons [8] = "SRR-61";
- weapons [9] = "MG4";
- weapons [10] = "RPG-7V2";
- weapons [11] = "MP412 REX";
- weapons [12] = "COMPACT .45";
- weapons [13] = "BUTTERFLY KNIFE";
- weapons [14] = "G18";
- weapons [15] = "M320 HE";
- weapons [16] = "M320 DART";
- weapons [17] = "870 MCS";
- for (int i = 0; i < accountInfo.customInfo.weaponInfo.Length; i++)
- {
- ObscuredPrefs.SetInt("UNLOCKED_" + weapons[i], accountInfo.customInfo.weaponInfo[i].unlockedWeapon);
- DownloadUnlockedCamos(i, weapons[i]);
- DownloadUnlockedSights(i, weapons[i]);
- DownloadUnlockedBarrels(i, weapons[i]);
- DownloadUnlockedAccessories(i, weapons[i]);
- DownloadCamosOnLoadouts(i, weapons[i]);
- DownloadSightsOnLoadouts(i, weapons[i]);
- DownloadBarrelsOnLoadouts(i, weapons[i]);
- DownloadAccessoriesOnLoadouts(i, weapons[i]);
- if (PlayerPrefs.GetInt("streamer") == 1)
- {
- ObscuredPrefs.SetInt(weapons[i] + "_HASCAMO_" + (int)CamoManager.CamoType.Mobcrush, 1);
- }
- else{
- ObscuredPrefs.SetInt(weapons[i] + "_HASCAMO_" + (int)CamoManager.CamoType.Mobcrush, 0);
- }
- }
- string[] throwables = new string[4];
- throwables [0] = "M67 FRAG";
- throwables [1] = "M84 FLASHBANG";
- throwables [2] = "M18 SMOKE GRENADE";
- throwables [3] = "THROWING KNIFE";
- for (int i = 0; i < accountInfo.customInfo.throwableInfo.Length; i++)
- {
- ObscuredPrefs.SetInt("UNLOCKED_" + throwables[i], accountInfo.customInfo.throwableInfo[i].unlockedWeapon);
- }
- if (uploadAfter)
- {
- accountInfo.TryToUpload (accountID, FinishedUpdating);
- }
- }
- public void FinishedUpdating(string message)
- {
- if (CamoCaseManager.singleton)
- {
- CamoCaseManager.singleton.SetCases(accountInfo.customInfo.bfAccountInfo.cases);
- }
- if (LoadoutManager.Instance)
- {
- LoadoutManager.Instance.SetGold(accountInfo.customInfo.bfAccountInfo.gold);
- LoadoutManager.Instance.WeaponPurchased();
- }
- if (GoldManager.singleton)
- {
- GoldManager.singleton.SetGold(accountInfo.customInfo.bfAccountInfo.gold);
- }
- if (CreditsManager.singleton)
- {
- CreditsManager.singleton.FinishedUploadingCredits();
- }
- if (useUI)
- {
- Application.LoadLevel ("MultiplayerScene");
- }
- }
- public void AccountInfoDownloaded (string message)
- {
- accountInfo.customInfo.bfAccountInfo.gold += ObscuredPrefs.GetInt("goldtoadd");
- accountInfo.customInfo.bfAccountInfo.v = PNetworkManager.version;
- switch (Application.platform)
- {
- case RuntimePlatform.IPhonePlayer:
- accountInfo.customInfo.bfAccountInfo.platform = "iOS";
- break;
- case RuntimePlatform.Android:
- accountInfo.customInfo.bfAccountInfo.platform = "Android";
- break;
- case RuntimePlatform.OSXEditor:
- accountInfo.customInfo.bfAccountInfo.platform = "Android";
- break;
- default:
- accountInfo.customInfo.bfAccountInfo.platform = "";
- break;
- }
- if (ObscuredPrefs.GetInt("goldtoadd") > 0)
- {
- accountInfo.customInfo.bfAccountInfo.totalGoldBought += ObscuredPrefs.GetInt("goldtoadd");
- }
- ObscuredPrefs.SetInt("cases", accountInfo.customInfo.bfAccountInfo.cases);
- ObscuredPrefs.SetInt("goldtoadd", 0);
- // These lines are to fool hackers
- PlayerPrefs.SetInt("gold", accountInfo.customInfo.bfAccountInfo.gold);
- PlayerPrefs.SetInt("credits", accountInfo.customInfo.bfAccountInfo.money);
- if (accountInfo.customInfo.bfAccountInfo.hacker)
- {
- ObscuredPrefs.SetBool("hacker", accountInfo.customInfo.bfAccountInfo.hacker);
- }
- PlayerPrefs.SetInt("streamer", accountInfo.customInfo.bfAccountInfo.streamer?1:0);
- PlayerPrefs.SetString("clan", accountInfo.customInfo.bfAccountInfo.clan);
- if (accountInfo.customInfo.bfAccountInfo.deviceID == "error")
- {
- Debug.Log("ERROR!!! :(");
- // There was an error deserializing the custom info XML, so upload correct data
- UploadCurrentInfo();
- }
- else if (accountInfo.customInfo.bfAccountInfo.deviceID == "") {
- // First time logging in after making new account
- //UploadCurrentInfo();
- UploadBlankInfo();
- }
- else if (accountInfo.GetFieldValue("username").ToLower() != ObscuredPrefs.GetString ("username").ToLower())
- {
- // Not first time logging in after making new account
- // Switching accounts or first time logging into existing account after installing app
- DownloadInfoFromServer();
- }
- else if ((accountInfo.customInfo.bfAccountInfo.deviceID != SystemInfo.deviceUniqueIdentifier ||
- accountInfo.customInfo.bfAccountInfo.xp > ObscuredPrefs.GetInt(XPManager.xpSaveKey))) {
- Debug.Log("LENGTH: " + accountInfo.customInfo.weaponInfo.Length);
- // Not first time logging in
- // Not switching accounts
- // Logging in from a different device (or same device with glitch) or account has more XP than device
- if (accountInfo.customInfo.weaponInfo.Length > 10)
- {
- DownloadInfoFromServer();
- }
- else{
- UploadCurrentInfo();
- //UploadBlankInfo();
- }
- } else if (accountInfo.customInfo.bfAccountInfo.deviceID == SystemInfo.deviceUniqueIdentifier) {
- // Normal log in to old account with same device
- // Update account info with local info
- UploadCurrentInfo();
- }
- if (useUI)
- {
- ObscuredPrefs.SetString ("username", finalUsername);
- ObscuredPrefs.SetString ("password", finalPassword);
- }
- ObscuredPrefs.Save();
- }
- // Messages to the user
- /// <summary>
- /// Make sure there's a child with that name in every canvas group
- /// </summary>
- public string guiMessageTextName = "GUI Message";
- /// <summary>
- /// Gui messages disappear after this time. Set negative or 0 to keep indefinitely.
- /// </summary>
- public float guiMessageTime = 5;
- // The parent groups of the different login states
- public CanvasGroup loginParent, registrationParent, recoveryParent;
- public InputField usernameField, passwordField, recoveryField;
- /// <summary>
- /// Container to place all the registration fields. The list is populated online by quering the server.
- /// </summary>
- public GridLayoutGroup registrationFieldsContainer;
- /// <summary>
- /// A prefab to serve as a registration input field
- /// </summary>
- public GameObject inputFieldPrefab;
- /// <summary>
- /// Only need to set this if using Password Recovery (from Setup)
- /// </summary>
- public Button recoveryButton;
- Text guiMessageText;
- AS_AccountInfo accountInfo = new AS_AccountInfo();
- AS_MySQLField passwordConfirm, emailConfirm;
- AS_AccountManagementGUI accountManagementGUI;
- // Shut everything off
- void Awake()
- {
- singleton = this;
- loginState = AS_LoginState.Idle;
- }
- // Check if we're good to go, and load up the first screen
- void Start()
- {
- if (useUI)
- {
- accountManagementGUI = GetComponentInChildren<AS_AccountManagementGUI> ();
- if (!loginParent || !registrationParent || !recoveryParent
- || !registrationFieldsContainer || !inputFieldPrefab) {
- this.Log (LogType.Exception, "Unassigned variables - make sure you have assigned all the variables in this object");
- return;
- }
- if ((!recoveryButton||!recoveryField) && AS_Preferences.enablePasswordRecovery) {
- this.Log (LogType.Exception, "You haven't assigned the Recovery button and/or field. Either disable password recovery from Setup, or assign a button and a field.");
- return;
- } else if ((recoveryButton || recoveryField) && !AS_Preferences.enablePasswordRecovery) {
- this.Log(LogType.Warning, "You have assigned a Recovery button and/or field, but haven't enabled password recovery in the setup. Disabling the button & field");
- if (recoveryButton)
- recoveryButton.gameObject.SetActive(false);
- if (recoveryField)
- recoveryField.gameObject.SetActive(false);
- }
- loginState = AS_LoginState.LoginPrompt;
- passwordField.text = ObscuredPrefs.GetString ("password");
- usernameField.text = ObscuredPrefs.GetString ("username");
- clanTagField.text = ObscuredPrefs.GetString("clantag");
- if (PlayerPrefs.GetInt("autologin") == 1)
- {
- PlayerPrefs.SetInt("autologin", 0);
- OnLoginRequested();
- }
- }
- }
- public string guiMessage { set {
- if (!guiMessageText)
- return;
- guiMessageText.text = value;
- if(guiMessageTime > 0)
- {
- StopCoroutine("ClearGUIText");
- StartCoroutine ("ClearGUIText", guiMessageText);
- } } }
- // If the state changes update messages / load level
- AS_LoginState _loginState = AS_LoginState.Idle;
- AS_LoginState loginState
- {
- get { return _loginState; }
- set
- {
- if (value == loginState)
- return;
- switch (value)
- {
- case AS_LoginState.Idle:
- ToggleCanvasGroup(null);
- break;
- case AS_LoginState.LoginPrompt:
- if (accountManagementGUI)
- accountManagementGUI.enabled = false;
- ToggleCanvasGroup(loginParent);
- break;
- case AS_LoginState.Registering:
- ToggleCanvasGroup(registrationParent);
- break;
- case AS_LoginState.RecoverPassword:
- ToggleCanvasGroup(recoveryParent);
- break;
- case AS_LoginState.LoginSuccessful:
- ToggleCanvasGroup(null);
- loading.SetActive(true);
- // ToggleCanvasGroup(accountManagementParent);
- break;
- }
- guiMessage = "";
- _loginState = value;
- }
- }
- #region Button Accessors
- public void OnLoginRequested()
- {
- string username = usernameField.text;
- string password = passwordField.text;
- finalUsername = username;
- finalPassword = password;
- username.TryToLogin(password, LoginAttempted);
- }
- public void OnLogoutRequested()
- {
- loginState = AS_LoginState.LoginPrompt;
- }
- public void OnRegistrationRequested()
- {
- // When the form is downloaded, RegistrationFormDownloaded is called
- accountInfo.TryToDownloadRegistrationForm(RegistrationFormDownloaded);
- // Equivalent to:
- // StartCoroutine ( AS_Login.TryToDownloadRegistrationForm (accountInfo, RegistrationFormDownloaded) );
- guiMessage = "Loading..";
- }
- public void OnRegistrationCancelled()
- {
- loginState = AS_LoginState.LoginPrompt;
- }
- public void OnRegistrationSubmitted()
- {
- // Offline field check
- string errorMessage = "";
- if (!AS_Login.CheckFields (accountInfo, passwordConfirm.stringValue, emailConfirm.stringValue, ref errorMessage)) {
- guiMessage = errorMessage;
- return;
- }
- // Online check with the given database
- guiMessage = "Attempting to Register..";
- accountInfo.TryToRegister(RegistrationAttempted);
- // Equivalent to:
- // StartCoroutine ( AS_Login.TryToRegister( accountInfo, RegistrationAttempted ) ) ;
- }
- public void OnRecoveryRequested()
- {
- loginState = AS_LoginState.RecoverPassword;
- }
- public void OnRecoveryCancelled()
- {
- loginState = AS_LoginState.LoginPrompt;
- }
- public void OnRecoverySubmitted()
- {
- recoveryField.text.TryToRecoverPassword(PasswordRecoveryAttempted);
- // Equivalent to:
- // StartCoroutine(AS_Login.TryToRecoverPassword ( emailPasswordRecovery, PasswordRecoveryAttempted ) );
- guiMessage = "Processing your request..";
- }
- #endregion
- #region Callbacks
- // Called by the AttemptLogin coroutine when it's finished executing
- public void LoginAttempted(string callbackMessage)
- {
- // If our log in failed,
- if (callbackMessage.IsAnError())
- {
- string [] s = callbackMessage.Split( new string[] { "Error: " }, StringSplitOptions.RemoveEmptyEntries);
- guiMessage = s.Length >= 1 ? s[0] : callbackMessage;
- this.Log( LogType.Error, callbackMessage);
- return;
- }
- // Otherwise,
- int accountId = Convert.ToInt32(callbackMessage);
- OnSuccessfulLogin(accountId);
- }
- // Used by the AttemptDownloadRegistrationForm when it's finished executing
- void RegistrationFormDownloaded(string callbackMessage)
- {
- if (callbackMessage.IsAnError())
- {
- this.Log( LogType.Error, callbackMessage);
- guiMessage = callbackMessage;
- return;
- }
- PopulateRegistrationGroup ();
- loginState = AS_LoginState.Registering;
- // What you want to appear in the registration GUI
- guiMessage = "Please fill in the required fields.";
- }
- // Called by the AttemptRegistration coroutine when it's finished executing
- public void RegistrationAttempted(string callbackMessage)
- {
- guiMessage = callbackMessage;
- // If our registration failed,
- if (callbackMessage.IsAnError())
- {
- this.Log( LogType.Error, callbackMessage);
- return;
- }
- // Otherwise, success
- loginState = AS_LoginState.LoginPrompt;
- guiMessage = callbackMessage;
- }
- // Called by the AttemptPasswordRecovery coroutine when it's finished executing
- public void PasswordRecoveryAttempted(string callbackMessage)
- {
- guiMessage = callbackMessage;
- // If our registration failed,
- if (callbackMessage.IsAnError())
- {
- this.Log( LogType.Error, callbackMessage);
- return;
- }
- // Otherwise,
- loginState = AS_LoginState.LoginPrompt;
- guiMessage = callbackMessage;
- }
- #endregion
- #region Helpers
- /// <summary>
- /// Toggles on a specific Canvas Group - and the rest off
- /// </summary>
- private void ToggleCanvasGroup(CanvasGroup groupToToggle)
- {
- if (groupToToggle) {
- groupToToggle.gameObject.SetActive(true);
- guiMessageText = FindGUIMessage (groupToToggle.transform);
- }
- else
- guiMessageText = null;
- foreach (CanvasGroup cG in gameObject.GetComponentsInChildren<CanvasGroup>())
- {
- cG.alpha = cG == groupToToggle ? 1 : 0;
- cG.blocksRaycasts = cG == groupToToggle;
- }
- }
- /// <summary>
- /// Finds the GUI message in the hierarchy of the given transform
- /// </summary>
- private Text FindGUIMessage(Transform parent)
- {
- foreach (Text text in parent.GetComponentsInChildren<Text>()) {
- if (text.name.CompareTo(guiMessageTextName) != 0)
- continue;
- return text;
- }
- this.Log(LogType.Exception, "Couldn't find a Text component named '" +guiMessageTextName+"' as a child of '" + parent.name +"'. Make sure you add one.");
- return null;
- }
- IEnumerator ClearGUIText(Text text)
- {
- if (!text)
- yield break;
- yield return new WaitForSeconds(guiMessageTime);
- if (text)
- text.text = "";
- }
- AS_InputField CreateInputField(AS_MySQLField field)
- {
- GameObject temp = GameObject.Instantiate (inputFieldPrefab) as GameObject;
- // Place it in the container
- temp.transform.SetParent (registrationFieldsContainer.transform, false);
- // Grab its AS_InputField component (or add it)
- AS_InputField inputField = temp.GetComponent<AS_InputField> ();
- if (!inputField)
- inputField = temp.AddComponent<AS_InputField> ();
- inputField.Initialize (field);
- return inputField;
- }
- void PopulateRegistrationGroup()
- {
- // Clean up first
- for (int c = 0; c < registrationFieldsContainer.transform.childCount; c++)
- Destroy(registrationFieldsContainer.transform.GetChild (c).gameObject);
- // Registration Info has the fields the user should fill in
- foreach (AS_MySQLField field in accountInfo.fields)
- {
- // Id is an auto-increment unique identifier
- // and custom info is not specified during registration
- if (field.name.ToLower() == "id" | field.name.ToLower() == "custominfo" | field.name.ToLower() == "isactive" | field.name.ToLower() == "unbanned")
- continue;
- // For any other field, create an InputField prefab
- // Initialize it
- AS_InputField inputField = CreateInputField(field);
- // User requires one more space for PWD & Confirm to allign
- if (field.name.ToLower().Contains("username"))
- {
- AS_MySQLField dummyField = new AS_MySQLField();
- dummyField.name = "<b>Bold *</b>: Required Field";
- AS_InputField dummy = CreateInputField(dummyField);
- dummy.background.gameObject.SetActive(false);
- }
- /// Password / Email -> Require confirmation
- if (field.name.ToLower().Contains("password"))
- {
- passwordConfirm = new AS_MySQLField(field);
- passwordConfirm.name = "Confirm Password";
- AS_InputField passwordInputField = CreateInputField(passwordConfirm);
- }
- else if (field.name.ToLower().Contains("email"))
- {
- emailConfirm = new AS_MySQLField(field);
- emailConfirm.name = "Confirm Email";
- AS_InputField passwordInputField = CreateInputField(emailConfirm);
- }
- }
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement