Skip to content

Commit 88c5005

Browse files
authored
Merge pull request #13 from coryleach/fb-streamingassets
Addition of methods for loading saved files from streaming assets to …
2 parents d4dfb60 + 16286fe commit 88c5005

3 files changed

Lines changed: 74 additions & 20 deletions

File tree

Runtime/SaveLoadManager.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,32 @@ public void Save(object obj, string filename, string folder = null)
7777
/// Gets the list of save files that have been created
7878
/// </summary>
7979
/// <param name="folder">sub folder</param>
80-
/// <param name="extension">include only files with this extension</param>
80+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
8181
/// <returns>list of file names (excludes the path)</returns>
82-
public string[] GetFiles(string folder = null, string extension = null)
82+
public string[] GetFiles(string folder = null, bool streamingAssets = false)
8383
{
8484
if (string.IsNullOrEmpty(folder))
8585
{
8686
folder = defaultFolder;
8787
}
88-
return SaveLoadUtility.GetSavedFiles(folder,baseFolder, extension);
88+
return SaveLoadUtility.GetSavedFiles(folder,baseFolder, streamingAssets);
89+
}
90+
91+
/// <summary>
92+
/// Gets the list of save files that have been created
93+
/// </summary>
94+
/// <param name="list">list to be populated with file names</param>
95+
/// <param name="folder">sub folder</param>
96+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
97+
/// <returns>list of file names (excludes the path)</returns>
98+
public void GetFiles(List<string> list, string folder = null, bool streamingAssets = false)
99+
{
100+
if (string.IsNullOrEmpty(folder))
101+
{
102+
folder = defaultFolder;
103+
}
104+
105+
SaveLoadUtility.GetSavedFiles(list, folder,baseFolder, streamingAssets);
89106
}
90107

91108
/// <summary>
@@ -126,11 +143,12 @@ public T Copy<T>(T obj)
126143
/// </summary>
127144
/// <param name="filename">Name of file to load from</param>
128145
/// <param name="folder">Name of folder containing the file</param>
146+
/// <param name="streamingAssets">Load file from streaming assets</param>
129147
/// <typeparam name="T">Type of object to be loaded from file</typeparam>
130148
/// <returns>Instance of object loaded from file</returns>
131-
public T Load<T>(string filename, string folder = null)
149+
public T Load<T>(string filename, string folder = null, bool streamingAssets = false)
132150
{
133-
return (T)Load(typeof(T), filename, folder);
151+
return (T)Load(typeof(T), filename, folder, streamingAssets);
134152
}
135153

136154
/// <summary>
@@ -139,15 +157,16 @@ public T Load<T>(string filename, string folder = null)
139157
/// <param name="type">Type of object to be loaded</param>
140158
/// <param name="filename">Name of file to load object from</param>
141159
/// <param name="folder">Name of folder containing the file to be loaded</param>
160+
/// <param name="streamingAssets">Load file from streaming assets</param>
142161
/// <returns>Instance of object to be loaded. Null if file did not exist.</returns>
143-
public object Load(Type type, string filename, string folder = null)
162+
public object Load(Type type, string filename, string folder = null, bool streamingAssets = false)
144163
{
145164
if (string.IsNullOrEmpty(folder))
146165
{
147166
folder = defaultFolder;
148167
}
149168
var saveLoadMethod = GetSaveLoadMethod(saveMethod);
150-
return SaveLoadUtility.Load(type, saveLoadMethod,filename,folder, baseFolder);
169+
return SaveLoadUtility.Load(type, saveLoadMethod,filename,folder, baseFolder, streamingAssets);
151170
}
152171

153172
/// <summary>

Runtime/SaveLoadUtility.cs

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public static class SaveLoadUtility
1111
private const string DefaultFolderName = "SaveLoad";
1212
private const string DefaultBaseFolderPath = "GameData";
1313

14-
public static string GetSavePath(string folderName = null, string baseFolderPath = null)
14+
public static string GetSavePath(string folderName = null, string baseFolderPath = null, bool streamingAssets = false)
1515
{
16-
return GetRuntimeSavePath(folderName, baseFolderPath);
16+
return !streamingAssets ? GetRuntimeSavePath(folderName, baseFolderPath) : GetStreamingAssetsSavePath(folderName, baseFolderPath);
1717
}
1818

1919
public static string GetRuntimeSavePath(string folderName = null, string baseFolderPath = null)
@@ -29,7 +29,24 @@ public static string GetRuntimeSavePath(string folderName = null, string baseFol
2929
}
3030

