Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEditor;
- using ParnassianStudios.General;
- using ParnassianStudios.General.Collections;
- using ParnassianStudios.General.Project;
- namespace ParnassianStudios.GeneralEditor
- {
- /// <summary>
- /// See LocalPrefs class for more information.
- /// </summary>
- /// <seealso cref="LocalPrefs"/>
- [CreateAssetMenu(fileName = "LocalEditorPrefs", menuName = "Local Editor Prefs")]
- public sealed class LocalEditorPrefs : ScriptableObject, ISerializationCallbackReceiver
- {
- /// <summary>
- /// The short path to the default LocalPrefs database, from EditorDefaultResources
- /// </summary>
- public const string PATH_DEFAULT = "ParnassianStudios/LocalEditorPrefs.asset";
- /// <summary>
- /// The full path to the default LocalPrefs database, from Assets
- /// </summary>
- public const string PATH_DEFAULT_FULL = @"Assets/Editor Default Resources/ParnassianStudios/LocalEditorPrefs.asset";
- // self-singleton reference
- private static LocalEditorPrefs _instance;
- private static LocalEditorPrefs instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = (LocalEditorPrefs)EditorGUIUtility.Load(PATH_DEFAULT);
- if (_instance == null)
- _instance = Utilities.ScriptableObjectUtil.CreateAsset<LocalEditorPrefs>(PATH_DEFAULT_FULL, false, false);
- _instance.InitializeAllLookups();
- }
- return _instance;
- }
- }
- // string-keyed collections
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<StringStringKVP> _stringKeyedStrings = new List<StringStringKVP>();
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<StringIntKVP> _stringKeyedInts = new List<StringIntKVP>();
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<StringFloatKVP> _stringKeyedFloats = new List<StringFloatKVP>();
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<StringBoolKVP> _stringKeyedBools = new List<StringBoolKVP>();
- // string-keyed lookups
- [System.NonSerialized]
- private Dictionary<string, string> _stringKeyedStringsLookup;
- [System.NonSerialized]
- private Dictionary<string, int> _stringKeyedIntsLookup;
- [System.NonSerialized]
- private Dictionary<string, float> _stringKeyedFloatsLookup;
- [System.NonSerialized]
- private Dictionary<string, bool> _stringKeyedBoolsLookup;
- // int-keyed collections
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<IntStringKVP> _intKeyedStrings = new List<IntStringKVP>();
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<IntIntKVP> _intKeyedInts = new List<IntIntKVP>();
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<IntFloatKVP> _intKeyedFloats = new List<IntFloatKVP>();
- [SerializeField, ReorderableArray(DisallowFoldout = true, HideElementLabel = true)]
- private List<IntBoolKVP> _intKeyedBools = new List<IntBoolKVP>();
- // int-keyed lookups
- [System.NonSerialized]
- private Dictionary<int, string> _intKeyedStringsLookup;
- [System.NonSerialized]
- private Dictionary<int, int> _intKeyedIntsLookup;
- [System.NonSerialized]
- private Dictionary<int, float> _intKeyedFloatsLookup;
- [System.NonSerialized]
- private Dictionary<int, bool> _intKeyedBoolsLookup;
- // gets called whenever the object is serialized in the editor
- void ISerializationCallbackReceiver.OnBeforeSerialize() { }
- // gets called whenever the object is deserialized in the editor
- void ISerializationCallbackReceiver.OnAfterDeserialize()
- {
- InitializeAllLookups();
- }
- /// <summary>
- /// Will initialize all of the lookup tables in the class. Because these are not
- /// serialized by the Unity serialization system, they need to be reconstructed
- /// whenever the data arrays are changed- this is the reason for both the
- /// serialization callback and runtime initialization method above.
- /// </summary>
- private void InitializeAllLookups()
- {
- InitializeLookup(ref _stringKeyedStringsLookup, _stringKeyedStrings.CastList<StringStringKVP, KVP<string, string>>());
- InitializeLookup(ref _stringKeyedIntsLookup, _stringKeyedInts.CastList<StringIntKVP, KVP<string, int>>());
- InitializeLookup(ref _stringKeyedFloatsLookup, _stringKeyedFloats.CastList<StringFloatKVP, KVP<string, float>>());
- InitializeLookup(ref _stringKeyedBoolsLookup, _stringKeyedBools.CastList<StringBoolKVP, KVP<string, bool>>());
- InitializeLookup(ref _intKeyedStringsLookup, _intKeyedStrings.CastList<IntStringKVP, KVP<int, string>>());
- InitializeLookup(ref _intKeyedIntsLookup, _intKeyedInts.CastList<IntIntKVP, KVP<int, int>>());
- InitializeLookup(ref _intKeyedFloatsLookup, _intKeyedFloats.CastList<IntFloatKVP, KVP<int, float>>());
- InitializeLookup(ref _intKeyedBoolsLookup, _intKeyedBools.CastList<IntBoolKVP, KVP<int, bool>>());
- }
- /// <summary>
- /// Initializes the String-keyed lookup table.
- /// </summary>
- private void InitializeLookup<T1, T2>(ref Dictionary<T1, T2> lookup, IList<KVP<T1, T2>> list)
- {
- if (lookup == null)
- lookup = new Dictionary<T1, T2>();
- else
- lookup.Clear();
- for (int i = 0; i < list.Count; i++)
- lookup[list[i].Key] = list[i].Value;
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasString(string key)
- {
- return instance._stringKeyedStringsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasString(int key)
- {
- return instance._intKeyedStringsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns the string stored for the given key, if any.
- /// </summary>
- public static string GetString(string key, string defaultValue = null)
- {
- if (instance._stringKeyedStringsLookup.ContainsKey(key))
- return instance._stringKeyedStringsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Returns the string stored for the given key, if any.
- /// </summary>
- public static string GetString(int key, string defaultValue = null)
- {
- if (instance._intKeyedStringsLookup.ContainsKey(key))
- return instance._intKeyedStringsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Sets the string value to the given key.
- /// </summary>
- public static void SetString(string key, string value)
- {
- var kvp = instance._stringKeyedStrings.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._stringKeyedStrings.Add(new StringStringKVP(key, value));
- instance._stringKeyedStringsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Sets the string value to the given key.
- /// </summary>
- public static void SetString(int key, string value)
- {
- var kvp = instance._intKeyedStrings.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._intKeyedStrings.Add(new IntStringKVP(key, value));
- instance._intKeyedStringsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasInt(string key)
- {
- return instance._stringKeyedIntsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasInt(int key)
- {
- return instance._intKeyedIntsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns the int stored for the given key, if any.
- /// </summary>
- public static int GetInt(string key, int defaultValue = 0)
- {
- if (instance._stringKeyedIntsLookup.ContainsKey(key))
- return instance._stringKeyedIntsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Returns the int stored for the given key, if any.
- /// </summary>
- public static int GetInt(int key, int defaultValue = 0)
- {
- if (instance._intKeyedIntsLookup.ContainsKey(key))
- return instance._intKeyedIntsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Sets the int value to the given key.
- /// </summary>
- public static void SetInt(string key, int value)
- {
- var kvp = instance._stringKeyedInts.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._stringKeyedInts.Add(new StringIntKVP(key, value));
- instance._stringKeyedIntsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Sets the int value to the given key.
- /// </summary>
- public static void SetInt(int key, int value)
- {
- var kvp = instance._intKeyedInts.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._intKeyedInts.Add(new IntIntKVP(key, value));
- instance._intKeyedIntsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasFloat(string key)
- {
- return instance._stringKeyedFloatsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasFloat(int key)
- {
- return instance._intKeyedFloatsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns the float stored for the given key, if any.
- /// </summary>
- public static float GetFloat(string key, float defaultValue = 0f)
- {
- if (instance._stringKeyedFloatsLookup.ContainsKey(key))
- return instance._stringKeyedFloatsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Returns the float stored for the given key, if any.
- /// </summary>
- public static float GetFloat(int key, float defaultValue = 0f)
- {
- if (instance._intKeyedFloatsLookup.ContainsKey(key))
- return instance._intKeyedFloatsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Sets the float value to the given key.
- /// </summary>
- public static void SetFloat(string key, float value)
- {
- var kvp = instance._stringKeyedFloats.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._stringKeyedFloats.Add(new StringFloatKVP(key, value));
- instance._stringKeyedFloatsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Sets the float value to the given key.
- /// </summary>
- public static void SetFloat(int key, float value)
- {
- var kvp = instance._intKeyedFloats.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._intKeyedFloats.Add(new IntFloatKVP(key, value));
- instance._intKeyedFloatsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasBool(string key)
- {
- return instance._stringKeyedBoolsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns a bool indicating that the key exists in the database.
- /// </summary>
- public static bool HasBool(int key)
- {
- return instance._intKeyedBoolsLookup.ContainsKey(key);
- }
- /// <summary>
- /// Returns the bool stored for the given key, if any.
- /// </summary>
- public static bool GetBool(string key, bool defaultValue = false)
- {
- if (instance._stringKeyedBoolsLookup.ContainsKey(key))
- return instance._stringKeyedBoolsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Returns the bool stored for the given key, if any.
- /// </summary>
- public static bool GetBool(int key, bool defaultValue = false)
- {
- if (instance._intKeyedBoolsLookup.ContainsKey(key))
- return instance._intKeyedBoolsLookup[key];
- return defaultValue;
- }
- /// <summary>
- /// Sets the bool value to the given key.
- /// </summary>
- public static void SetBool(string key, bool value)
- {
- var kvp = instance._stringKeyedBools.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._stringKeyedBools.Add(new StringBoolKVP(key, value));
- instance._stringKeyedBoolsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Sets the bool value to the given key.
- /// </summary>
- public static void SetBool(int key, bool value)
- {
- var kvp = instance._intKeyedBools.FirstOrDefault(t => t.Key.Equals(key));
- if (kvp != null)
- kvp.Value = value;
- else
- instance._intKeyedBools.Add(new IntBoolKVP(key, value));
- instance._intKeyedBoolsLookup[key] = value;
- Save();
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteString(string key)
- {
- // if this even exists
- var index = instance._stringKeyedStrings.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._stringKeyedStrings.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._stringKeyedStringsLookup.ContainsKey(key))
- instance._stringKeyedStringsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteString(int key)
- {
- // if this even exists
- var index = instance._intKeyedStrings.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._intKeyedStrings.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._intKeyedStringsLookup.ContainsKey(key))
- instance._intKeyedStringsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteInt(string key)
- {
- // if this even exists
- var index = instance._stringKeyedInts.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._stringKeyedInts.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._stringKeyedIntsLookup.ContainsKey(key))
- instance._stringKeyedIntsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteInt(int key)
- {
- // if this even exists
- var index = instance._intKeyedInts.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._intKeyedInts.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._intKeyedIntsLookup.ContainsKey(key))
- instance._intKeyedIntsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteFloat(string key)
- {
- // if this even exists
- var index = instance._stringKeyedFloats.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._stringKeyedFloats.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._stringKeyedFloatsLookup.ContainsKey(key))
- instance._stringKeyedFloatsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteFloat(int key)
- {
- // if this even exists
- var index = instance._intKeyedFloats.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._intKeyedFloats.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._intKeyedFloatsLookup.ContainsKey(key))
- instance._intKeyedFloatsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteBool(string key)
- {
- // if this even exists
- var index = instance._stringKeyedBools.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._stringKeyedBools.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._stringKeyedBoolsLookup.ContainsKey(key))
- instance._stringKeyedBoolsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// Deletes the value stored with the given key.
- /// </summary>
- public static void DeleteBool(int key)
- {
- // if this even exists
- var index = instance._intKeyedBools.FindIndex(t => t.Key.Equals(key));
- if (index > -1)
- {
- // update serialized collection
- instance._intKeyedBools.RemoveAt(index);
- // update lookup so we don't need to rebuild the whole thing
- if (instance._intKeyedBoolsLookup.ContainsKey(key))
- instance._intKeyedBoolsLookup.Remove(key);
- Save();
- }
- }
- /// <summary>
- /// WARNING: EXTREMELY DESTRUCTIVE METHOD. Will reset the entire prefs database,
- /// including all lookup tables.
- /// </summary>
- public static void DeleteAll()
- {
- if (EditorUtility.DisplayDialog("Are you absolutely sure?", "DeleteAll is an extremely destructive method, are you sure you'd like to proceed?", "DESTROY", "Cancel"))
- {
- instance._stringKeyedStrings.Clear();
- instance._stringKeyedInts.Clear();
- instance._stringKeyedFloats.Clear();
- instance._stringKeyedBools.Clear();
- instance._intKeyedStrings.Clear();
- instance._intKeyedInts.Clear();
- instance._intKeyedFloats.Clear();
- instance._intKeyedBools.Clear();
- instance._stringKeyedStringsLookup.Clear();
- instance._stringKeyedIntsLookup.Clear();
- instance._stringKeyedFloatsLookup.Clear();
- instance._stringKeyedBoolsLookup.Clear();
- instance._intKeyedStringsLookup.Clear();
- instance._intKeyedIntsLookup.Clear();
- instance._intKeyedFloatsLookup.Clear();
- instance._intKeyedBoolsLookup.Clear();
- Save();
- }
- }
- public static void Save()
- {
- EditorUtility.SetDirty(instance);
- AssetDatabase.SaveAssets();
- }
- // this garbage is required for serializing the lists properly, because reasons
- [System.Serializable]
- private class StringStringKVP : KVP<string, string>
- {
- public StringStringKVP(string key, string value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class StringIntKVP : KVP<string, int>
- {
- public StringIntKVP(string key, int value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class StringFloatKVP : KVP<string, float>
- {
- public StringFloatKVP(string key, float value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class StringBoolKVP : KVP<string, bool>
- {
- public StringBoolKVP(string key, bool value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class IntStringKVP : KVP<int, string>
- {
- public IntStringKVP(int key, string value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class IntIntKVP : KVP<int, int>
- {
- public IntIntKVP(int key, int value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class IntFloatKVP : KVP<int, float>
- {
- public IntFloatKVP(int key, float value)
- : base(key, value) { }
- }
- [System.Serializable]
- private class IntBoolKVP : KVP<int, bool>
- {
- public IntBoolKVP(int key, bool value)
- : base(key, value) { }
- }
- /// <summary>
- /// Base "KeyValuePair" serializable class for prefs collections
- /// </summary>
- [System.Serializable]
- private class KVP<T1, T2> : System.IEquatable<KVP<T1, T2>>
- {
- public T1 Key;
- public T2 Value;
- public KVP(T1 key, T2 value)
- {
- this.Key = key;
- this.Value = value;
- }
- public override int GetHashCode()
- {
- unchecked { return Key.GetHashCode() + Value.GetHashCode(); }
- }
- public override bool Equals(object obj)
- {
- if (typeof(KVP<T1, T2>).IsAssignableFrom(obj.GetType()))
- return this.Equals((KVP<T1, T2>)obj);
- return false;
- }
- public bool Equals(KVP<T1, T2> other)
- {
- return Key.Equals(other.Key) && Value.Equals(other.Value);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment