Пример #1
0
        public static string SerializeString(System.Object obj, bool useEncryption = false)
        {
            string serializedString;

            if (obj.GetType() == typeof(string))
            {
                serializedString = (string)obj;
            }
            else
            {
                serializedString = JsonUtility.ToJson(obj);
            }

            if (useEncryption)
            {
                serializedString = TBDataEncryptor.Encrypt(serializedString, _encryptionPassword);
            }

            return(serializedString);
        }
Пример #2
0
        /// <summary>
        /// Deserializes a string into the given object type. Does NOT handle file reading. Use DeserializeFromFile or Async.Load
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serializedString"></param>
        /// <param name="fileName"></param>
        /// <param name="useEncryption"></param>
        /// <returns></returns>
        public static T DeserializeString <T>(string serializedString, string fileName, bool useEncryption = false)
        {
            if (!string.IsNullOrEmpty(serializedString))
            {
                T convertedObject;

                if (useEncryption)
                {
                    try
                    {
                        string decryptedString;
                        decryptedString = TBDataEncryptor.Decrypt(serializedString, _encryptionPassword);
                        convertedObject = (T)JsonUtility.FromJson(decryptedString, typeof(T));
                        return(convertedObject);
                    }
                    catch
                    {
                        Debug.LogError("Attempted to deserialize an encrypted string, but was not able to cast to the requested type T. Will now attempt to deserialize without decrypting.");
                    }
                }

                // If the data isn't null or empty, return it as the requested object type.
                try
                {
                    convertedObject = (T)JsonUtility.FromJson(serializedString, typeof(T));
                }
                catch
                {
                    Debug.LogError("The serialized string did not match the expected format, and will be replaced.");
                    Debug.LogError("The serialized string was: " + serializedString);
                    return(GetNullOrEmptyOfType <T>());
                }
                return(convertedObject);
            }
            else
            {
                // If the serilaized string is null, then either there was an error or the file didn't exist. Data will be created if possible.
                Debug.LogWarning("TBData attempted to deserialize file " + fileName + " but the deserialized version of the data was null. This expected if the file didn't exist.");
                return(GetNullOrEmptyOfType <T>());
            }
        }
Пример #3
0
 public static string EncryptWithPassword(string serializedString)
 {
     return(TBDataEncryptor.Encrypt(serializedString, _encryptionPassword));
 }