示例#1
0
        /// <summary>
        /// Saves data using the identifier.
        /// </summary>
        /// <param name="filename">Identifier of the file containing the data. Can route to a folder relative to the Application.persistentDataPath.</param>
        /// <param name="objectToSave">Object to save.</param>
        /// <param name="encryptionPassword">Encryption Password.</param>
        /// <param name="encoding">Encoding.</param>
        /// <typeparam name="T">The objectToSave's type.</typeparam>
        public static void Save <T>(T objectToSave, string filename, string encryptionPassword = null, Encoding encoding = null)
        {
            // Setup
            SD_JsonSerializer serializer = new SD_JsonSerializer();
            SD_Encoder        encoder    = new SD_Encoder();

            encoding ??= Encoding.UTF8;
            objectToSave ??= default(T);
            if (string.IsNullOrEmpty(filename))
            {
                throw new System.ArgumentNullException(nameof(filename));
            }
            string filePath = GetFilePath(filename);

            // Build IO stream
            Stream stream;

            #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
            #if UNITY_WSA || UNITY_WINRT
            UnityEngine.Windows.Directory.CreateDirectory(filePath);
            #else
            Directory.CreateDirectory(Path.GetDirectoryName(filePath) ?? string.Empty);
            #endif
            #endif
            if (!encryptionPassword.IsNullEmptyOrWhiteSpace())
            {
                stream = new MemoryStream();
            }
            else
            {
            #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (Utils.IsIOSupported())
                {
            #if UNITY_WSA || UNITY_WINRT
                    stream = new MemoryStream();
            #else
                    stream = File.Create(filePath);
            #endif
                }
                else
                {
                    stream = new MemoryStream();
                }
            #else
                stream = new MemoryStream();
            #endif
            }

            // Serialize object
            serializer.Serialize(objectToSave, stream, encoding);
            if (!encryptionPassword.IsNullEmptyOrWhiteSpace())
            {
                string data    = System.Convert.ToBase64String(((MemoryStream)stream).ToArray());
                string encoded = encoder.Encode(data, encryptionPassword);
            #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (Utils.IsIOSupported())
                {
            #if UNITY_WSA || UNITY_WINRT
                    UnityEngine.Windows.File.WriteAllBytes(filePath, encoding.GetBytes(encoded));
            #else
                    File.WriteAllText(filePath, encoded, encoding);
            #endif
                }
                else
                {
                    PlayerPrefs.SetString(filePath, encoded);
                    PlayerPrefs.Save();
                }
            #else
                PlayerPrefs.SetString(filePath, encoded);
                PlayerPrefs.Save();
            #endif
            }
            else if (!Utils.IsIOSupported())
            {
                string data = encoding.GetString(((MemoryStream)stream).ToArray());
                PlayerPrefs.SetString(filePath, data);
                PlayerPrefs.Save();
            }
            stream.Dispose();
        }
示例#2
0
        // ReSharper disable Unity.PerformanceAnalysis
        /// <summary>
        /// Loads data using identifier.
        /// </summary>
        /// <param name="filename">Identifier of the file containing the data. Can route to a folder relative to the Application.persistentDataPath.</param>
        /// <param name="defaultValue">Default Value. Used in case the saved data is not found.</param>
        /// <param name="encryptionPassword">Encryption Password (set it to the same password you used to save it).</param>
        /// <param name="encoding">Encoding.</param>
        /// <param name="supressFileNotFoundWarning">Should the warning be disabled so if the desired data is not found no warning is created? Set it to <c>True</c> for hiding the warning.</param>
        /// <typeparam name="T">The objectToSave's type.</typeparam>
        public static T Load <T>(string filename, T defaultValue, string encryptionPassword = null, Encoding encoding = null, bool supressFileNotFoundWarning = false)
        {
            // Setup
            SD_JsonSerializer serializer = new SD_JsonSerializer();
            SD_Encoder        encoder    = new SD_Encoder();

            encoding ??= Encoding.UTF8;
            defaultValue ??= default(T);
            if (string.IsNullOrEmpty(filename))
            {
                throw new System.ArgumentNullException(nameof(filename));
            }
            string filePath = GetFilePath(filename);
            T      result   = defaultValue;

            #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
            if (!Exists(filename))
            #else
            if (!Exists(filePath, path))
            #endif
            {
                if (!supressFileNotFoundWarning)
                {
                    Debug.LogWarning(
                        $"The file '{GetFilePath(filename)}' was not found.    You can use the parameter 'supressFileNotFoundWarning' to disable the warning or use 'Exists()' to check if such file exists or not before trying to load them.\n" +
                        "Returning the default(T) instance."
                        );
                }
                return(result);
            }
            Stream stream;
            if (!encryptionPassword.IsNullEmptyOrWhiteSpace())
            {
                string data;
            #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (Utils.IsIOSupported())
                {
            #if UNITY_WSA || UNITY_WINRT
                    data = encoding.GetString(UnityEngine.Windows.File.ReadAllBytes(filePath));
            #else
                    data = File.ReadAllText(filePath, encoding);
            #endif
                }
                else
                {
                    data = PlayerPrefs.GetString(filePath);
                }
            #else
                data = PlayerPrefs.GetString(filePath);
            #endif
                string decoded = encoder.Decode(data, encryptionPassword);
                stream = new MemoryStream(System.Convert.FromBase64String(decoded), true);
            }
            else
            {
            #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL
                if (Utils.IsIOSupported())
                {
            #if UNITY_WSA || UNITY_WINRT
                    stream = new MemoryStream(UnityEngine.Windows.File.ReadAllBytes(filePath));
            #else
                    stream = File.OpenRead(filePath);
            #endif
                }
                else
                {
                    string data = PlayerPrefs.GetString(filePath);
                    stream = new MemoryStream(encoding.GetBytes(data));
                }
            #else
                string data = PlayerPrefs.GetString(filePath);
                stream = new MemoryStream(encoding.GetBytes(data));
            #endif
            }
            result = serializer.Deserialize <T>(stream, encoding);
            stream.Dispose();
            if (result == null)
            {
                result = defaultValue;
            }
            return(result);
        }