/// <summary> /// Checks whether the specified identifier exists or not. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="path">Path.</param> /// <param name="web">Check in Web?</param> /// <param name="webUsername">Web username.</param> /// <param name="webPassword">Web password.</param> /// <param name="webURL">Web URL.</param> public static bool Exists(string identifier, SaveGamePath path) { if (string.IsNullOrEmpty(identifier)) { throw new System.ArgumentNullException("identifier"); } string filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", Application.dataPath, identifier); break; case SaveGamePath.RoomDataPath: filePath = string.Format("{0}/{1}", Application.dataPath + "/RoomData", identifier); break; } } else { filePath = identifier; } #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { bool exists = false; #if UNITY_WSA || UNITY_WINRT exists = UnityEngine.Windows.Directory.Exists(filePath); #else exists = Directory.Exists(filePath); #endif if (!exists) { #if UNITY_WSA || UNITY_WINRT exists = UnityEngine.Windows.File.Exists(filePath); #else exists = File.Exists(filePath); #endif } return(exists); } else { return(PlayerPrefs.HasKey(filePath)); } #else return(PlayerPrefs.HasKey(filePath)); #endif }
/// <summary> /// Delete the specified identifier and path. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="path">Path.</param> public static void Delete(string identifier, SaveGamePath path) { if (string.IsNullOrEmpty(identifier)) { throw new System.ArgumentNullException("identifier"); } string filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", Application.dataPath, identifier); break; case SaveGamePath.RoomDataPath: filePath = string.Format("{0}/{1}", Application.dataPath + "/RoomData", identifier); break; } } else { filePath = identifier; } if (!Exists(filePath, path)) { return; } #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #if UNITY_WSA || UNITY_WINRT UnityEngine.Windows.File.Delete(filePath); #else File.Delete(filePath); #endif } else { PlayerPrefs.DeleteKey(filePath); } #else PlayerPrefs.DeleteKey(filePath); #endif }
/// <summary> /// Deletes all. /// </summary> /// <param name="path">Path.</param> public static void DeleteAll(SaveGamePath path) { string dirPath = ""; switch (path) { case SaveGamePath.PersistentDataPath: dirPath = Application.persistentDataPath; break; case SaveGamePath.DataPath: dirPath = Application.dataPath; break; case SaveGamePath.RoomDataPath: dirPath = Application.dataPath + "/RoomData"; break; } #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #if UNITY_WSA || UNITY_WINRT UnityEngine.Windows.Directory.Delete(dirPath); #else DirectoryInfo info = new DirectoryInfo(dirPath); FileInfo [] files = info.GetFiles(); for (int i = 0; i < files.Length; i++) { files [i].Delete(); } DirectoryInfo [] dirs = info.GetDirectories(); for (int i = 0; i < dirs.Length; i++) { dirs [i].Delete(true); } #endif } else { PlayerPrefs.DeleteAll(); } #else PlayerPrefs.DeleteAll(); #endif }
/// <summary> /// The constructor sets all settings and then loads all local files to Dictionary<string, string> Saves. /// </summary> /// <param name="settings"></param> public SaveService(SaveSettings settings) { SerializerResolver = settings.SerializerResolver; SerializerSettings = new JsonSerializerSettings { ContractResolver = SerializerResolver }; CurrentPlatform = Application.platform; Formatter = new BinaryFormatter(); Encode = settings.Encode; EncodePassword = settings.EncodePassword; Encoding = settings.Encoding; SaveId = settings.SaveId; SavePath = settings.SavePath; Saves = LoadLocal(); }
/// <summary> /// Retrieves directories from the given directory path. /// </summary> /// <param name="identifier"></param> /// <param name="path"></param> /// <returns></returns> public static DirectoryInfo[] GetDirectories(string identifier, SaveGamePath path) { if (string.IsNullOrEmpty(identifier)) { identifier = string.Empty; } var filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", Application.dataPath, identifier); break; } } else { filePath = identifier; } var directories = new DirectoryInfo[0]; if (!Exists(filePath, path)) { return(directories); } if (Directory.Exists(filePath)) { var info = new DirectoryInfo(filePath); directories = info.GetDirectories(); } return(directories); }
/// <summary> /// Checks whether the specified identifier exists or not. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="path">Path.</param> /// <param name="web">Check in Web?</param> /// <param name="webUsername">Web username.</param> /// <param name="webPassword">Web password.</param> /// <param name="webURL">Web URL.</param> public static bool Exists(string identifier, SaveGamePath path) { string filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", Application.dataPath, identifier); break; } } else { filePath = identifier; } #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #if UNITY_WSA || UNITY_WINRT return(UnityEngine.Windows.File.Exists(filePath)); #else return(File.Exists(identifier)); #endif } else { return(PlayerPrefs.HasKey(filePath)); } #else return(PlayerPrefs.HasKey(filePath)); #endif }
/// <summary> /// Retrieves files from the given directory path. /// </summary> /// <param name="identifier"></param> /// <param name="path"></param> /// <returns></returns> public static FileInfo[] GetFiles(string identifier, SaveGamePath path) { if (string.IsNullOrEmpty(identifier)) { identifier = string.Empty; } string filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}", identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", $"E:\\MaoXianYuJingYing\\Unity\\Assets\\Data\\LevelData", identifier); break; } } else { filePath = identifier; } FileInfo[] files = new FileInfo[0]; if (!Exists(filePath, path)) { return(files); } if (Directory.Exists(filePath)) { DirectoryInfo info = new DirectoryInfo(filePath); files = info.GetFiles(); } return(files); }
/// <summary> /// Clear the specified path. /// Alias of DeleteAll /// </summary> /// <param name="path">Path.</param> public static void Clear(SaveGamePath path) { DeleteAll(path); }
/// <summary> /// Loads data using identifier. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="defaultValue">Default Value.</param> /// <param name="encode">Load encrypted data? (set it to true if you have used encryption in save)</param> /// <param name="password">Encryption Password.</param> /// <param name="serializer">Serializer.</param> /// <param name="encoder">Encoder.</param> /// <param name="encoding">Encoding.</param> /// <param name="path">Path.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T Load <T>(string identifier, T defaultValue, bool encode, string password, ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding, SaveGamePath path) { if (string.IsNullOrEmpty(identifier)) { throw new ArgumentNullException("identifier"); } if (serializer == null) { serializer = Serializer; } if (encoding == null) { encoding = DefaultEncoding; } if (defaultValue == null) { defaultValue = default(T); } var result = defaultValue; var filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", Application.dataPath, identifier); break; } } else { filePath = identifier; } #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (!Exists(filePath, path)) #else if (!Exists(filePath, path)) #endif { Debug.LogWarningFormat( "The specified identifier ({1}) does not exists. please use Exists () to check for existent before calling Load.\n" + "returning the default(T) instance.", filePath, identifier); return(result); } Stream stream = null; if (encode) { var data = ""; #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #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 var decoded = encoder.Decode(data, password); stream = new MemoryStream(encoding.GetBytes(decoded), true); } else { #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #if UNITY_WSA || UNITY_WINRT stream = new MemoryStream(UnityEngine.Windows.File.ReadAllBytes(filePath)); #else stream = File.OpenRead(filePath); #endif } else { var 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; } if (LoadCallback != null) { LoadCallback.Invoke( result, identifier, encode, password, serializer, encoder, encoding, path); } if (OnLoaded != null) { OnLoaded( result, identifier, encode, password, serializer, encoder, encoding, path); } return(result); }
/// <summary> /// Load the specified identifier, defaultValue and savePath. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="defaultValue">Default value.</param> /// <param name="savePath">Save path.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T Load <T>(string identifier, T defaultValue, SaveGamePath savePath) { return(Load(identifier, defaultValue, Encode, EncodePassword, Serializer, Encoder, DefaultEncoding, savePath)); }
/// <summary> /// Saves data using the identifier. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="obj">Object to save.</param> /// <param name="encode">Encrypt the data?</param> /// <param name="password">Encryption Password.</param> /// <param name="serializer">Serializer.</param> /// <param name="encoder">Encoder.</param> /// <param name="encoding">Encoding.</param> /// <param name="path">Path.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static void Save <T>(string identifier, T obj, bool encode, string password, ISaveGameSerializer serializer, ISaveGameEncoder encoder, Encoding encoding, SaveGamePath path) { if (string.IsNullOrEmpty(identifier)) { throw new ArgumentNullException("identifier"); } if (serializer == null) { serializer = Serializer; } if (encoding == null) { encoding = DefaultEncoding; } var filePath = ""; if (!IsFilePath(identifier)) { switch (path) { default: case SaveGamePath.PersistentDataPath: filePath = string.Format("{0}/{1}", Application.persistentDataPath, identifier); break; case SaveGamePath.DataPath: filePath = string.Format("{0}/{1}", Application.dataPath, identifier); break; } } else { filePath = identifier; } if (obj == null) { obj = default(T); } Stream stream = null; #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL #if UNITY_WSA || UNITY_WINRT UnityEngine.Windows.Directory.CreateDirectory(filePath); #else Directory.CreateDirectory(Path.GetDirectoryName(filePath)); #endif #endif if (encode) { stream = new MemoryStream(); } else { #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #if UNITY_WSA || UNITY_WINRT stream = new MemoryStream(); #else stream = File.Create(filePath); #endif } else { stream = new MemoryStream(); } #else stream = new MemoryStream(); #endif } serializer.Serialize(obj, stream, encoding); if (encode) { var data = encoding.GetString(((MemoryStream)stream).ToArray()); var encoded = encoder.Encode(data, password); #if !UNITY_SAMSUNGTV && !UNITY_TVOS && !UNITY_WEBGL if (IOSupported()) { #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 (!IOSupported()) { var data = encoding.GetString(((MemoryStream)stream).ToArray()); PlayerPrefs.SetString(filePath, data); PlayerPrefs.Save(); } stream.Dispose(); if (SaveCallback != null) { SaveCallback.Invoke( obj, identifier, encode, password, serializer, encoder, encoding, path); } if (OnSaved != null) { OnSaved( obj, identifier, encode, password, serializer, encoder, encoding, path); } }
/// <summary> /// Save the specified identifier, obj and savePath. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="obj">Object.</param> /// <param name="savePath">Save path.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static void Save <T>(string identifier, T obj, SaveGamePath savePath) { Save(identifier, obj, Encode, EncodePassword, Serializer, Encoder, DefaultEncoding, savePath); }
/// <summary> /// Load the specified identifier and savePath. /// </summary> /// <param name="identifier">Identifier.</param> /// <param name="savePath">Save path.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T Load <T>(string identifier, string encodePassword, SaveGamePath savePath) { return(Load <T>(identifier, default(T), true, encodePassword, Serializer, Encoder, DefaultEncoding, savePath)); }