diff --git a/Assets/TEngine/Scripts/Editor/Inspector/SettingComponentInspector.cs b/Assets/TEngine/Scripts/Editor/Inspector/SettingComponentInspector.cs new file mode 100644 index 00000000..3ae989bd --- /dev/null +++ b/Assets/TEngine/Scripts/Editor/Inspector/SettingComponentInspector.cs @@ -0,0 +1,75 @@ +using UnityEditor; +using UnityEngine; +using TEngine.Runtime; + +namespace TEngine.Editor +{ + [CustomEditor(typeof(SettingComponent))] + internal sealed class SettingComponentInspector : TEngineInspector + { + private HelperInfo m_SettingHelperInfo = new HelperInfo("Setting"); + + public override void OnInspectorGUI() + { + base.OnInspectorGUI(); + + SettingComponent t = (SettingComponent)target; + + EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode); + { + m_SettingHelperInfo.Draw(); + } + EditorGUI.EndDisabledGroup(); + + if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject)) + { + EditorGUILayout.LabelField("Setting Count", t.Count >= 0 ? t.Count.ToString() : ""); + if (t.Count > 0) + { + string[] settingNames = t.GetAllSettingNames(); + foreach (string settingName in settingNames) + { + EditorGUILayout.LabelField(settingName, t.GetString(settingName)); + } + } + } + + if (EditorApplication.isPlaying) + { + if (GUILayout.Button("Save Settings")) + { + t.Save(); + } + + if (GUILayout.Button("Remove All Settings")) + { + t.RemoveAllSettings(); + } + } + + serializedObject.ApplyModifiedProperties(); + + Repaint(); + } + + protected override void OnCompileComplete() + { + base.OnCompileComplete(); + + RefreshTypeNames(); + } + + private void OnEnable() + { + m_SettingHelperInfo.Init(serializedObject); + + RefreshTypeNames(); + } + + private void RefreshTypeNames() + { + m_SettingHelperInfo.Refresh(); + serializedObject.ApplyModifiedProperties(); + } + } +} \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Editor/Inspector/SettingComponentInspector.cs.meta b/Assets/TEngine/Scripts/Editor/Inspector/SettingComponentInspector.cs.meta new file mode 100644 index 00000000..3b6a7bac --- /dev/null +++ b/Assets/TEngine/Scripts/Editor/Inspector/SettingComponentInspector.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 00d16a46d09c4eb9afeae332d16b0b0b +timeCreated: 1664442035 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSetting.cs b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSetting.cs new file mode 100644 index 00000000..128a25d1 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSetting.cs @@ -0,0 +1,305 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; + +namespace TEngine.Runtime +{ + /// + /// 默认游戏配置。 + /// + public sealed class DefaultSetting + { + private readonly SortedDictionary m_Settings = new SortedDictionary(StringComparer.Ordinal); + + /// + /// 初始化本地版本资源列表的新实例。 + /// + public DefaultSetting() + { + } + + /// + /// 获取游戏配置项数量。 + /// + public int Count + { + get + { + return m_Settings.Count; + } + } + + /// + /// 获取所有游戏配置项的名称。 + /// + /// 所有游戏配置项的名称。 + public string[] GetAllSettingNames() + { + int index = 0; + string[] allSettingNames = new string[m_Settings.Count]; + foreach (KeyValuePair setting in m_Settings) + { + allSettingNames[index++] = setting.Key; + } + + return allSettingNames; + } + + /// + /// 获取所有游戏配置项的名称。 + /// + /// 所有游戏配置项的名称。 + public void GetAllSettingNames(List results) + { + if (results == null) + { + throw new Exception("Results is invalid."); + } + + results.Clear(); + foreach (KeyValuePair setting in m_Settings) + { + results.Add(setting.Key); + } + } + + /// + /// 检查是否存在指定游戏配置项。 + /// + /// 要检查游戏配置项的名称。 + /// 指定的游戏配置项是否存在。 + public bool HasSetting(string settingName) + { + return m_Settings.ContainsKey(settingName); + } + + /// + /// 移除指定游戏配置项。 + /// + /// 要移除游戏配置项的名称。 + /// 是否移除指定游戏配置项成功。 + public bool RemoveSetting(string settingName) + { + return m_Settings.Remove(settingName); + } + + /// + /// 清空所有游戏配置项。 + /// + public void RemoveAllSettings() + { + m_Settings.Clear(); + } + + /// + /// 从指定游戏配置项中读取布尔值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的布尔值。 + public bool GetBool(string settingName) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + Log.Warning("Setting '{0}' is not exist.", settingName); + return false; + } + + return int.Parse(value) != 0; + } + + /// + /// 从指定游戏配置项中读取布尔值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的布尔值。 + public bool GetBool(string settingName, bool defaultValue) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + return defaultValue; + } + + return int.Parse(value) != 0; + } + + /// + /// 向指定游戏配置项写入布尔值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的布尔值。 + public void SetBool(string settingName, bool value) + { + m_Settings[settingName] = value ? "1" : "0"; + } + + /// + /// 从指定游戏配置项中读取整数值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的整数值。 + public int GetInt(string settingName) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + Log.Warning("Setting '{0}' is not exist.", settingName); + return 0; + } + + return int.Parse(value); + } + + /// + /// 从指定游戏配置项中读取整数值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的整数值。 + public int GetInt(string settingName, int defaultValue) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + return defaultValue; + } + + return int.Parse(value); + } + + /// + /// 向指定游戏配置项写入整数值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的整数值。 + public void SetInt(string settingName, int value) + { + m_Settings[settingName] = value.ToString(); + } + + /// + /// 从指定游戏配置项中读取浮点数值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的浮点数值。 + public float GetFloat(string settingName) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + Log.Warning("Setting '{0}' is not exist.", settingName); + return 0f; + } + + return float.Parse(value); + } + + /// + /// 从指定游戏配置项中读取浮点数值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的浮点数值。 + public float GetFloat(string settingName, float defaultValue) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + return defaultValue; + } + + return float.Parse(value); + } + + /// + /// 向指定游戏配置项写入浮点数值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的浮点数值。 + public void SetFloat(string settingName, float value) + { + m_Settings[settingName] = value.ToString(); + } + + /// + /// 从指定游戏配置项中读取字符串值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的字符串值。 + public string GetString(string settingName) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + Log.Warning("Setting '{0}' is not exist.", settingName); + return null; + } + + return value; + } + + /// + /// 从指定游戏配置项中读取字符串值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的字符串值。 + public string GetString(string settingName, string defaultValue) + { + string value = null; + if (!m_Settings.TryGetValue(settingName, out value)) + { + return defaultValue; + } + + return value; + } + + /// + /// 向指定游戏配置项写入字符串值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的字符串值。 + public void SetString(string settingName, string value) + { + m_Settings[settingName] = value; + } + + /// + /// 序列化数据。 + /// + /// 目标流。 + public void Serialize(Stream stream) + { + using (BinaryWriter binaryWriter = new BinaryWriter(stream, Encoding.UTF8)) + { + binaryWriter.Write7BitEncodedInt32(m_Settings.Count); + foreach (KeyValuePair setting in m_Settings) + { + binaryWriter.Write(setting.Key); + binaryWriter.Write(setting.Value); + } + } + } + + /// + /// 反序列化数据。 + /// + /// 指定流。 + public void Deserialize(Stream stream) + { + m_Settings.Clear(); + using (BinaryReader binaryReader = new BinaryReader(stream, Encoding.UTF8)) + { + int settingCount = binaryReader.Read7BitEncodedInt32(); + for (int i = 0; i < settingCount; i++) + { + m_Settings.Add(binaryReader.ReadString(), binaryReader.ReadString()); + } + } + } + } +} diff --git a/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSetting.cs.meta b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSetting.cs.meta new file mode 100644 index 00000000..71e85205 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSetting.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 413748acf7fa4e0aa71fdaa185d17ea7 +timeCreated: 1664441250 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSettingHelper.cs b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSettingHelper.cs new file mode 100644 index 00000000..f1d6a301 --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSettingHelper.cs @@ -0,0 +1,382 @@ +using System; +using System.Collections.Generic; +using System.IO; +using UnityEngine; + +namespace TEngine.Runtime +{ + /// + /// 默认游戏配置辅助器。 + /// + public class DefaultSettingHelper : SettingHelperBase + { + private const string SettingFileName = "GameFrameworkSetting.dat"; + + private string m_FilePath = null; + private DefaultSetting m_Settings = null; + + /// + /// 获取游戏配置项数量。 + /// + public override int Count + { + get + { + return m_Settings != null ? m_Settings.Count : 0; + } + } + + /// + /// 获取游戏配置存储文件路径。 + /// + public string FilePath + { + get + { + return m_FilePath; + } + } + + /// + /// 获取游戏配置。 + /// + public DefaultSetting Setting + { + get + { + return m_Settings; + } + } + /// + /// 加载游戏配置。 + /// + /// 是否加载游戏配置成功。 + public override bool Load() + { + try + { + if (!File.Exists(m_FilePath)) + { + return true; + } + + using (FileStream fileStream = new FileStream(m_FilePath, FileMode.Open, FileAccess.Read)) + { + Deserialize(fileStream); + return true; + } + } + catch (Exception exception) + { + Log.Warning("Load settings failure with exception '{0}'.", exception); + return false; + } + } + + /// + /// 保存游戏配置。 + /// + /// 是否保存游戏配置成功。 + public override bool Save() + { + try + { + using (FileStream fileStream = new FileStream(m_FilePath, FileMode.Create, FileAccess.Write)) + { + return Serialize(fileStream, m_Settings); + } + } + catch (Exception exception) + { + Log.Warning("Save settings failure with exception '{0}'.", exception); + return false; + } + } + + /// + /// 获取所有游戏配置项的名称。 + /// + /// 所有游戏配置项的名称。 + public override string[] GetAllSettingNames() + { + return m_Settings.GetAllSettingNames(); + } + + /// + /// 获取所有游戏配置项的名称。 + /// + /// 所有游戏配置项的名称。 + public override void GetAllSettingNames(List results) + { + m_Settings.GetAllSettingNames(results); + } + + /// + /// 检查是否存在指定游戏配置项。 + /// + /// 要检查游戏配置项的名称。 + /// 指定的游戏配置项是否存在。 + public override bool HasSetting(string settingName) + { + return m_Settings.HasSetting(settingName); + } + + /// + /// 移除指定游戏配置项。 + /// + /// 要移除游戏配置项的名称。 + /// 是否移除指定游戏配置项成功。 + public override bool RemoveSetting(string settingName) + { + return m_Settings.RemoveSetting(settingName); + } + + /// + /// 清空所有游戏配置项。 + /// + public override void RemoveAllSettings() + { + m_Settings.RemoveAllSettings(); + } + + /// + /// 从指定游戏配置项中读取布尔值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的布尔值。 + public override bool GetBool(string settingName) + { + return m_Settings.GetBool(settingName); + } + + /// + /// 从指定游戏配置项中读取布尔值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的布尔值。 + public override bool GetBool(string settingName, bool defaultValue) + { + return m_Settings.GetBool(settingName, defaultValue); + } + + /// + /// 向指定游戏配置项写入布尔值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的布尔值。 + public override void SetBool(string settingName, bool value) + { + m_Settings.SetBool(settingName, value); + } + + /// + /// 从指定游戏配置项中读取整数值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的整数值。 + public override int GetInt(string settingName) + { + return m_Settings.GetInt(settingName); + } + + /// + /// 从指定游戏配置项中读取整数值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的整数值。 + public override int GetInt(string settingName, int defaultValue) + { + return m_Settings.GetInt(settingName, defaultValue); + } + + /// + /// 向指定游戏配置项写入整数值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的整数值。 + public override void SetInt(string settingName, int value) + { + m_Settings.SetInt(settingName, value); + } + + /// + /// 从指定游戏配置项中读取浮点数值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的浮点数值。 + public override float GetFloat(string settingName) + { + return m_Settings.GetFloat(settingName); + } + + /// + /// 从指定游戏配置项中读取浮点数值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的浮点数值。 + public override float GetFloat(string settingName, float defaultValue) + { + return m_Settings.GetFloat(settingName, defaultValue); + } + + /// + /// 向指定游戏配置项写入浮点数值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的浮点数值。 + public override void SetFloat(string settingName, float value) + { + m_Settings.SetFloat(settingName, value); + } + + /// + /// 从指定游戏配置项中读取字符串值。 + /// + /// 要获取游戏配置项的名称。 + /// 读取的字符串值。 + public override string GetString(string settingName) + { + return m_Settings.GetString(settingName); + } + + /// + /// 从指定游戏配置项中读取字符串值。 + /// + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认值。 + /// 读取的字符串值。 + public override string GetString(string settingName, string defaultValue) + { + return m_Settings.GetString(settingName, defaultValue); + } + + /// + /// 向指定游戏配置项写入字符串值。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的字符串值。 + public override void SetString(string settingName, string value) + { + m_Settings.SetString(settingName, value); + } + + /// + /// 从指定游戏配置项中读取对象。 + /// + /// 要读取对象的类型。 + /// 要获取游戏配置项的名称。 + /// 读取的对象。 + public override T GetObject(string settingName) + { + return Utility.Json.ToObject(GetString(settingName)); + } + + /// + /// 从指定游戏配置项中读取对象。 + /// + /// 要读取对象的类型。 + /// 要获取游戏配置项的名称。 + /// 读取的对象。 + public override object GetObject(Type objectType, string settingName) + { + return Utility.Json.ToObject(objectType, GetString(settingName)); + } + + /// + /// 从指定游戏配置项中读取对象。 + /// + /// 要读取对象的类型。 + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认对象。 + /// 读取的对象。 + public override T GetObject(string settingName, T defaultObj) + { + string json = GetString(settingName, null); + if (json == null) + { + return defaultObj; + } + + return Utility.Json.ToObject(json); + } + + /// + /// 从指定游戏配置项中读取对象。 + /// + /// 要读取对象的类型。 + /// 要获取游戏配置项的名称。 + /// 当指定的游戏配置项不存在时,返回此默认对象。 + /// 读取的对象。 + public override object GetObject(Type objectType, string settingName, object defaultObj) + { + string json = GetString(settingName, null); + if (json == null) + { + return defaultObj; + } + + return Utility.Json.ToObject(objectType, json); + } + + /// + /// 向指定游戏配置项写入对象。 + /// + /// 要写入对象的类型。 + /// 要写入游戏配置项的名称。 + /// 要写入的对象。 + public override void SetObject(string settingName, T obj) + { + SetString(settingName, Utility.Json.ToJson(obj)); + } + + /// + /// 向指定游戏配置项写入对象。 + /// + /// 要写入游戏配置项的名称。 + /// 要写入的对象。 + public override void SetObject(string settingName, object obj) + { + SetString(settingName, Utility.Json.ToJson(obj)); + } + + private void Awake() + { + m_FilePath = Utility.Path.GetRegularPath(Path.Combine(Application.persistentDataPath, SettingFileName)); + m_Settings = new DefaultSetting(); + } + + private static readonly byte[] Header = new byte[] { (byte)'T', (byte)'E', (byte)'G' }; + + /// 从指定流反序列化数据。 + /// 指定流。 + /// 反序列化的数据。 + private DefaultSetting Deserialize(Stream stream) + { + byte[] header = Header; + byte num1 = (byte) stream.ReadByte(); + byte num2 = (byte) stream.ReadByte(); + byte num3 = (byte) stream.ReadByte(); + if ((int) num1 != (int) header[0] || (int) num2 != (int) header[1] || (int) num3 != (int) header[2]) + throw new Exception(Utility.Text.Format("Header is invalid, need '{0}{1}{2}', current '{3}{4}{5}'.", (char) header[0], (char) header[1], (char) header[2], (char) num1, (char) num2, (char) num3)); + m_Settings.Deserialize(stream); + return m_Settings; + } + + /// 序列化数据到目标流中。 + /// 目标流。 + /// 要序列化的数据。 + /// 是否序列化数据成功。 + private bool Serialize(Stream stream, DefaultSetting data) + { + byte[] header = Header; + stream.WriteByte(header[0]); + stream.WriteByte(header[1]); + stream.WriteByte(header[2]); + m_Settings.Serialize(stream); + return true; + } + } +} diff --git a/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSettingHelper.cs.meta b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSettingHelper.cs.meta new file mode 100644 index 00000000..4b1e7b5f --- /dev/null +++ b/Assets/TEngine/Scripts/Runtime/Core/Setting/DefaultSettingHelper.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 89c691ddcd5c478f9679840ef19f16d3 +timeCreated: 1664441218 \ No newline at end of file diff --git a/Assets/TEngine/Scripts/Runtime/Core/Setting/SettingComponent.cs b/Assets/TEngine/Scripts/Runtime/Core/Setting/SettingComponent.cs index 95538b44..facbd5f4 100644 --- a/Assets/TEngine/Scripts/Runtime/Core/Setting/SettingComponent.cs +++ b/Assets/TEngine/Scripts/Runtime/Core/Setting/SettingComponent.cs @@ -13,6 +13,7 @@ namespace TEngine.Runtime { private SettingManager m_SettingManager = null; + [SerializeField] private string m_SettingHelperTypeName = "TEngine.Runtime.DefaultSettingHelper"; [SerializeField] @@ -35,30 +36,29 @@ namespace TEngine.Runtime public override void Awake() { base.Awake(); + + SettingHelperBase settingHelper = null; + settingHelper = Helper.CreateHelper(m_SettingHelperTypeName, m_CustomSettingHelper); + + if (settingHelper == null) + { + Log.Error("Can not create setting helper."); + return; + } + else + { + settingHelper.gameObject.transform.SetParent(gameObject.transform); + } + m_SettingManager = SettingManager.Instance; + if (m_SettingManager == null) { Log.Fatal("Setting manager is invalid."); return; } - SettingHelperBase settingHelper = null; - - if (m_CustomSettingHelper == null) - { - settingHelper = Helper.CreateHelper(m_SettingHelperTypeName, m_CustomSettingHelper); - if (settingHelper == null) - { - Log.Error("Can not create setting helper."); - return; - } - } - else - { - settingHelper = UnityEngine.Object.FindObjectOfType(); - } - if (settingHelper != null) { m_SettingManager.SetSettingHelper(settingHelper);