Ejemplo n.º 1
0
        /// <summary>
        /// Get object from local storage from JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <param name="encryptKey"></param>
        /// <returns>Saved value</returns>
        public static T GetObjectFromJSON <T>(string dataName, string encryptKey = null) where T : class
        {
            if (!HasData(dataName))
            {
                var ex = new Exception($"<{dataName}> does not exist\n" +
                                       $"<{dataName}>不存在");
                Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
                return(null);
            }
            if (String.IsNullOrEmpty(encryptKey))
            {
                encryptKey = defaultEncryptKey;
            }
            if (encryptKey.Length != 16)
            {
                throw new Exception("encryptKey needs to be 16 characters!");
            }
            var result = PlayerPrefs.GetString(dataName);

            try
            {
                result = CryptoHelper.DecryptStr(result, encryptKey);
                return(StringifyHelper.JSONDeSerliaze <T>(result));
            }
            catch (Exception ex)
            {
                Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
                return(default(T));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Get object from local storage from JSON
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <param name="encryptKey"></param>
        /// <returns>Saved value</returns>
        public static T GetObjectFromJSON <T>(string dataName, string encryptKey = null)
        {
            if (encryptKey == null)
            {
                encryptKey = Init.Instance.Key;
            }
            if (encryptKey.Length != 16)
            {
                throw new Exception("encryptKey needs to be 16 characters!");
            }
            var result = PlayerPrefs.GetString(dataName);

            try
            {
                result = CryptoHelper.DecryptStr(result, encryptKey);
                return(StringifyHelper.JSONDeSerliaze <T>(result));
            }
            catch (Exception ex)
            {
                Log.PrintError(ex);
                return(default(T));
            }
        }