diff --git a/README.md b/README.md index 842a096..95ac853 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

Gameframe.SaveLoad 👋

- Version + Version Twitter: coryleach @@ -13,7 +13,7 @@ Serialization helper utility that supports save, load and encryption. #### Using UnityPackageManager (for Unity 2019.3 or later) Open the package manager window (menu: Window > Package Manager)
Select "Add package from git URL...", fill in the pop-up with the following link:
-https://github.com/coryleach/UnitySaveLoad.git#1.0.6
+https://github.com/coryleach/UnitySaveLoad.git#1.0.7
#### Using UnityPackageManager (for Unity 2019.1 or later) @@ -21,7 +21,7 @@ Find the manifest.json file in the Packages folder of your project and edit it t ```js { "dependencies": { - "com.gameframe.saveload": "https://github.com/coryleach/UnitySaveLoad.git#1.0.6", + "com.gameframe.saveload": "https://github.com/coryleach/UnitySaveLoad.git#1.0.7", ... }, } diff --git a/Runtime/SaveLoadManager.cs b/Runtime/SaveLoadManager.cs index 7b0b9ba..e412332 100644 --- a/Runtime/SaveLoadManager.cs +++ b/Runtime/SaveLoadManager.cs @@ -13,18 +13,18 @@ public class SaveLoadManager : ScriptableObject { [Header("Settings"),SerializeField] private string defaultFolder = "SaveData"; public string DefaultFolder => defaultFolder; - + [SerializeField] private string baseFolder = "GameData"; public string BaseFolder => baseFolder; - + [SerializeField] private SerializationMethodType saveMethod = SerializationMethodType.Default; [Header("Encryption"),SerializeField] protected string key = string.Empty; public string Key => key; - + [SerializeField] protected string salt = string.Empty; public string Salt => salt; - + private Dictionary _methods; private void OnEnable() @@ -53,10 +53,10 @@ public static SaveLoadManager Create(string baseFolder, string defaultFolder, Se instance.key = key; instance.salt = salt; instance.saveMethod = saveMethod; - + return instance; } - + ///