3131
var savePath = $"{Application.persistentDataPath}/{baseFolderPath}/";
32-
savePath = savePath + folderName + "/";
32+
savePath = $"{savePath}{folderName}/";
33+
return savePath;
34+
}
35+
36+
public static string GetStreamingAssetsSavePath(string folderName = null, string baseFolderPath = null)
37+
{
38+
if (string.IsNullOrEmpty(folderName))
39+
{
40+
folderName = DefaultFolderName;
41+
}
42+
43+
if (string.IsNullOrEmpty(baseFolderPath))
44+
{
45+
baseFolderPath = DefaultBaseFolderPath;
46+
}
47+
48+
var savePath = $"{Application.streamingAssetsPath}/{baseFolderPath}/";
49+
savePath = $"{savePath}{folderName}/";
3350
return savePath;
3451
}
3552

@@ -77,10 +94,11 @@ public static void Save(object saveObject, ISerializationMethod serializationMet
7794
/// <param name="filename"></param>
7895
/// <param name="folderName"></param>
7996
/// <param name="baseFolderPath"></param>
97+
/// <param name="streamingAssets"></param>
8098
/// <returns></returns>
81-
public static object Load(System.Type objectType, ISerializationMethod serializationMethod, string filename, string folderName = null, string baseFolderPath = null)
99+
public static object Load(System.Type objectType, ISerializationMethod serializationMethod, string filename, string folderName = null, string baseFolderPath = null, bool streamingAssets = false)
82100
{
83-
var savePath = GetSavePath(folderName, baseFolderPath);
101+
var savePath = GetSavePath(folderName, baseFolderPath, streamingAssets);
84102
var saveFilename = savePath + GetSaveFileName(filename);
85103

86104
object returnObject = null;
@@ -104,11 +122,12 @@ public static object Load(System.Type objectType, ISerializationMethod serializa
104122
/// </summary>
105123
/// <param name="folderName">folder containing the save files</param>
106124
/// <param name="baseFolderPath">base path to the folder</param>
107-
/// <param name="extension">include only files with the specified extension</param>
125+
/// <param name="extension">include only files with this extension</param>
126+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
108127
/// <returns>list of file names</returns>
109-
public static IEnumerable<string> EnumerateSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null)
128+
public static IEnumerable<string> EnumerateSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null, bool streamingAssets = false)
110129
{
111-
var savePath = GetSavePath(folderName,baseFolderPath);
130+
var savePath = GetSavePath(folderName,baseFolderPath,streamingAssets);
112131

113132
//If directory does not exist we're done
114133
if (!Directory.Exists(savePath))
@@ -126,13 +145,29 @@ public static IEnumerable<string> EnumerateSavedFiles(string folderName = null,
126145
/// <summary>
127146
/// Creates an array list of save files in the given folder and path
128147
/// </summary>
129-
/// <param name="folderName">folder containing the save files</param>
130-
/// <param name="baseFolderPath">base path to the folder</param>
148+
/// <param name="folderName"></param>
149+
/// <param name="baseFolderPath"></param>
150+
/// <param name="extension">include only files with this extension</param>
151+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
152+
/// <returns>Array of file names</returns>
153+
public static string[] GetSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null, bool streamingAssets = false)
154+
{
155+
return EnumerateSavedFiles(folderName, baseFolderPath, extension, streamingAssets).ToArray();
156+
}
157+
158+
/// <summary>
159+
/// Populates a given array with a list of save files in the given folder and path
160+
/// </summary>
161+
/// <param name="list">list to be populated with file names</param>
162+
/// <param name="folderName"></param>
163+
/// <param name="baseFolderPath"></param>
131164
/// <param name="extension">include only files with this extension</param>
165+
/// <param name="streamingAssets">Will use Application.streamingAssetsPath as base path if true otherwise Application.persistentDataPath</param>
132166
/// <returns>Array of file names</returns>
133-
public static string[] GetSavedFiles(string folderName = null, string baseFolderPath = null, string extension = null)
167+
public static void GetSavedFiles(List<string> list, string folderName = null, string baseFolderPath = null, string extension = null, bool streamingAssets = false)
134168
{
135-
return EnumerateSavedFiles(folderName, baseFolderPath, extension).ToArray();
169+
list.Clear();
170+
list.AddRange(EnumerateSavedFiles(folderName, baseFolderPath, extension streamingAssets));
136171
}
137172

138173
/// <summary>

Tests/Editor/SaveLoadUtilityTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void CanGetFiles()
8383
SaveLoadUtility.Save(testSave,serializationMethod,filename,folder);
8484

8585
var files = SaveLoadUtility.GetSavedFiles(folder);
86-
Assert.IsTrue(files.Length == 1);
86+
Assert.IsTrue(files.Length == 1,$"Total Save Files: {files.Length} Expected 1");
8787

8888
//Files should contain a list of names that exactly match the file name used
8989
//omits the path of the file

0 commit comments

Comments
 (0)