Example #1
0
        /// <summary>
        /// Saves an object to a root using the specified settings, overwriting if it exists
        /// </summary>
        /// <typeparam name="T">The type of object to save</typeparam>
        /// <param name="root">The root this object will be saved under</param>
        /// <param name="value">The object to save</param>
        /// <param name="settings">Settings</param>
        public static void Save <T>(string root, T value, QuickSaveSettings settings)
        {
            string jsonToSave;

            try
            {
                jsonToSave = JsonSerialiser.Serialise(TypeHelper.ReplaceIfUnityType(value));
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Json serialisation failed", e);
            }

            string encryptedJson;

            try
            {
                encryptedJson = Cryptography.Encrypt(jsonToSave, settings.SecurityMode, settings.Password);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Encryption failed", e);
            }

            if (!FileAccess.SaveString(root, false, encryptedJson))
            {
                throw new QuickSaveException("Failed to write to file");
            }
        }
Example #2
0
        /// <summary>
        /// Loads the contents of the root into the specified object using the specified settings
        /// </summary>
        /// <typeparam name="T">The type of object to load</typeparam>
        /// <param name="root">The root this object was saved under</param>
        /// <param name="settings">Settings</param>
        /// <returns>The object that was loaded</returns>
        public static T Load <T>(string root, QuickSaveSettings settings)
        {
            string fileJson = FileAccess.LoadString(root, false);

            if (string.IsNullOrEmpty(fileJson))
            {
                throw new QuickSaveException("File either does not exist or is empty");
            }

            string decryptedJson;

            try
            {
                decryptedJson = Cryptography.Decrypt(fileJson, settings.SecurityMode, settings.Password);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Decryption failed", e);
            }

            try
            {
                return(JsonSerialiser.Deserialise <T>(decryptedJson));
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Failed to deserialise json", e);
            }
        }
Example #3
0
        /// <summary>
        /// Loads the contents of the specified file into a string using the specified settings
        /// </summary>
        /// <param name="filename">The file to load from</param>
        /// <param name="settings">Settings</param>
        /// <returns>The contents of the file as a string</returns>
        public static string LoadString(string filename, QuickSaveSettings settings)
        {
            var content = FileAccess.LoadString(filename, true);

            if (content == null)
            {
                throw new QuickSaveException("Failed to load file");
            }

            // Gzip parses base64 anyway so no need to do it twice
            if (settings.CompressionMode != CompressionMode.Gzip || settings.SecurityMode != SecurityMode.Base64)
            {
                try
                {
                    content = Cryptography.Decrypt(content, settings.SecurityMode, settings.Password);
                }
                catch (Exception e)
                {
                    throw new QuickSaveException("Decryption failed", e);
                }
            }

            try
            {
                content = Compression.Decompress(content, settings.CompressionMode);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Decompression failed", e);
            }

            return(content);
        }
Example #4
0
        /// <summary>
        /// Creates a QuickSaveWriter on the specified root using the specified settings
        /// </summary>
        /// <param name="root">The root to write to</param>
        /// <param name="settings">Settings</param>
        /// <returns>A QuickSaveWriter instance</returns>
        public static QuickSaveWriter Create(string root, QuickSaveSettings settings)
        {
            QuickSaveWriter quickSaveWriter = new QuickSaveWriter(root, settings);

            quickSaveWriter.Load(true);
            return(quickSaveWriter);
        }
Example #5
0
        /// <summary>
        /// Saves a string directly to the specified file using the specified settings, overwriting if it already exists
        /// </summary>
        /// <param name="filename">The file to save to</param>
        /// <param name="content">The string to save</param>
        /// <param name="settings">Settings</param>
        public static void SaveString(string filename, string content, QuickSaveSettings settings)
        {
            string contentToWrite;

            try
            {
                contentToWrite = Compression.Compress(content, settings.CompressionMode);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Compression failed", e);
            }

            // Gzip outputs base64 anyway so no need to do it twice
            if (settings.CompressionMode != CompressionMode.Gzip || settings.SecurityMode != SecurityMode.Base64)
            {
                try
                {
                    contentToWrite = Cryptography.Encrypt(contentToWrite, settings.SecurityMode, settings.Password);
                }
                catch (Exception e)
                {
                    throw new QuickSaveException("Encryption failed", e);
                }
            }

            if (!FileAccess.SaveString(filename, true, contentToWrite))
            {
                throw new QuickSaveException("Failed to write to file");
            }
        }
Example #6
0
        /// <summary>
        /// Creates a QuickSaveReader on the specified root using the specified settings
        /// </summary>
        /// <param name="root">The root to read from</param>
        /// <param name="settings">Settings</param>
        /// <returns>A QuickSaveReader instance</returns>
        public static QuickSaveReader Create(string root, QuickSaveSettings settings)
        {
            QuickSaveReader quickSaveReader = new QuickSaveReader(root, settings);

            quickSaveReader.Load(false);
            return(quickSaveReader);
        }
Example #7
0
        /// <summary>
        /// Attempts to load an object from a root under the specified key using the specified settings
        /// </summary>
        /// <typeparam name="T">The type of object to load</typeparam>
        /// <param name="root">The root this object was saved under</param>
        /// <param name="key">The key this object was saved under</param>
        /// <param name="settings">Settings</param>
        /// <param name="result">The object that was loaded</param>
        /// <returns>Was the load successful</returns>
        public static bool TryLoad <T>(string root, string key, QuickSaveSettings settings, out T result)
        {
            result = default(T);

            try
            {
                string fileJson = FileAccess.LoadString(root, false);

                if (string.IsNullOrEmpty(fileJson))
                {
                    return(false);
                }

                string decryptedJson = Cryptography.Decrypt(fileJson, settings.SecurityMode, settings.Password);

                Dictionary <string, object> items = JsonSerialiser.Deserialise <Dictionary <string, object> >(decryptedJson) ?? new Dictionary <string, object>();

                if (!items.ContainsKey(key))
                {
                    return(false);
                }

                string propertyJson = JsonSerialiser.Serialise(items[key]);

                result = JsonSerialiser.Deserialise <T>(propertyJson);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #8
0
        /// <summary>
        /// Saves a string directly to the specified file using the specified settings, overwriting if it already exists
        /// </summary>
        /// <param name="filename">The file to save to</param>
        /// <param name="content">The string to save</param>
        /// <param name="settings">Settings</param>
        public static void SaveString(string filename, string content, QuickSaveSettings settings)
        {
            string encryptedText;

            try
            {
                encryptedText = Cryptography.Encrypt(content, settings.SecurityMode, settings.Password);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Encryption failed", e);
            }

            if (!FileAccess.SaveString(filename, true, encryptedText))
            {
                throw new QuickSaveException("Failed to write to file");
            }
        }
Example #9
0
        /// <summary>
        /// Attempts to save an object to a root under the specified key using the specified settings
        /// </summary>
        /// <typeparam name="T">The type of object to save</typeparam>
        /// <param name="root">The root this object will be saved under</param>
        /// <param name="key">The key this object will be saved under</param>
        /// <param name="value">The object to save</param>
        /// <param name="settings">Settings</param>
        /// <returns>Was the save successful</returns>
        public static bool TrySave <T>(string root, string key, T value, QuickSaveSettings settings)
        {
            try
            {
                string fileJson = FileAccess.LoadString(root, false);

                Dictionary <string, object> items = null;

                if (string.IsNullOrEmpty(fileJson))
                {
                    items = new Dictionary <string, object>();
                }
                else
                {
                    string decryptedJson = Cryptography.Decrypt(fileJson, settings.SecurityMode, settings.Password);

                    items = JsonSerialiser.Deserialise <Dictionary <string, object> >(decryptedJson) ?? new Dictionary <string, object>();

                    if (items.ContainsKey(key))
                    {
                        items.Remove(key);
                    }
                }

                items.Add(key, TypeHelper.ReplaceIfUnityType(value));

                string jsonToSave = JsonSerialiser.Serialise(items);

                string encryptedJson = Cryptography.Encrypt(jsonToSave, settings.SecurityMode, settings.Password);

                FileAccess.SaveString(root, false, encryptedJson);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #10
0
        /// <summary>
        /// Loads the contents of the specified file into a string using the specified settings
        /// </summary>
        /// <param name="filename">The file to load from</param>
        /// <param name="settings">Settings</param>
        /// <returns>The contents of the file as a string</returns>
        public static string LoadString(string filename, QuickSaveSettings settings)
        {
            string content = FileAccess.LoadString(filename, true);

            if (content == null)
            {
                throw new QuickSaveException("Failed to load file");
            }

            string decryptedText;

            try
            {
                decryptedText = Cryptography.Decrypt(content, settings.SecurityMode, settings.Password);
            }
            catch (Exception e)
            {
                throw new QuickSaveException("Decryption failed", e);
            }

            return(decryptedText);
        }
Example #11
0
 private QuickSaveWriter(string root, QuickSaveSettings settings)
 {
     _root     = root;
     _settings = settings;
 }