Example #1
0
 /// <summary>
 /// Save the specified value using the identifier.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="value">Value.</param>
 /// <param name="settings">Settings.</param>
 public static void Save(string identifier, object value, SaveGameSettings settings)
 {
     if (string.IsNullOrEmpty(identifier))
     {
         throw new ArgumentNullException("identifier");
     }
     if (value == null)
     {
         Debug.LogWarning("SaveGamePro: Can't Save a null value");
         return;
     }
     if (!settings.Formatter.IsTypeSupported(value.GetType()))
     {
         Debug.LogWarningFormat("SaveGamePro: The serialization of type {0} isn't supported.", value.GetType());
         return;
     }
     settings.Identifier = identifier;
     settings.Storage.OnSave(settings);
     settings.Formatter.Serialize(settings.Storage.GetWriteStream(settings), value, settings);
     settings.Storage.OnSaved(settings);
     if (OnSaved != null)
     {
         OnSaved(identifier, value, settings);
     }
 }
Example #2
0
        /// <summary>
        /// Saves the image.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="texture">Texture.</param>
        /// <param name="settings">Settings.</param>
        public static void SaveImage(string identifier, Texture2D texture, SaveGameSettings settings)
        {
            settings.Identifier = identifier;
            string path = SaveGameFileStorage.GetAbsolutePath(settings.Identifier, settings.BasePath);

            File.WriteAllBytes(path, texture.EncodeToPNG());
        }
Example #3
0
 /// <summary>
 /// Load the specified identifier, if not exists, returns the default value.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="defaultValue">Default value.</param>
 /// <param name="settings">Settings.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static T Load <T>(string identifier, T defaultValue, SaveGameSettings settings)
 {
     if (defaultValue == null)
     {
         defaultValue = default(T);
     }
     return((T)Load(identifier, typeof(T), defaultValue, settings));
 }
Example #4
0
 /// <summary>
 /// Clear all user data.
 /// </summary>
 /// <param name="settings">Settings.</param>
 public static void Clear(SaveGameSettings settings)
 {
     settings.Storage.Clear(settings);
     if (OnCleared != null)
     {
         OnCleared(settings);
     }
 }
Example #5
0
 /// <summary>
 /// Copy the specified identifier to identifier.
 /// </summary>
 /// <param name="fromIdentifier">From identifier.</param>
 /// <param name="toIdentifier">To identifier.</param>
 /// <param name="settings">Settings.</param>
 public static void Copy(string fromIdentifier, string toIdentifier, SaveGameSettings settings)
 {
     settings.Storage.Copy(fromIdentifier, toIdentifier, settings);
     if (OnCopied != null)
     {
         OnCopied(fromIdentifier, toIdentifier, settings);
     }
 }
Example #6
0
 /// <summary>
 /// Move the specified identifier to identifier.
 /// </summary>
 /// <param name="fromIdentifier">From identifier.</param>
 /// <param name="toIdentifier">To identifier.</param>
 /// <param name="settings">Settings.</param>
 public static void Move(string fromIdentifier, string toIdentifier, SaveGameSettings settings)
 {
     settings.Storage.Move(fromIdentifier, toIdentifier, settings);
     if (OnMoved != null)
     {
         OnMoved(fromIdentifier, toIdentifier, settings);
     }
 }
Example #7
0
 /// <summary>
 /// Delete the specified identifier.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="settings">Settings.</param>
 public static void Delete(string identifier, SaveGameSettings settings)
 {
     settings.Identifier = identifier;
     settings.Storage.Delete(settings);
     if (OnDeleted != null)
     {
         OnDeleted(identifier, settings);
     }
 }
Example #8
0
        /// <summary>
        /// Loads the image Asynchronously.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="settings">Settings.</param>
        public static Task <Texture2D> LoadImageAsync(string identifier, SaveGameSettings settings)
        {
            Task <Texture2D> task = new Task <Texture2D>(() =>
            {
                return(SaveGame.LoadImage(identifier, settings));
            });

            task.Start();
            return(task);
        }
Example #9
0
        /// <summary>
        /// Saves the image Asynchronously.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="texture">Texture.</param>
        /// <param name="settings">Settings.</param>
        public static Task SaveImageAsync(string identifier, Texture2D texture, SaveGameSettings settings)
        {
            Task task = new Task(() =>
            {
                SaveGame.SaveImage(identifier, texture, settings);
            });

            task.Start();
            return(task);
        }
Example #10
0
        /// <summary>
        /// Loads the data into the value Asynchronously.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="value">Value.</param>
        /// <param name="settings">Settings.</param>
        public static Task LoadIntoAsync(string identifier, object value, SaveGameSettings settings)
        {
            Task task = new Task(() =>
            {
                SaveGame.LoadInto(identifier, value, settings);
            });

            task.Start();
            return(task);
        }
