Beispiel #1
0
        /// <summary>
        /// Wrapper for the same method in PlayerPrefs but works with encrypted player prefs.
        /// </summary>
        public static string GetString(string key, string defaultValue = "", bool?useSecurePrefs = null)
        {
            // if not using secure prefs, or we provide an override then just fall through to standard call.
            if ((!UseSecurePrefs && !useSecurePrefs.HasValue) || (useSecurePrefs.HasValue && useSecurePrefs.Value == false))
            {
                return(JSONPrefs.GetString(key, defaultValue));
            }

            // using secure prefs.
            var prefsEntryBytes = GetDecryptedPrefsEntry(key, ItemType.String);

            if (prefsEntryBytes != null)
            {
                return(Encoding.UTF8.GetString(prefsEntryBytes, 0, prefsEntryBytes.Length));
            }

            // if the prefs entry was not found and we are auto converting then try and get and replace any unencrypted value.
            if (AutoConvertUnsecurePrefs && JSONPrefs.HasKey(key))
            {
                var value = JSONPrefs.GetString(key, defaultValue);
                SetString(key, value);
                JSONPrefs.DeleteKey(key);
                return(value);
            }

            // nothing found so return the default value
            return(defaultValue);
        }
    public void SyncProgress()
    {
        if (!IsUserLoggedIn())
        {
            return;
        }

        DateTime now = UnbiasedTime.Instance.Now();

        // when implement void SaveState(), add it here to save state before sync
        CustomFreePrizeManager.Instance.SaveState();
        LevelController.Instance.SaveState();

        PreferencesFactory.SetString("LastProgressSyncDate", now.ToString(CultureInfo.InvariantCulture), false);
        PreferencesFactory.Save();

        string json = JSONPrefs.String();

        GSRequestData parsedJson = new GSRequestData(json);

        new LogEventRequest()
        .SetEventKey("PlayerProgress")
        .SetEventAttribute("data", parsedJson)
        .Send(((response) => {
            if (!response.HasErrors)
            {
                PlayerPrefs.SetString("LastProgressSyncDate", now.ToString(CultureInfo.InvariantCulture));
                PlayerPrefs.Save();
            }
        }));
    }
Beispiel #3
0
        /// <summary>
        /// Wrapper for the same method in PlayerPrefs but works with encrypted player prefs.
        /// </summary>
        public static void SetString(string key, string value, bool?useSecurePrefs = null)
        {
            // if not using secure prefs, or we provide an override then just fall through to standard call.
            if ((!UseSecurePrefs && !useSecurePrefs.HasValue) || (useSecurePrefs.HasValue && useSecurePrefs.Value == false))
            {
                JSONPrefs.SetString(key, value);
                return;
            }

            var valueBytes = Encoding.UTF8.GetBytes(value);

            SetEncryptedPrefsEntry(key, valueBytes, ItemType.String);
        }
