generated from StansAssets/Unity-Package-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Local project settings singleton #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lytvynenko-Danylo
wants to merge
3
commits into
master
Choose a base branch
from
feat/local-project-settings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Plugin package name. | ||
| /// </summary> | ||
| public string PackageName { get; } | ||
|
|
||
| /// <summary> | ||
| /// Plugin settings folder path. | ||
| /// </summary> | ||
| public string SettingsFolderPath => $"{Application.dataPath}/{PackagesConfig.LibraryPath}/{PackageName}"; | ||
|
|
||
| /// <summary> | ||
| /// Plugin settings file path. | ||
| /// </summary> | ||
| public string SettingsFilePath => $"{SettingsFolderPath}/{SettingsFileName}.json"; | ||
|
|
||
| /// <summary> | ||
| /// Settings file name. | ||
| /// </summary> | ||
| public string SettingsFileName { get; } | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettings.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
110 changes: 110 additions & 0 deletions
110
com.stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using UnityEngine; | ||
|
|
||
| namespace StansAssets.Plugins | ||
| { | ||
| public abstract class LocalProjectSettingsSingleton<T> : LocalProjectSettings where T : LocalProjectSettings, new() | ||
| { | ||
| static T s_Instance; | ||
|
|
||
| /// <summary> | ||
| /// 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 <see cref="PackagesConfig.SettingsPath" /> location | ||
| /// </summary> | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Saves instance to an editor database. | ||
| /// Only applicable while in the editor. | ||
| /// </summary> | ||
| public static void Save() | ||
| { | ||
| // Only applicable while in the editor. | ||
| #if UNITY_EDITOR | ||
| Save(Instance); | ||
| #endif | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// // Only applicable while in the editor. | ||
| /// </summary> | ||
| 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<T>(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}"); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } |
3 changes: 3 additions & 0 deletions
3
...stansassets.plugins-dev-kit/Runtime/PackageSettings/LocalProjectSettingsSingleton.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need it and where do we use it?
Plus seems like we should have
ILocalProjectSettings&StansAssetsLocalProjectSettingsthat holds basic path implementations like$"{Application.dataPath}/{PackagesConfig.LibraryPath}/{PackageName}";ets.