| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410 |
- using System;
- using UnityEngine;
- namespace ET.PackageManager.Editor
- {
- /// 用于本地小数据量记录的playerPre的包装,用于方便把单个设置项直接当做变量使用
- /// 使用这个,可以避免读取时每次都调用PlayerPrefs
- /// 另外集成了以groupKey(比如userid)来分组数据
- public struct Prefs<T>
- {
- private T m_value;
- private string m_key;
- private string m_valueKey;
- private RwFlag m_rwFlag;
- private int m_groupKeyVer;
- private IGroupKey m_groupKey;
- private IPrefsAccessor m_accessor;
- private T m_defValue;
- public Prefs(string key, IGroupKey groupKey, T defValue, IPrefsAccessor accessor)
- {
- m_defValue = defValue;
- m_value = defValue;
- m_groupKey = groupKey;
- m_accessor = accessor;
- m_groupKeyVer = int.MinValue;
- m_rwFlag = RwFlag.None;
- m_valueKey = key;
- m_key = key;
- UpdateKey();
- }
- private void UpdateKey(bool notWrite = false)
- {
- if (m_groupKey == null)
- {
- return;
- }
- var curVer = m_groupKey.Version;
- if (curVer != m_groupKeyVer)
- {
- m_groupKeyVer = curVer;
- var oldKey = m_key;
- m_key = StrUtil.Concat(m_groupKey.Key, "|", m_valueKey);
- if (m_rwFlag.Has(RwFlag.Write))
- {
- if (oldKey != m_key)
- {
- //如果之前已经有写入,则需要删除旧的,写入新的
- PlayerPrefs.DeleteKey(oldKey);
- if (!notWrite)
- {
- m_accessor.Set(m_key, m_value);
- }
- }
- }
- //清除读取标记
- m_rwFlag.UnmarkSelf(RwFlag.Read);
- }
- }
- public T Value
- {
- get
- {
- UpdateKey();
- //如果即没有读过,也没有写过
- if (!m_rwFlag.Overlaps(RwFlag.Full))
- {
- if (m_accessor.HasKey(m_key))
- {
- m_value = m_accessor.Get(m_key);
- }
- else if (m_groupKey != null && m_accessor.HasKey(m_valueKey))
- {
- //第一次时,如果定向的值拿不到,就去尝试拿全局的值
- m_value = m_accessor.Get(m_valueKey);
- }
- else
- {
- m_value = m_defValue;
- }
- m_rwFlag.MarkSelf(RwFlag.Read);
- }
- return m_value;
- }
- set
- {
- UpdateKey();
- if (m_rwFlag.Overlaps(RwFlag.Full) && value.Equals(m_value))
- {
- return;
- }
- m_value = value;
- m_rwFlag |= RwFlag.Write;
- m_accessor.Set(m_key, value);
- }
- }
- public void Delete()
- {
- UpdateKey(true);
- m_groupKeyVer = int.MinValue;
- m_value = default;
- m_rwFlag.MarkSelf(RwFlag.Write);
- PlayerPrefs.DeleteKey(m_key);
- }
- public interface IPrefsAccessor
- {
- bool HasKey(string key);
- T Get(string key);
- void Set(string key, T value);
- }
- }
- public struct IntPrefs
- {
- private Prefs<int> m_value;
- public int Value
- {
- get => m_value.Value;
- set => m_value.Value = value;
- }
- public IntPrefs(string key, IGroupKey groupKey = null, int defValue = 0)
- {
- m_value = new Prefs<int>(key, groupKey, defValue, IntAccessor.Inst);
- }
- public void Delete()
- {
- m_value.Delete();
- }
- public static implicit operator int(IntPrefs value)
- {
- return value.Value;
- }
- }
- public struct FloatPrefs
- {
- private Prefs<float> m_value;
- public float Value
- {
- get => m_value.Value;
- set => m_value.Value = value;
- }
- public FloatPrefs(string key, IGroupKey groupKey = null, float defValue = 0f)
- {
- m_value = new Prefs<float>(key, groupKey, defValue, FloatAccessor.Inst);
- }
- public void Delete()
- {
- m_value.Delete();
- }
- public static implicit operator float(FloatPrefs value)
- {
- return value.Value;
- }
- }
- public struct StringPrefs
- {
- private Prefs<string> m_value;
- public string Value
- {
- get => m_value.Value;
- set => m_value.Value = value;
- }
- public StringPrefs(string key, IGroupKey groupKey = null, string defValue = null)
- {
- m_value = new Prefs<string>(key, groupKey, defValue, StrAccessor.Inst);
- }
- public void Delete()
- {
- m_value.Delete();
- }
- public static implicit operator string(StringPrefs value)
- {
- return value.Value;
- }
- }
- public struct BoolPrefs
- {
- private Prefs<int> m_value;
- public bool Value
- {
- get => m_value.Value == 1;
- set => m_value.Value = value ? 1 : 0;
- }
- public BoolPrefs(string key, IGroupKey groupKey = null, bool defValue = false)
- {
- m_value = new Prefs<int>(key, groupKey, defValue ? 1 : 0, IntAccessor.Inst);
- }
- public void Delete()
- {
- m_value.Delete();
- }
- public static implicit operator bool(BoolPrefs value)
- {
- return value.Value;
- }
- }
- public struct ArrPrefs<T> where T : IComparable
- {
- private Prefs<string> m_value;
- public ArrPrefs(string key, IGroupKey groupKey = null)
- {
- m_value = new Prefs<string>(key, groupKey, "", StrAccessor.Inst);
- }
- public T[] Get()
- {
- string value = m_value.Value;
- if (string.IsNullOrEmpty(value))
- {
- return Array.Empty<T>();
- }
- return StrConv.ToArr<T>(value, StrConv.ArrSplitLv1);
- }
- public void Set(T[] value)
- {
- if (value == null || value.Length < 1)
- {
- m_value.Delete();
- return;
- }
- var sb = SbPool.Get();
- sb.Append(value[0].ToString());
- for (int i = 1; i < value.Length; i++)
- {
- sb.Append(StrConv.ChrArrSplitLv1).Append(value[i].ToString());
- }
- m_value.Value = SbPool.PutAndToStr(sb);
- }
- public void Delete()
- {
- m_value.Delete();
- }
- }
- public struct EnumPrefs<T> where T : Enum
- {
- private Prefs<int> m_value;
- public T Value
- {
- get => (T)Enum.ToObject(typeof(T), m_value.Value);
- set => m_value.Value = Convert.ToInt32(value);
- }
- public EnumPrefs(string key, IGroupKey groupKey = null, T defValue = default)
- {
- m_value = new Prefs<int>(key, groupKey, Convert.ToInt32(defValue), IntAccessor.Inst);
- }
- public void Delete()
- {
- m_value.Delete();
- }
- public static implicit operator T(EnumPrefs<T> value)
- {
- return value.Value;
- }
- }
- public class IntAccessor : Prefs<int>.IPrefsAccessor
- {
- public static readonly IntAccessor Inst = new IntAccessor();
- public bool HasKey(string key)
- {
- return PlayerPrefs.HasKey(key);
- }
- public int Get(string key)
- {
- return PlayerPrefs.GetInt(key);
- }
- public void Set(string key, int value)
- {
- PlayerPrefs.SetInt(key, value);
- }
- }
- public class FloatAccessor : Prefs<float>.IPrefsAccessor
- {
- public static readonly FloatAccessor Inst = new FloatAccessor();
- public bool HasKey(string key)
- {
- return PlayerPrefs.HasKey(key);
- }
- public float Get(string key)
- {
- return PlayerPrefs.GetFloat(key);
- }
- public void Set(string key, float value)
- {
- PlayerPrefs.SetFloat(key, value);
- }
- }
- public class StrAccessor : Prefs<string>.IPrefsAccessor
- {
- public static readonly StrAccessor Inst = new StrAccessor();
- public bool HasKey(string key)
- {
- return PlayerPrefs.HasKey(key);
- }
- public string Get(string key)
- {
- return PlayerPrefs.GetString(key);
- }
- public void Set(string key, string value)
- {
- PlayerPrefs.SetString(key, value);
- }
- }
- /// <summary>
- /// 分组KEY
- /// </summary>
- public interface IGroupKey
- {
- int Version { get; }
- string Key { get; }
- }
- [Flags]
- public enum RwFlag
- {
- None = 0,
- Write = 1,
- Read = 1 << 1,
- Full = Write | Read
- }
- public static class RwFlagExt
- {
- public static RwFlag Mark(this RwFlag owner, RwFlag flags)
- {
- return owner | flags;
- }
- public static RwFlag Unmark(this RwFlag owner, RwFlag flags)
- {
- return owner & (~flags);
- }
- public static void MarkSelf(this ref RwFlag owner, RwFlag flags)
- {
- owner |= flags;
- }
- public static void UnmarkSelf(this ref RwFlag owner, RwFlag flags)
- {
- owner &= (~flags);
- }
- public static bool Has(this RwFlag owner, RwFlag flags)
- {
- return (owner & flags) == flags;
- }
- public static bool Overlaps(this RwFlag owner, RwFlag flags)
- {
- return (owner & flags) != 0;
- }
- }
- }
|