diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefs.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefs.cs new file mode 100644 index 00000000..253cd5f4 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefs.cs @@ -0,0 +1,44 @@ +using AxibugEmuOnline.Client.ClientCore; + +public static class AxiPlayerPrefs +{ + //#if UNITY_SWITCH && !UNITY_EDITOR + // public static string SaveDataRootDirPath = "save:/axibug"; + //#elif UNITY_PSP2 && !UNITY_EDITOR + // public static string SaveDataRootDirPath = "ux0:data/axibug"; + //#else + // public static string SaveDataRootDirPath = UnityEngine.Application.persistentDataPath; + //#endif + + //使用统一的平台宏区分目录 + public static string SaveDataRootDirPath => App.PersistentDataRootPath(); + + static IAxiPlayerPrefs m_axiPlayerPrefs; + static IAxiPlayerPrefs axiPlayerPrefs + { + get + { + if (m_axiPlayerPrefs == null) + { +#if UNITY_SWITCH || UNITY_PSP2 + m_axiPlayerPrefs = new AxiPlayerPrefsForFileSystem(); +#else + m_axiPlayerPrefs = new AxiPlayerPrefsForUnity(); +#endif + } + return m_axiPlayerPrefs; + } + } + + public static float GetFloat(string key) { return axiPlayerPrefs.GetFloat(key); } + public static void SetFloat(string key, float value) { axiPlayerPrefs.SetFloat(key, value); } + public static float GetFloat(string key, float defaultValue) { return axiPlayerPrefs.GetFloat(key, defaultValue); } + public static int GetInt(string key) { return axiPlayerPrefs.GetInt(key); } + public static void SetInt(string key, int value) { axiPlayerPrefs.SetInt(key, value); } + public static int GetInt(string key, int defaultValue) { return axiPlayerPrefs.GetInt(key, defaultValue); } + public static string GetString(string key) { return axiPlayerPrefs.GetString(key); } + public static void SetString(string key, string value) { axiPlayerPrefs.SetString(key, value); } + public static string GetString(string key, string defaultValue) { return axiPlayerPrefs.GetString(key, defaultValue); } + + internal static void DeleteAll() { axiPlayerPrefs.DeleteAll(); } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefs.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefs.cs.meta new file mode 100644 index 00000000..5d162739 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefs.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 71c6edecb09ea914b90fa115b491698d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsFileBase.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsFileBase.cs new file mode 100644 index 00000000..c2ed6ed5 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsFileBase.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public abstract class AxiPlayerPrefsFileBase : IAxiPlayerPrefs +{ + protected static string AxiPlayerPrefsFilePath => AxiPlayerPrefs.SaveDataRootDirPath + "/AxiPlayerPrefs.dat"; + + Dictionary m_keyval = new Dictionary(); + Func> m_LoadFunc; + Action> m_SaveFunc; + bool bDirty = false; + + [Serializable] + public class AxiPlayerPrefsAllData + { + public int version; + public List datalist; + } + + [Serializable] + public class AxiPlayerPrefsKeyValye + { + public string key; + public int intval; + public string strval; + public float floatval; + } + + public AxiPlayerPrefsFileBase(Func> load, Action> save) + { + m_LoadFunc = load; + m_SaveFunc = save; + Load(); + AxiPlayerPrefsMono.SetInvoke(Save, 15); + } + public static Dictionary JsonStrToData(string dataStr) + { + AxiPlayerPrefsAllData alldata = UnityEngine.JsonUtility.FromJson(dataStr); + Dictionary data = new Dictionary(); + foreach (var item in alldata.datalist) + { + data.Add(item.key, item); + } + return data; + } + + public static string DataToJsonStr(Dictionary data) + { + return UnityEngine.JsonUtility.ToJson(new AxiPlayerPrefsAllData() { version = 1, datalist = data.Values.ToList() }); + } + + AxiPlayerPrefsKeyValye GetByKey(string key, bool NonAutoCreate, out bool IsNew) + { + //Debug.Log($"GetByKey=>{key}"); + if (!m_keyval.ContainsKey(key)) + { + IsNew = true; + if (!NonAutoCreate) + return null; + m_keyval.Add(key, new AxiPlayerPrefsKeyValye() { key = key }); + } + else + IsNew = false; + return m_keyval[key]; + } + + public void Load() + { + m_keyval = m_LoadFunc.Invoke(); + } + + public void Save() + { + if (bDirty) + { + Debug.Log("Auto AxiPlayerPrefs."); + bDirty = false; + m_SaveFunc.Invoke(m_keyval); + } + } + + public float GetFloat(string key, float defaultValue) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool IsNew); + if (IsNew) + kv.floatval = defaultValue; + return kv.floatval; + } + + public int GetInt(string key, int defaultValue) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool IsNew); + if (IsNew) + kv.intval = defaultValue; + return kv.intval; + } + + public string GetString(string key, string defaultValue) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool IsNew); + if (IsNew) + kv.strval = defaultValue; + return kv.strval; + } + + public float GetFloat(string key) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, false, out bool _); + if (kv != null) return kv.floatval; + return default(float); + } + + public int GetInt(string key) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, false, out bool _); + if (kv != null) return kv.intval; + return default(int); + } + + public string GetString(string key) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, false, out bool _); + if (kv != null) return kv.strval; + return string.Empty; + } + + + public void SetInt(string key, int value) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool _); + if (kv.intval == value) + return; + kv.intval = value; + bDirty = true; + } + + public void SetString(string key, string value) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool _); + if (string.Equals(kv.strval, value)) + return; + kv.strval = value; + bDirty = true; + } + + public void SetFloat(string key, float value) + { + AxiPlayerPrefsKeyValye kv = GetByKey(key, true, out bool _); + if (kv.floatval == value) + return; + kv.floatval = value; + bDirty = true; + } + public void DeleteAll() + { + m_keyval.Clear(); + bDirty = true; + } + +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsFileBase.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsFileBase.cs.meta new file mode 100644 index 00000000..cb7150bc --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsFileBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4085aa85cd82706448a25a172cd681a0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForFileSystem.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForFileSystem.cs new file mode 100644 index 00000000..afb51f7e --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForFileSystem.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using UnityEngine; + +public class AxiPlayerPrefsForFileSystem : AxiPlayerPrefsFileBase +{ + public AxiPlayerPrefsForFileSystem() : base(LoadData, SaveData) + { + Debug.Log($"AxiPlayerPrefsForPSVita Init"); + } + + public static Dictionary LoadData() + { + if (!AxiIO.AxiIO.io.file_Exists(AxiPlayerPrefsFilePath)) + return new Dictionary(); + else + { + string outputData = string.Empty; + byte[] loadedData = AxiIO.AxiIO.io.file_ReadAllBytes(AxiPlayerPrefsFilePath); + if (loadedData != null && loadedData.Length != 0) + { + using (System.IO.MemoryStream stream = new System.IO.MemoryStream(loadedData)) + { + using (System.IO.BinaryReader reader = new System.IO.BinaryReader(stream)) + { + outputData = reader.ReadString(); + } + } + } + if (string.IsNullOrEmpty(outputData)) + return new Dictionary(); + return AxiPlayerPrefsFileBase.JsonStrToData(outputData); + } + } + + public static void SaveData(Dictionary data) + { + string jsonStr = AxiPlayerPrefsFileBase.DataToJsonStr(data); + byte[] dataByteArray; + using (System.IO.MemoryStream stream = new System.IO.MemoryStream(jsonStr.Length * sizeof(char))) + { + System.IO.BinaryWriter binaryWriter = new System.IO.BinaryWriter(stream); + binaryWriter.Write(jsonStr); + dataByteArray = stream.GetBuffer(); + stream.Close(); + } + AxiIO.AxiIO.io.file_WriteAllBytes(AxiPlayerPrefsFilePath, dataByteArray, false); + } + +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForUnity.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForUnity.cs new file mode 100644 index 00000000..0a3eb278 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForUnity.cs @@ -0,0 +1,55 @@ + +using UnityEngine; + +public class AxiPlayerPrefsForUnity : IAxiPlayerPrefs +{ + public string GetString(string key, string defaultValue) + { + return PlayerPrefs.GetString(key, defaultValue); + } + + public void SetString(string key, string value) + { + PlayerPrefs.SetString(key, value); + } + + public int GetInt(string key, int defaultValue) + { + return PlayerPrefs.GetInt(key, defaultValue); + } + + public void SetInt(string key, int value) + { + PlayerPrefs.SetInt(key, value); + } + + public float GetFloat(string key, float defaultValue) + { + return PlayerPrefs.GetFloat(key, defaultValue); + } + + public float GetFloat(string key) + { + return PlayerPrefs.GetFloat(key); + } + + public int GetInt(string key) + { + return PlayerPrefs.GetInt(key); + } + + public string GetString(string key) + { + return PlayerPrefs.GetString(key); + } + + public void SetFloat(string key, float value) + { + PlayerPrefs.SetFloat(key,value); + } + + public void DeleteAll() + { + PlayerPrefs.DeleteAll(); + } +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForUnity.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForUnity.cs.meta new file mode 100644 index 00000000..c9af0db7 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsForUnity.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 94efbbb58793f1146b395c1ebfe19d33 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsMono.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsMono.cs new file mode 100644 index 00000000..264692de --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsMono.cs @@ -0,0 +1,39 @@ +using System; +using UnityEngine; + +public class AxiPlayerPrefsMono : MonoBehaviour +{ + Action act; + float waittime; + float lastinvokeTime; + public static void SetInvoke(Action _act, int _waitsec) + { + GameObject gobj = GameObject.Find($"[{nameof(AxiPlayerPrefsMono)}]"); + if (gobj == null) + { + gobj = new GameObject(); + gobj.name = $"[{nameof(AxiPlayerPrefsMono)}]"; + GameObject.DontDestroyOnLoad(gobj); + } + AxiPlayerPrefsMono com = gobj.GetComponent(); + if (com == null) + { + com = gobj.AddComponent(); + } + com.act = _act; + com.waittime = _waitsec; + } + + public void OnEnable() + { + Debug.Log("AxiPlayerPrefsMono Enable"); + } + public void Update() + { + if (Time.time - lastinvokeTime < waittime) + return; + lastinvokeTime = Time.time; + if (act != null) + act.Invoke(); + } +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsMono.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsMono.cs.meta new file mode 100644 index 00000000..9946b52c --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/AxiPlayerPrefsMono.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8fff109e9498ef549a7d124a24894a98 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/IAxiPlayerPrefs.cs b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/IAxiPlayerPrefs.cs new file mode 100644 index 00000000..b12e57ce --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/IAxiPlayerPrefs.cs @@ -0,0 +1,13 @@ +public interface IAxiPlayerPrefs +{ + void DeleteAll(); + float GetFloat(string key, float defaultValue); + float GetFloat(string key); + int GetInt(string key, int defaultValue); + int GetInt(string key); + string GetString(string key); + string GetString(string key, string defaultValue); + void SetFloat(string key, float value); + void SetInt(string key, int value); + void SetString(string key, string value); +} \ No newline at end of file diff --git a/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/IAxiPlayerPrefs.cs.meta b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/IAxiPlayerPrefs.cs.meta new file mode 100644 index 00000000..3fec03e7 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/AppMain/AxiPlayerPrefs/IAxiPlayerPrefs.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e194ccb9ac1dc984da8922b7c67ec879 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: