Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ public static class PackagesConfig
/// Package runtime settings location path.
/// </summary>
public const string SettingsPath = "Assets/Plugins/StansAssets/Settings";

/// <summary>
/// Library settings folder path.
/// </summary>
public const string LibraryPath = "Library/StansAssets";
}
}
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;
}
Comment on lines +15 to +19
Copy link
Contributor

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 & StansAssetsLocalProjectSettings that holds basic path implementations like $"{Application.dataPath}/{PackagesConfig.LibraryPath}/{PackageName}"; ets.


/// <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; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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}");
}
});
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.