Example #11
0
        /// <summary>
        /// Loads the image.
        /// </summary>
        /// <returns>The image.</returns>
        /// <param name="identifier">Identifier.</param>
        /// <param name="settings">Settings.</param>
        public static Texture2D LoadImage(string identifier, SaveGameSettings settings)
        {
            settings.Identifier = identifier;
            string path = SaveGameFileStorage.GetAbsolutePath(settings.Identifier, settings.BasePath);

            byte[]    data    = File.ReadAllBytes(path);
            Texture2D texture = new Texture2D(0, 0);

            texture.LoadImage(data);
            return(texture);
        }
Example #12
0
        /// <summary>
        /// Load the specified identifier, if not exists, returns the default value Asynchronously.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="settings">Settings.</param>
        public static Task <T> LoadAsync <T>(string identifier, T defaultValue, SaveGameSettings settings)
        {
            if (defaultValue == null)
            {
                defaultValue = default(T);
            }
            Task <T> task = new Task <T>(() =>
            {
                return((T)SaveGame.Load(identifier, typeof(T), defaultValue, settings));
            });

            task.Start();
            return(task);
        }
Example #13
0
        /// <summary>
        /// Save the specified value using the identifier.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="value">Value.</param>
        /// <param name="settings">Settings.</param>
        public static void Save(string identifier, object value, SaveGameSettings settings)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (value == null)
            {
                Debug.LogWarning("SaveGamePro: Can't Save a null value");
                return;
            }
            if (!settings.Formatter.IsTypeSupported(value.GetType()))
            {
                Debug.LogWarningFormat("SaveGamePro: The serialization of type {0} isn't supported.", value.GetType());
                return;
            }
            settings.Identifier = identifier;
            settings.Storage.OnSave(settings);
            Stream stream = settings.Storage.GetWriteStream(settings);

            if (settings.Encrypt)
            {
                byte[] serializedData;
                using (MemoryStream mStream = new MemoryStream())
                {
                    settings.Formatter.Serialize(mStream, value, settings);
                    serializedData = mStream.ToArray();
                }
                CryptoStream encStream = new CryptoStream(stream, settings.Encryptor, CryptoStreamMode.Write);
                encStream.Write(serializedData, 0, serializedData.Length);
                encStream.FlushFinalBlock();
                stream = encStream;
            }
            else
            {
                settings.Formatter.Serialize(stream, value, settings);
            }
            settings.Storage.OnSaved(settings);
            stream.Dispose();
            if (OnSaved != null)
            {
                OnSaved(identifier, value, settings);
            }
        }
Example #14
0
        /// <summary>
        /// Load the specified identifier, if not exists, returns the default value.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="type">Type.</param>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="settings">Settings.</param>
        public static object Load(string identifier, Type type, object defaultValue, SaveGameSettings settings)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (defaultValue == null)
            {
                defaultValue = type.GetDefault();
            }
            settings.Identifier = identifier;
            if (!Exists(settings.Identifier, settings))
            {
                if (defaultValue == null)
                {
                    Debug.LogWarning("SaveGamePro: The specified identifier does not exists, please make sure the identifeir is exist before loading. The Default value is not specified, it might make exceptions and errors.");
                }
                else
                {
                    Debug.LogWarning("SaveGamePro: The specified identifier does not exists, please make sure the identifeir is exist before loading. Returning default value.");
                }
                return(defaultValue);
            }
            settings.Storage.OnLoad(settings);
            object result = settings.Formatter.Deserialize(settings.Storage.GetReadStream(settings), type, settings);

            settings.Storage.OnLoaded(settings);
            if (result == null)
            {
                result = defaultValue;
            }
            if (OnLoaded != null)
            {
                OnLoaded(identifier, result, type, defaultValue, settings);
            }
            return(result);
        }
Example #15
0
 /// <summary>
 /// Loads the data into the value.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="value">Value.</param>
 /// <param name="settings">Settings.</param>
 public static void LoadInto(string identifier, object value, SaveGameSettings settings)
 {
     if (string.IsNullOrEmpty(identifier))
     {
         throw new ArgumentNullException("identifier");
     }
     if (value == null)
     {
         throw new ArgumentNullException("value");
     }
     settings.Identifier = identifier;
     if (!Exists(settings.Identifier, settings))
     {
         Debug.LogWarning("SaveGamePro: The specified identifier does not exists.");
         return;
     }
     settings.Storage.OnLoad(settings);
     settings.Formatter.DeserializeInto(settings.Storage.GetReadStream(settings), value, settings);
     settings.Storage.OnLoaded(settings);
     if (OnLoadedInto != null)
     {
         OnLoadedInto(identifier, value, settings);
     }
 }
Example #16
0
 /// <summary>
 /// Gets the directories.
 /// This method only works on platforms that support file storage.
 /// </summary>
 /// <returns>The directories.</returns>
 /// <param name="identifier">Identifier.</param>
 /// <param name="settings">Settings.</param>
 public static DirectoryInfo[] GetDirectories(string identifier, SaveGameSettings settings)
 {
     settings.Identifier = identifier;
     return(settings.Storage.GetDirectories(settings));
 }