Beispiel #4
0
        /// <summary>
        /// Wrapper for the same method in PlayerPrefs but works with encrypted player prefs.
        /// </summary>
        public static void DeleteKey(string key, bool?useSecurePrefs = null)
        {
            // if not using secure prefs, or we provide an override then just fall through to standard call.
            if ((!UseSecurePrefs && !useSecurePrefs.HasValue) || (useSecurePrefs.HasValue && useSecurePrefs.Value == false))
            {
                JSONPrefs.DeleteKey(key);
                return;
            }

            JSONPrefs.DeleteKey(EncryptKey(key));
            if (AutoConvertUnsecurePrefs)
            {
                JSONPrefs.DeleteKey(key);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Wrapper for the same method in PlayerPrefs but works with encrypted player prefs.
        /// </summary>
        public static bool HasKey(string key, bool?useSecurePrefs = null)
        {
            // if not using secure prefs, or we provide an override then just fall through to standard call.
            if ((!UseSecurePrefs && !useSecurePrefs.HasValue) || (useSecurePrefs.HasValue && useSecurePrefs.Value == false))
            {
                return(JSONPrefs.HasKey(key));
            }

            var hasEncryptedKey = JSONPrefs.HasKey(EncryptKey(key));

            if (hasEncryptedKey || !AutoConvertUnsecurePrefs)
            {
                return(hasEncryptedKey);
            }

            return(JSONPrefs.HasKey(key));
        }
Beispiel #6
0
        /// <summary>
        /// Get and decrypt an encrypted prefs entry into a byte array.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="type"></param>
        /// <returns>byte array, or null if there was an error.</returns>
        public static byte[] GetDecryptedPrefsEntry(string key, ItemType type)
        {
            var value = JSONPrefs.GetString(EncryptKey(key), null);

            return(DecryptValue(value, type));
        }
Beispiel #7
0
 /// <summary>
 /// Get the raw encrypted prefs value for the given key
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public static string GetRawEncryptedPrefsEntry(string key)
 {
     return(JSONPrefs.GetString(EncryptKey(key), null));
 }
Beispiel #8
0
 /// <summary>
 /// Set an encrypted prefs entry based upon the passed byte array and type
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <param name="type"></param>
 public static void SetEncryptedPrefsEntry(string key, byte[] value, ItemType type)
 {
     JSONPrefs.SetString(EncryptKey(key), EncryptValue(value, type));
 }
Beispiel #9
0
 /// <summary>
 /// Wrapper for the same method in PlayerPrefs but works with encrypted player prefs.
 /// </summary>
 public static void DeleteAll()
 {
     JSONPrefs.DeleteAll();
 }
Beispiel #10
0
 /// <summary>
 /// Wrapper for the same method in PlayerPrefs but works with encrypted player prefs.
 /// </summary>
 public static void Save()
 {
     JSONPrefs.Save();
 }
    public void SyncRestore()
    {
        if (!IsUserLoggedIn())
        {
            return;
        }

        new LogEventRequest()
        .SetEventKey("LoadPlayerProgress")
        .Send((response) => {
            if (!response.HasErrors && response.ScriptData != null)
            {
                GSData data = response.ScriptData.GetGSData("data");

                if (data == null)
                {
                    return;
                }

                DateTime now = UnbiasedTime.Instance.Now();
                string json  = data.JSON;

                // date from remote json
                DateTime syncDate = now;

                if (data.ContainsKey("LastProgressSyncDate"))
                {
                    syncDate = DateTime.Parse(data.GetString("LastProgressSyncDate"));
                }

                // date in device when was last sync
                DateTime lastUpdateDate = DateTime.Parse(PlayerPrefs.GetString("LastProgressSyncDate", now.ToString(CultureInfo.InvariantCulture)));
                MyDebug.Log("Should update prefs; lastUpdateDate: " + lastUpdateDate + "; syncDate: " + syncDate);

                if (syncDate.CompareTo(lastUpdateDate) != 0)
                {
                    MyDebug.Log("Diff hash, update prefs");

                    JSONPrefs.Replace(json);

                    // updating device date to sync date, because on next start shouldn't update again
                    PlayerPrefs.SetString("LastProgressSyncDate", syncDate.ToString(CultureInfo.InvariantCulture));
                    PlayerPrefs.Save();

                    CustomGameManager manager = CustomGameManager.Instance as CustomGameManager;
                    manager.ResetGame();

                    // reload data from sync
                    LevelController.Instance.Reload();

                    //

                    GSRequestData logData = new GSRequestData();
                    logData.AddString("key", "ProgressSync");
                    logData.AddString("message", "User synched progress from server");

                    GSData _d = new GSData(new Dictionary <string, object>()
                    {
                        { "Date in JSON", syncDate },
                        { "Date in device", lastUpdateDate }
                    });

                    logData.AddObject("data", _d);

                    Log(logData);
                }

                GetAwards();                 // safe place to get awards
            }
        });
    }