From 2b4761c6b5f3d770b7f4b79ee51600a9b2c30cc6 Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Tue, 5 Sep 2017 20:09:29 +1000 Subject: [PATCH 1/3] SaveManager now uses json files in a FungusSaves sub directory for all platforms but webplayer & webgl which still use playerprefs --- .../Fungus/Scripts/Components/SaveManager.cs | 53 +++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/Assets/Fungus/Scripts/Components/SaveManager.cs b/Assets/Fungus/Scripts/Components/SaveManager.cs index 5ae5b3a7..6451a6f9 100644 --- a/Assets/Fungus/Scripts/Components/SaveManager.cs +++ b/Assets/Fungus/Scripts/Components/SaveManager.cs @@ -10,14 +10,34 @@ namespace Fungus { /// /// Manages the Save History (a list of Save Points) and provides a set of operations for saving and loading games. + /// + /// Note WebGL and Webplayer (deprecated) save using playerprefs instead of using a json file in persistent storage + /// -webgl would require additional js to force a sync of FS.syncfs + /// -webplayer does not implement system io /// public class SaveManager : MonoBehaviour { protected static SaveHistory saveHistory = new SaveHistory(); + public static string STORAGE_DIRECTORY { get { return Application.persistentDataPath + "/FungusSaves/"; } } + + private static string GetFullFilePath(string saveDataKey) + { + return STORAGE_DIRECTORY + saveDataKey + ".json"; + } + protected virtual bool ReadSaveHistory(string saveDataKey) { - var historyData = PlayerPrefs.GetString(saveDataKey); + var historyData = string.Empty; +#if UNITY_WEBPLAYER || UNITY_WEBGL + historyData = PlayerPrefs.GetString(saveDataKey); +#else + var fullFilePath = GetFullFilePath(saveDataKey); + if (System.IO.File.Exists(fullFilePath)) + { + historyData = System.IO.File.ReadAllText(fullFilePath); + } +#endif//UNITY_WEBPLAYER if (!string.IsNullOrEmpty(historyData)) { var tempSaveHistory = JsonUtility.FromJson(historyData); @@ -36,8 +56,18 @@ namespace Fungus var historyData = JsonUtility.ToJson(saveHistory, true); if (!string.IsNullOrEmpty(historyData)) { +#if UNITY_WEBPLAYER || UNITY_WEBGL PlayerPrefs.SetString(saveDataKey, historyData); PlayerPrefs.Save(); +#else + var fileLoc = GetFullFilePath(saveDataKey); + + //make sure the dir exists + System.IO.FileInfo file = new System.IO.FileInfo(fileLoc); + file.Directory.Create(); + + System.IO.File.WriteAllText(fileLoc, historyData); +#endif//UNITY_WEBPLAYER return true; } @@ -57,7 +87,7 @@ namespace Fungus SavePointLoaded.NotifyEventHandlers(savePointKey); // Execute any block containing a SavePoint command matching the save key, with Resume On Load enabled - var savePoints = Object.FindObjectsOfType(); + var savePoints = UnityEngine.Object.FindObjectsOfType(); for (int i = 0; i < savePoints.Length; i++) { var savePoint = savePoints[i]; @@ -83,7 +113,7 @@ namespace Fungus // Each scene should have one Save Point with the IsStartPoint property enabled. // We automatically start execution from this command whenever the scene starts 'normally' (i.e. first play, restart or scene load via the Load Scene command or SceneManager.LoadScene). - var savePoints = Object.FindObjectsOfType(); + var savePoints = UnityEngine.Object.FindObjectsOfType(); for (int i = 0; i < savePoints.Length; i++) { var savePoint = savePoints[i]; @@ -207,8 +237,16 @@ namespace Fungus /// public void Delete(string saveDataKey) { +#if UNITY_WEBPLAYER || UNITY_WEBGL PlayerPrefs.DeleteKey(saveDataKey); PlayerPrefs.Save(); +#else + var fullFilePath = GetFullFilePath(saveDataKey); + if (System.IO.File.Exists(fullFilePath)) + { + System.IO.File.Delete(fullFilePath); + } +#endif//UNITY_WEBPLAYER } /// @@ -216,8 +254,13 @@ namespace Fungus /// public bool SaveDataExists(string saveDataKey) { +#if UNITY_WEBPLAYER || UNITY_WEBGL return PlayerPrefs.HasKey(saveDataKey); - } +#else + var fullFilePath = GetFullFilePath(saveDataKey); + return System.IO.File.Exists(fullFilePath); +#endif//UNITY_WEBPLAYER + } /// /// Creates a new Save Point using a key and description, and adds it to the Save History. @@ -275,7 +318,7 @@ namespace Fungus return saveHistory.GetDebugInfo(); } - #endregion +#endregion } } From b209d4fcd20b93893329c2dbddafd22eb6f48d68 Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Tue, 12 Sep 2017 20:00:27 +1000 Subject: [PATCH 2/3] Allow multiple CommandInfos to be added to a class SpawnObject can now also be found under GameObject/Instantiate --- Assets/Fungus/Scripts/Commands/SpawnObject.cs | 3 +++ Assets/Fungus/Scripts/Components/Command.cs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/Assets/Fungus/Scripts/Commands/SpawnObject.cs b/Assets/Fungus/Scripts/Commands/SpawnObject.cs index 734243f3..a5d4f6c8 100644 --- a/Assets/Fungus/Scripts/Commands/SpawnObject.cs +++ b/Assets/Fungus/Scripts/Commands/SpawnObject.cs @@ -12,6 +12,9 @@ namespace Fungus [CommandInfo("Scripting", "Spawn Object", "Spawns a new object based on a reference to a scene or prefab game object.")] + [CommandInfo("GameObject", + "Instantiate", + "Instantiate a game object")] [AddComponentMenu("")] [ExecuteInEditMode] public class SpawnObject : Command diff --git a/Assets/Fungus/Scripts/Components/Command.cs b/Assets/Fungus/Scripts/Components/Command.cs index 64407984..0c63df11 100644 --- a/Assets/Fungus/Scripts/Components/Command.cs +++ b/Assets/Fungus/Scripts/Components/Command.cs @@ -11,6 +11,8 @@ namespace Fungus /// /// Attribute class for Fungus commands. /// + /// + [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class CommandInfoAttribute : Attribute { /// From cc7bbf0db19666efc901a9fc48d13ee5463f5518 Mon Sep 17 00:00:00 2001 From: desktop-maesty/steve Date: Tue, 19 Sep 2017 17:31:15 +1000 Subject: [PATCH 3/3] GetCommandInfo returns the highest priority CommandInfo to better support multiple names while maintaining what is shown in the inspector --- Assets/Fungus/Scripts/Commands/SpawnObject.cs | 3 ++- Assets/Fungus/Scripts/Editor/CommandEditor.cs | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Assets/Fungus/Scripts/Commands/SpawnObject.cs b/Assets/Fungus/Scripts/Commands/SpawnObject.cs index a5d4f6c8..35fed925 100644 --- a/Assets/Fungus/Scripts/Commands/SpawnObject.cs +++ b/Assets/Fungus/Scripts/Commands/SpawnObject.cs @@ -11,7 +11,8 @@ namespace Fungus /// [CommandInfo("Scripting", "Spawn Object", - "Spawns a new object based on a reference to a scene or prefab game object.")] + "Spawns a new object based on a reference to a scene or prefab game object.", + Priority = 10)] [CommandInfo("GameObject", "Instantiate", "Instantiate a game object")] diff --git a/Assets/Fungus/Scripts/Editor/CommandEditor.cs b/Assets/Fungus/Scripts/Editor/CommandEditor.cs index 4f9f43f1..05ae1e17 100644 --- a/Assets/Fungus/Scripts/Editor/CommandEditor.cs +++ b/Assets/Fungus/Scripts/Editor/CommandEditor.cs @@ -16,17 +16,22 @@ namespace Fungus.EditorUtils public static CommandInfoAttribute GetCommandInfo(System.Type commandType) { + CommandInfoAttribute retval = null; + object[] attributes = commandType.GetCustomAttributes(typeof(CommandInfoAttribute), false); foreach (object obj in attributes) { CommandInfoAttribute commandInfoAttr = obj as CommandInfoAttribute; if (commandInfoAttr != null) { - return commandInfoAttr; + if (retval == null) + retval = commandInfoAttr; + else if (retval.Priority < commandInfoAttr.Priority) + retval = commandInfoAttr; } } - return null; + return retval; } public virtual void DrawCommandInspectorGUI()