Example #17
0
 /// <summary>
 /// Gets the files.
 /// This method only works on platforms that support file storage.
 /// </summary>
 /// <returns>The files.</returns>
 /// <param name="identifier">Identifier.</param>
 /// <param name="settings">Settings.</param>
 public static FileInfo[] GetFiles(string identifier, SaveGameSettings settings)
 {
     settings.Identifier = identifier;
     return(settings.Storage.GetFiles(settings));
 }
Example #18
0
 /// <summary>
 /// Checks if the specified identifier exists or not.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="settings">Settings.</param>
 public static bool Exists(string identifier, SaveGameSettings settings)
 {
     settings.Identifier = identifier;
     return(settings.Storage.Exists(settings));
 }
Example #19
0
        /// <summary>
        /// Load the specified identifier, if not exists, returns the default value Asynchronously.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="type">Type.</param>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="settings">Settings.</param>
        public static Task <object> LoadAsync(string identifier, Type type, object defaultValue, SaveGameSettings settings)
        {
            Task <object> task = new Task <object>(() =>
            {
                return(SaveGame.Load(identifier, type, defaultValue, settings));
            });

            task.Start();
            return(task);
        }
Example #20
0
        /// <summary>
        /// Load the specified identifier, if not exists, returns the default value.
        /// </summary>
        /// <param name="identifier">Identifier.</param>
        /// <param name="type">Type.</param>
        /// <param name="defaultValue">Default value.</param>
        /// <param name="settings">Settings.</param>
        public static object Load(string identifier, Type type, object defaultValue, SaveGameSettings settings)
        {
            if (string.IsNullOrEmpty(identifier))
            {
                throw new ArgumentNullException("identifier");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (defaultValue == null)
            {
                defaultValue = type.GetDefault();
            }
            settings.Identifier = identifier;
            if (!Exists(settings.Identifier, settings))
            {
                if (defaultValue == null)
                {
                    Debug.LogWarning("SaveGamePro: The specified identifier does not exists, please make sure the identifeir is exist before loading. The Default value is not specified, it might make exceptions and errors.");
                }
                else
                {
                    Debug.LogWarning("SaveGamePro: The specified identifier does not exists, please make sure the identifeir is exist before loading. Returning default value.");
                }
                return(defaultValue);
            }
            settings.Storage.OnLoad(settings);
            object result = null;
            Stream stream = settings.Storage.GetReadStream(settings);

            if (settings.Encrypt)
            {
                using (MemoryStream dataOut = new MemoryStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream(stream.ReadFully()))
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, settings.Decryptor, CryptoStreamMode.Read))
                        {
                            byte[] decryptedData = cryptoStream.ReadFully();
                            dataOut.Write(decryptedData, 0, decryptedData.Length);
                        }
                    }
                    dataOut.Flush();
                    result = settings.Formatter.Deserialize(dataOut, type, settings);
                }
            }
            else
            {
                result = settings.Formatter.Deserialize(stream, type, settings);
            }
            settings.Storage.OnLoaded(settings);
            stream.Dispose();
            if (result == null)
            {
                result = defaultValue;
            }
            if (OnLoaded != null)
            {
                OnLoaded(identifier, result, type, defaultValue, settings);
            }
            return(result);
        }
Example #21
0
 /// <summary>
 /// Loads the data into the value Asynchronously.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="value">Value.</param>
 /// <param name="settings">Settings.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static Task LoadIntoAsync <T>(string identifier, T value, SaveGameSettings settings)
 {
     return(LoadIntoAsync(identifier, (object)value, settings));
 }
Example #22
0
 /// <summary>
 /// Save the specified value using the identifier.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="value">Value.</param>
 /// <param name="settings">Settings.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static void Save <T>(string identifier, T value, SaveGameSettings settings)
 {
     Save(identifier, (object)value, settings);
 }
Example #23
0
 /// <summary>
 /// Gets the absolute path to the identifier.
 /// This method only works on platforms that support file storage.
 /// </summary>
 /// <param name="identifier"></param>
 /// <returns></returns>
 public static string GetAbsolutePath(string identifier, SaveGameSettings settings)
 {
     settings.Identifier = identifier;
     return(SaveGameFileStorage.GetAbsolutePath(settings.Identifier, settings.BasePath));
 }
Example #24
0
 /// <summary>
 /// Loads the data into the value.
 /// </summary>
 /// <param name="identifier">Identifier.</param>
 /// <param name="value">Value.</param>
 /// <param name="settings">Settings.</param>
 /// <typeparam name="T">The 1st type parameter.</typeparam>
 public static void LoadInto <T>(string identifier, T value, SaveGameSettings settings)
 {
     LoadInto(identifier, (object)value, settings);
 }