| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine; | 
| using System.Text; | 
| using System; | 
| using System.Text.RegularExpressions; | 
| /// <summary> | 
| /// 服务端Setting配置,存储一些快捷设置,目前长度为100 | 
| /// </summary> | 
|   | 
| public class QuickSetting : Singleton<QuickSetting> | 
| { | 
|     public QuickSetting() | 
|     { | 
|         quickSettingDic.Add(QuickSettingType.Skill, new QuickSettingRange(QuickSettingType.Skill, 0, 30, 3)); | 
|         quickSettingDic.Add(QuickSettingType.SkillMatchPage, new QuickSettingRange(QuickSettingType.SkillMatchPage, 30, 1)); | 
|         quickSettingDic.Add(QuickSettingType.AutoUseXp, new QuickSettingRange(QuickSettingType.AutoUseXp, 31, 1)); | 
|         quickSettingDic.Add(QuickSettingType.SkillMatch, new QuickSettingRange(QuickSettingType.SkillMatch, 32, 45, 1)); | 
|         quickSettingDic.Add(QuickSettingType.GatherSoulAutoResolve, new QuickSettingRange(QuickSettingType.GatherSoulAutoResolve, 80, 1)); | 
|         setting = new string(UCharacter, 100); | 
|         DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += BeforePlayerDataInitializeEvent; | 
|     } | 
|   | 
|     public Dictionary<QuickSettingType, QuickSettingRange> quickSettingDic = new Dictionary<QuickSettingType, QuickSettingRange>(); | 
|     public const char UCharacter = '*'; | 
|     private string setting; | 
|     private string cacheSetting; | 
|     private StringBuilder textBuilder = new StringBuilder(); | 
|   | 
|     public event Action onQuickSettingUpdate; | 
|   | 
|     public void SetQuickSetting<T>(QuickSettingType type, T value, int index = 0) where T : struct | 
|     { | 
|         SetQuickSetting(type, value.ToString(), index); | 
|     } | 
|     public void SetQuickSetting(QuickSettingType type, bool value, int index = 0) | 
|     { | 
|         SetQuickSetting<int>(type, value ? 1 : 0, index); | 
|     } | 
|   | 
|     public void SetQuickSetting(int type, string value, int index) | 
|     { | 
|         SetQuickSetting((QuickSettingType)type, value, index); | 
|     } | 
|   | 
|     public void SetQuickSetting(QuickSettingType type, string value, int index) | 
|     {  | 
|         var quickRange = quickSettingDic[type];  | 
|   | 
|         if (type == QuickSettingType.Skill && quickRange.single > value.Length && !string.IsNullOrEmpty(value)) | 
|         { | 
|             //技能的value 补充* 使其长度为3 | 
|             value = StringUtility.Contact(value, new string(UCharacter, quickRange.single - value.Length)); | 
|         } | 
|   | 
|         if ((index - quickRange.index) * quickRange.single + value.Length > quickRange.length || value.Length > quickRange.single) | 
|         { | 
|             DebugEx.LogErrorFormat("存储的数据超过最大长度!{0}", type); | 
|             return; | 
|         } | 
|         textBuilder.Length = 0; | 
|         var start = quickRange.index + index * quickRange.single; | 
|         var end = start + value.Length; | 
|         if (quickRange.length == quickRange.single) | 
|         { | 
|             end = start + quickRange.length; | 
|         } | 
|         textBuilder.Append(setting, 0, start); | 
|         textBuilder.Append(value); | 
|         if (quickRange.length == quickRange.single | 
|             && quickRange.length > value.Length) | 
|         { | 
|             textBuilder.Append('*', quickRange.length - value.Length); | 
|         } | 
|         textBuilder.Append(setting, end, setting.Length - end); | 
|         setting = textBuilder.ToString(); | 
|         PlayerDatas.Instance.baseData.Setting = setting; | 
|     } | 
|     public void SetQuickSetting(string value) | 
|     { | 
|         value = UIHelper.ServerStringTrim(value); | 
|         if (value != null && value.Length > 0) | 
|         { | 
|             setting = StringUtility.Contact(value, setting.Substring(value.Length, setting.Length - value.Length)); | 
|             cacheSetting = setting; | 
|             PlayerDatas.Instance.baseData.Setting = setting; | 
|         } | 
|         PlayerDatas.Instance.skill.SetQuickSkills(); | 
|   | 
|         if (onQuickSettingUpdate != null) | 
|         { | 
|             onQuickSettingUpdate(); | 
|         } | 
|     } | 
|   | 
|     public T GetQuickSettingValue<T>(QuickSettingType type, int index) where T : struct | 
|     { | 
|         string destinationStr = GetQuickSetting(type, index); | 
|         if (string.IsNullOrEmpty(destinationStr)) | 
|         { | 
|             return default(T); | 
|         } | 
|         return (T)Convert.ChangeType(destinationStr, typeof(T)); | 
|     } | 
|     public string GetQuickSetting(QuickSettingType type, int index) | 
|     { | 
|         var quickRange = quickSettingDic[type]; | 
|         var start = quickRange.index + index * quickRange.single; | 
|         var destinationStr = setting.Substring(start, quickRange.single); | 
|         destinationStr = Regex.Replace(destinationStr, @"\*", string.Empty); | 
|         return destinationStr; | 
|     } | 
|     public bool GetQuickSettingBool(QuickSettingType type, int index, bool _default = false) | 
|     { | 
|         string destinationStr = GetQuickSetting(type, index); | 
|         if (string.IsNullOrEmpty(destinationStr)) | 
|         { | 
|             return _default; | 
|         } | 
|         int _value = _default ? 1 : 0; | 
|         int.TryParse(destinationStr, out _value); | 
|         return _value == 1; | 
|     } | 
|   | 
|     public string GetQuickSetting(int type, int index) | 
|     { | 
|         return GetQuickSetting((QuickSettingType)type, index); | 
|     } | 
|   | 
|     public void SendPackage() | 
|     { | 
|         if (cacheSetting != setting) | 
|         { | 
|             cacheSetting = setting; | 
|             C0303_tagCSetShutCutData vNetData = new C0303_tagCSetShutCutData(); | 
|             vNetData.Setting = setting; | 
|             GameNetSystem.Instance.SendInfo(vNetData); | 
|         } | 
|     } | 
|   | 
|     private void BeforePlayerDataInitializeEvent() | 
|     { | 
|         setting = null; | 
|         setting = new string(UCharacter, 100); | 
|         PlayerDatas.Instance.baseData.Setting = setting; | 
|     } | 
|   | 
|     public enum QuickSettingType | 
|     { | 
|         Skill, | 
|         SkillMatchPage, | 
|         AutoUseXp, | 
|         SkillMatch, | 
|         GatherSoulAutoResolve | 
|     } | 
|     public struct QuickSettingRange | 
|     { | 
|         public QuickSettingType type; | 
|         public int index; | 
|         public int length; | 
|         public int single; | 
|   | 
|         public QuickSettingRange(QuickSettingType type, int start, int length, int single = 0) | 
|         { | 
|             this.type = type; | 
|             this.index = start; | 
|             this.length = length; | 
|             this.single = single == 0 ? length : single; | 
|         } | 
|     } | 
| } |