Exemplo n.º 1
0
 /// <summary>
 /// Save a data to local storage as protobuf bytes
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="val"></param>
 /// <param name="encryptKey"></param>
 /// <returns>Saved value</returns>
 public static byte[] SaveAsProtobufBytes <T>(string dataName, T val, string encryptKey = null) where T : class
 {
     if (String.IsNullOrEmpty(encryptKey))
     {
         encryptKey = defaultEncryptKey;
     }
     if (encryptKey.Length != 16)
     {
         var ex = new Exception("encryptKey needs to be 16 characters!");
         Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
         return(null);
     }
     try
     {
         byte[] byteData = StringifyHelper.ProtoSerialize(val);
         var    result   = CryptoHelper.AesEncrypt(byteData, encryptKey);
         PlayerPrefs.SetString(dataName, Convert.ToBase64String(result));
         AddJSaverKeys(dataName);
         return(result);
     }
     catch (Exception ex)
     {
         Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
         return(null);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Save a data to local storage as JSON
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="val"></param>
 /// <param name="encryptKey"></param>
 /// <returns>Saved value</returns>
 public static string SaveAsJSON <T>(string dataName, T val, string encryptKey = null)
 {
     if (String.IsNullOrEmpty(encryptKey))
     {
         encryptKey = defaultEncryptKey;
     }
     if (encryptKey.Length != 16)
     {
         var ex = new Exception("encryptKey needs to be 16 characters!");
         Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
         return(null);
     }
     try
     {
         string strData = StringifyHelper.JSONSerliaze(val);
         var    result  = CryptoHelper.EncryptStr(strData, encryptKey);
         PlayerPrefs.SetString(dataName, result);
         AddJSaverKeys(dataName);
         return(result);
     }
     catch (Exception ex)
     {
         Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
         return(null);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Get object from local storage from protobuf
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <param name="encryptKey"></param>
        /// <returns>Saved value</returns>
        public static T GetObjectFromProtobuf <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
            {
                byte[] bytes = CryptoHelper.AesDecrypt(Convert.FromBase64String(result), encryptKey);
                return(StringifyHelper.ProtoDeSerialize <T>(bytes));
            }
            catch (Exception ex)
            {
                Log.PrintError($"[JSaver] 错误:{ex.Message}, {ex.Data["StackTrace"]}");
                return(default(T));
            }
        }
Exemplo n.º 4
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(ex);
                return(null);
            }
            if (String.IsNullOrEmpty(encryptKey))
            {
                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));
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// Save a data to local storage as JSON
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="val"></param>
 /// <param name="encryptKey"></param>
 /// <returns>Saved value</returns>
 public static string SaveAsJSON <T>(string dataName, T val, string encryptKey = null)
 {
     if (String.IsNullOrEmpty(encryptKey))
     {
         encryptKey = Init.Instance.Key;
     }
     if (encryptKey.Length != 16)
     {
         var ex = new Exception("encryptKey needs to be 16 characters!");
         Log.PrintError(ex);
         return(null);
     }
     try
     {
         string strData = StringifyHelper.JSONSerliaze(val);
         var    result  = CryptoHelper.EncryptStr(strData, encryptKey);
         PlayerPrefs.SetString(dataName, result);
         return(result);
     }
     catch (Exception ex)
     {
         Log.PrintError(ex);
         return(null);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// Save a data to local storage as protobuf bytes
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="val"></param>
 /// <param name="encryptKey"></param>
 /// <returns>Saved value</returns>
 public static byte[] SaveAsProtobufBytes <T>(string dataName, T val, string encryptKey = null) where T : class
 {
     if (encryptKey == null)
     {
         encryptKey = Init.Instance.Key;
     }
     if (encryptKey.Length != 16)
     {
         var ex = new Exception("encryptKey needs to be 16 characters!");
         Log.PrintError(ex);
         return(null);
     }
     try
     {
         byte[] byteData = StringifyHelper.ProtoSerialize(val);
         var    result   = CryptoHelper.AesEncrypt(byteData, encryptKey);
         PlayerPrefs.SetString(dataName, Convert.ToBase64String(result));
         return(result);
     }
     catch (Exception ex)
     {
         Log.PrintError(ex);
         return(null);
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Get object from local storage from protobuf
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="val"></param>
        /// <param name="encryptKey"></param>
        /// <returns>Saved value</returns>
        public static T GetObjectFromProtobuf <T>(string dataName, string encryptKey = null) where T : class
        {
            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
            {
                byte[] bytes = CryptoHelper.AesDecrypt(Convert.FromBase64String(result), encryptKey);
                return(StringifyHelper.ProtoDeSerialize <T>(bytes));
            }
            catch (Exception ex)
            {
                Log.PrintError(ex);
                return(default(T));
            }
        }
Exemplo n.º 8
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));
            }
        }