Beispiel #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);
     }
 }
Beispiel #2
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);
     }
 }