/// Save an object to disk /// @@ -77,14 +77,15 @@ public void Save(object obj, string filename, string folder = null) /// Gets the list of save files that have been created /// /// sub folder + /// include only files with this extension /// list of file names (excludes the path) - public string[] GetFiles(string folder = null) + public string[] GetFiles(string folder = null, string extension = null) { if (string.IsNullOrEmpty(folder)) { folder = defaultFolder; } - return SaveLoadUtility.GetSavedFiles(folder,baseFolder); + return SaveLoadUtility.GetSavedFiles(folder,baseFolder, extension); } /// @@ -119,7 +120,7 @@ public T Copy(T obj) var saveLoadMethod = GetSaveLoadMethod(saveMethod); return (T)saveLoadMethod.Copy(obj); } - + /// /// Load an object from disk /// @@ -162,7 +163,7 @@ public void DeleteSave(string filename, string folder = null) } SaveLoadUtility.DeleteSavedFile(filename,folder, baseFolder); } - + /// /// Save object to file and specify the method of save/load /// @@ -217,7 +218,7 @@ public void SaveUnityObject(UnityEngine.Object unityObj, string filename, string { jsonData = JsonUtility.ToJson(unityObj) }; - + Save(savedObj,filename,folder); } @@ -233,12 +234,12 @@ public void SaveUnityObject(UnityEngine.Object unityObj, string filename, string public bool LoadUnityObjectOverwrite(UnityEngine.Object objectToOverwrite, string filename, string folder = null) { var savedObj = Load(filename, folder); - + if (savedObj == null || string.IsNullOrEmpty(savedObj.jsonData)) { return false; } - + JsonUtility.FromJsonOverwrite(savedObj.jsonData,objectToOverwrite); return true; } @@ -253,7 +254,7 @@ public void CopyUnityObjectOverwrite(UnityEngine.Object toCopy, UnityEngine.Obje var jsonData = JsonUtility.ToJson(toCopy); JsonUtility.FromJsonOverwrite(jsonData,toOverwrite); } - + /// /// JsonSerializedUnityObject /// Wrapper for json data created when using Unity's JsonUtility to serialize an object derived from UnityEngine.Object @@ -280,7 +281,7 @@ public void SetCustomSerializationMethod(ISerializationMethod customSerializatio } _methods[SerializationMethodType.Custom] = customSerializationMethod; } - + private ISerializationMethod GetSaveLoadMethod(SerializationMethodType methodType) { if (_methods == null) @@ -305,14 +306,14 @@ private ISerializationMethod GetSaveLoadMethod(SerializationMethodType methodTyp case SerializationMethodType.UnityJson: method = new SerializationMethodUnityJson(); break; - + case SerializationMethodType.BinaryEncrypted: method = new SerializationMethodBinaryEncrypted(key,salt); break; case SerializationMethodType.UnityJsonEncrypted: method = new SerializationMethodUnityJsonEncrypted(key,salt); break; - + #if JSON_DOT_NET case SerializationMethodType.JsonDotNet: method = new SerializationMethodJsonDotNet(); @@ -321,7 +322,7 @@ private ISerializationMethod GetSaveLoadMethod(SerializationMethodType methodTyp method = new SerializationMethodJsonDotNetEncrypted(key,salt); break; #endif - + case SerializationMethodType.Custom: throw new MissingComponentException("SerializationMethodType is Custom but no custom ISerializationMethod was found."); default: @@ -329,11 +330,9 @@ private ISerializationMethod GetSaveLoadMethod(SerializationMethodType methodTyp } _methods[methodType] = method; - + return method; } - - } -} - + } +} diff --git a/Runtime/SaveLoadUtility.cs b/Runtime/SaveLoadUtility.cs index 1c1ac8f..7f0666e 100644 --- a/Runtime/SaveLoadUtility.cs +++ b/Runtime/SaveLoadUtility.cs @@ -10,12 +10,12 @@ public static class SaveLoadUtility //Default folder name will be used if none is provided. private const string DefaultFolderName = "SaveLoad"; private const string DefaultBaseFolderPath = "GameData"; - + public static string GetSavePath(string folderName = null, string baseFolderPath = null) { return GetRuntimeSavePath(folderName, baseFolderPath); } - + public static string GetRuntimeSavePath(string folderName = null, string baseFolderPath = null) { if (string.IsNullOrEmpty(folderName)) @@ -34,7 +34,7 @@ public static string GetRuntimeSavePath(string folderName = null, string baseFol } /// - /// + /// /// /// /// @@ -42,9 +42,9 @@ private static string GetSaveFileName(string fileName) { return fileName; } - + /// - /// + /// /// /// /// @@ -55,7 +55,7 @@ public static void Save(object saveObject, ISerializationMethod serializationMet { var savePath = GetSavePath(folderName,baseFolderPath); var saveFilename = GetSaveFileName(filename); - + //Create directory if it does not exist if (!Directory.Exists(savePath)) { @@ -95,43 +95,46 @@ public static object Load(System.Type objectType, ISerializationMethod serializa returnObject = serializationMethod.Load(objectType, saveFile); saveFile.Close(); } - + return returnObject; } - + /// /// Enumerate files in the save directory /// /// folder containing the save files /// base path to the folder + /// include only files with the specified extension /// list of file names - public static IEnumerable EnumerateSavedFiles(string folderName = null, string baseFolderPath = null) + public static IEnumerable EnumerateSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null) { var savePath = GetSavePath(folderName,baseFolderPath); - + //If directory does not exist we're done if (!Directory.Exists(savePath)) { yield break; } - - foreach ( var file in Directory.EnumerateFiles(savePath,"*",SearchOption.AllDirectories) ) + + var searchPattern = string.IsNullOrEmpty(extension) ? "*" : $"*.{extension}"; + foreach ( var file in Directory.EnumerateFiles(savePath,searchPattern,SearchOption.AllDirectories) ) { yield return Path.GetFileName(file); } } - + /// /// Creates an array list of save files in the given folder and path /// - /// - /// + /// folder containing the save files + /// base path to the folder + /// include only files with this extension /// Array of file names - public static string[] GetSavedFiles(string folderName = null, string baseFolderPath = null) + public static string[] GetSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null) { - return EnumerateSavedFiles(folderName, baseFolderPath).ToArray(); + return EnumerateSavedFiles(folderName, baseFolderPath, extension).ToArray(); } - + /// /// Check if a saved file exists /// @@ -175,10 +178,8 @@ public static void DeleteDirectory(string path) { DeleteDirectory(dir); } - + Directory.Delete(path,false); } } } - - diff --git a/Tests/Editor/SaveLoadUtilityTests.cs b/Tests/Editor/SaveLoadUtilityTests.cs index 53dd265..97d301b 100644 --- a/Tests/Editor/SaveLoadUtilityTests.cs +++ b/Tests/Editor/SaveLoadUtilityTests.cs @@ -27,7 +27,6 @@ private ISerializationMethod GetSerializationMethod(SerializationMethodType meth case SerializationMethodType.UnityJsonEncrypted: return new SerializationMethodUnityJsonEncrypted(TestKey,TestSalt); #if JSON_DOT_NET - case SerializationMethodType.JsonDotNet: return new SerializationMethodJsonDotNet(); case SerializationMethodType.JsonDotNetEncrypted: @@ -96,6 +95,29 @@ public void CanGetFiles() Assert.IsTrue(files.Length == 0); } + [Test] + public void CanGetFilesWithExtension() + { + var testSave = new SaveLoadTestObject() {testData = "SaveFileExists"}; + var serializationMethod = GetSerializationMethod(SerializationMethodType.Binary); + var filename = "TestSave.sav"; + var folder = "TestFolder"; + + SaveLoadUtility.Save(testSave,serializationMethod,filename,folder); + + var files = SaveLoadUtility.GetSavedFiles(folder,null, "sav"); + Assert.IsTrue(files.Length == 1); + + //Files should contain a list of names that exactly match the file name used + //omits the path of the file + Assert.IsTrue(files[0] == filename); + + SaveLoadUtility.DeleteSavedFile(filename,folder); + + files = SaveLoadUtility.GetSavedFiles(); + Assert.IsTrue(files.Length == 0); + } + [TearDown] public void TearDown() { diff --git a/package.json b/package.json index 4276b20..7d4b75d 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.gameframe.saveload", "displayName": "Gameframe.SaveLoad", - "version": "1.0.6", + "version": "1.0.7", "description": "Serialization helper utility that supports save, load and encryption.", "keywords": [], "author": {