diff --git a/com.stansassets.plugins-dev-kit/Runtime/Config/PackagesConfig.cs b/com.stansassets.plugins-dev-kit/Runtime/Config/PackagesConfig.cs index d5f0dab..24342a9 100644 --- a/com.stansassets.plugins-dev-kit/Runtime/Config/PackagesConfig.cs +++ b/com.stansassets.plugins-dev-kit/Runtime/Config/PackagesConfig.cs @@ -9,5 +9,10 @@ public static class PackagesConfig /// Package runtime settings location path. /// public const string SettingsPath = "Assets/Plugins/StansAssets/Settings"; + + /// + /// Library settings folder path. + /// + public const string LibraryPath = "Library/StansAssets"; } } diff --git a/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs new file mode 100644 index 0000000..24cd248 --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs @@ -0,0 +1,41 @@ +using System; +using UnityEngine; + +namespace StansAssets.Plugins +{ + [Serializable] + public class LocalProjectSettings + { + public LocalProjectSettings() + { + PackageName = "newPackage"; + SettingsFileName = GetType().Name; + } + + public LocalProjectSettings(string packageName, string settingsFileName = null) + { + PackageName = packageName; + SettingsFileName = settingsFileName == null ? settingsFileName : GetType().Name; + } + + /// + /// Plugin package name. + /// + public string PackageName { get; } + + /// + /// Plugin settings folder path. + /// + public string SettingsFolderPath => $"{Application.dataPath}/{PackagesConfig.LibraryPath}/{PackageName}"; + + /// + /// Plugin settings file path. + /// + public string SettingsFilePath => $"{SettingsFolderPath}/{SettingsFileName}.json"; + + /// + /// Settings file name. + /// + public string SettingsFileName { get; } + } +} \ No newline at end of file diff --git a/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs.meta b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs.meta new file mode 100644 index 0000000..76cf654 --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d4a0268991794378bbd76db3ebffcef6 +timeCreated: 1614852471 \ No newline at end of file diff --git a/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs new file mode 100644 index 0000000..439239b --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs @@ -0,0 +1,110 @@ +using System; +using System.IO; +using System.Threading.Tasks; +using UnityEngine; + +namespace StansAssets.Plugins +{ + public abstract class LocalProjectSettingsSingleton : LocalProjectSettings where T : LocalProjectSettings, new() + { + static T s_Instance; + + /// + /// Returns a singleton class instance + /// If current instance is not assigned it will try to find an object of the instance type, + /// in case instance already exists in a project. If not, new instance will be created, + /// and saved under a location + /// + public static T Instance + { + get + { + if (s_Instance == null) + { + var inst = new T(); + s_Instance = InitFromCache(inst); + + if (s_Instance == null) + { + s_Instance = inst; + Save(inst); + } + } + + return s_Instance; + } + } + + /// + /// Saves instance to an editor database. + /// Only applicable while in the editor. + /// + public static void Save() + { + // Only applicable while in the editor. +#if UNITY_EDITOR + Save(Instance); +#endif + } + + /// + /// // Only applicable while in the editor. + /// + static void Save(T asset) + { + // Only applicable while in the editor. +#if UNITY_EDITOR + var path = asset.SettingsFilePath; + var directory = Path.GetDirectoryName(path); + + if (directory == null) + throw new InvalidOperationException($"Failed to get directory from package settings path: {path}"); + + if (!Directory.Exists(directory)) + Directory.CreateDirectory(directory); + + CacheDocument(asset); +#endif + } + + static T InitFromCache(T asset) + { + var path = asset.SettingsFilePath; + if (File.Exists(path)) + { + StreamReader reader = null; + try + { + reader = new StreamReader(path); + var text = reader.ReadToEnd(); + return JsonUtility.FromJson(text); + } + finally + { + reader?.Close(); + } + } + + return null; + } + + static Task CacheDocument(T asset) + { + var path = asset.SettingsFilePath; + return Task.Run(() => { + try + { + File.WriteAllText(path, JsonUtility.ToJson(asset)); + } + catch (Exception e) + { + Debug.LogException(e); + } + finally + { + Debug.Log($"Personal project settings saved: {path}"); + } + }); + } + } +} \ No newline at end of file diff --git a/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs.meta b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs.meta new file mode 100644 index 0000000..1182811 --- /dev/null +++ b/com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 627a9eeb89564029baf67e75595a948a +timeCreated: 1614852434 \ No newline at end of file