少年修仙传客户端代码仓库
client_Wu Xijin
2019-02-13 4dc88e1540d2fea7635eb2c376da8ec6c141e4be
3335 配置表读取重构。
4个文件已修改
414 ■■■■■ 已修改文件
Core/GameEngine/Model/Config/NPCConfig.cs 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Core/GameEngine/Model/ConfigBase.cs 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Core/GameEngine/Model/ConfigManager.cs 370 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Fight/GameActor/PartialModelResConfig.cs 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
Core/GameEngine/Model/Config/NPCConfig.cs
@@ -305,7 +305,9 @@
                if (preParse)
                {
                    configs[id] = new NPCConfig(line);
                    var config = new NPCConfig(line);
                    configs[id] = config;
                    (config as IConfigPostProcess).OnConfigParseCompleted();
                }
                else
                {
@@ -331,7 +333,9 @@
                    if (preParse)
                    {
                        configs[id] = new NPCConfig(line);
                        var config = new NPCConfig(line);
                        configs[id] = config;
                        (config as IConfigPostProcess).OnConfigParseCompleted();
                    }
                    else
                    {
Core/GameEngine/Model/ConfigBase.cs
@@ -1,38 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;

public interface IConfigPostProcess
{
    void OnConfigParseCompleted();
}
public class ConfigBase
{
    public ConfigBase()
    {
    }
    public ConfigBase(string content)
    {
    }
    public virtual string getKey()
    {
        return string.Empty;
    }
    public virtual void Parse(string content)
    {
    }
    protected static bool IsNumeric(string value)
    {
        return !string.IsNullOrEmpty(value) && Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
    }
}
Core/GameEngine/Model/ConfigManager.cs
@@ -20,24 +20,6 @@
        private set { m_Inited = value; }
    }
    Dictionary<int, Dictionary<string, ConfigBase>> configDictionary = new Dictionary<int, Dictionary<string, ConfigBase>>();
    public void RegisterGlobalEvent()
    {
        SnxxzGame.Instance.RemoveApplicationOutAction(OnApplicationOut);
        SnxxzGame.Instance.AddApplicationOutAction(OnApplicationOut);
    }
    public void LoadPriorBundleConfig()
    {
    }
    public void PreLoadConfigs()
    {
    }
    List<ConfigTask> configTasks = new List<ConfigTask>();
    public IEnumerator Co_LoadConfigs()
    {
@@ -91,49 +73,6 @@
    public void SyncLoadConfigs()
    {
    }
    public bool AllCompleted()
    {
        foreach (var task in configTasks)
        {
            if (task.state != TaskState.ParseSuccess)
            {
                // DesignDebug.LogFormat("未完成是配置解析任务:{0},当前状态:{1}", task.taskName, task.state);
                return false;
            }
        }
        return true;
    }
    public float GetProgress()
    {
        var count = 0;
        foreach (var task in configTasks)
        {
            if (task.state == TaskState.ParseSuccess)
            {
                count++;
            }
        }
        return (float)count / configTasks.Count;
    }
    public int GetTaskCount()
    {
        return configTasks.Count;
    }
    public void ConfigParsePostProcess()
    {
        foreach (var task in configTasks)
        {
            configDictionary[task.token] = task.container;
        }
        inited = true;
    }
    int GetMinWorkingTaskCount()
@@ -204,315 +143,6 @@
            default:
                return 5;
        }
    }
    ConfigTask AddAsyncTask<T>() where T : ConfigBase, new()
    {
        var typeName = typeof(T).Name;
        var path = string.Empty;
        var fileName = typeName.Substring(0, typeName.Length - 6);
        if (AssetSource.refdataFromEditor)
        {
            path = ResourcesPath.CONFIG_FODLER + "/" + fileName + ".txt";
        }
        else
        {
            path = AssetVersionUtility.GetAssetFilePath(StringUtility.Contact("config/", fileName, ".txt"));
        }
        var task = new ConfigTask(typeof(T), path);
        Action<ConfigTask> launch = (ConfigTask _task) => { ReadFile(_task, OnEndReadFile<T>); };
        task.launch = launch;
        configTasks.Add(task);
        return task;
    }
    void StartAsyncTask(ConfigTask _task)
    {
        _task.state = TaskState.Working;
        _task.launch(_task);
    }
    void ReadFile(ConfigTask _task, Action<ConfigTask> _callBack)
    {
        ThreadPool.QueueUserWorkItem(
            (object _obj) =>
            {
                string[] lines = null;
                try
                {
                    lines = File.ReadAllLines(_task.filePath, Encoding.UTF8);
                    _task.state = TaskState.ReadFileSuccess;
                }
                catch (Exception ex)
                {
                    _task.state = TaskState.ReadFileFailure;
                    DebugEx.Log(ex);
                }
                finally
                {
                    _task.contentLines = lines;
                    if (_callBack != null)
                    {
                        _callBack(_task);
                    }
                }
            }
            );
    }
    void AsyncParseConfig<T>(ConfigTask _task, Action<ConfigTask> _callBack) where T : ConfigBase, new()
    {
        ThreadPool.QueueUserWorkItem(
            (object _obj) =>
            {
                try
                {
                    var count = 0;
                    var container = _task.container as Dictionary<string, ConfigBase>;
                    for (int i = 3; i < _task.contentLines.Length; i++)
                    {
                        var newConfig = new T();
                        newConfig.Parse(_task.contentLines[i]);
                        if (newConfig is IConfigPostProcess)
                        {
                            (newConfig as IConfigPostProcess).OnConfigParseCompleted();
                        }
                        container[newConfig.getKey()] = newConfig;
                        count++;
                        if (count >= 500)
                        {
                            count = 0;
                            Thread.Sleep(30);
                        }
                    }
                    _task.state = TaskState.ParseSuccess;
                }
                catch (Exception ex)
                {
                    _task.state = TaskState.ParseFailure;
                    DebugEx.Log(ex);
                }
                finally
                {
                    if (_callBack != null)
                    {
                        _callBack(_task);
                    }
                }
            }
            );
    }
    private void OnEndReadFile<T>(ConfigTask _task) where T : ConfigBase, new()
    {
        if (isPlaying && _task.state == TaskState.ReadFileFailure)
        {
            Thread.Sleep(30);
            ReadFile(_task, OnEndReadFile<T>);
        }
        else
        {
            _task.capacity = Mathf.Max(_task.contentLines.Length - 3, 0);
            _task.container = new Dictionary<string, ConfigBase>(_task.capacity);
            AsyncParseConfig<T>(_task, OnEndParse<T>);
        }
    }
    private void OnEndParse<T>(ConfigTask _task) where T : ConfigBase, new()
    {
        if (isPlaying && _task.state == TaskState.ParseFailure)
        {
            Debug.LogFormat("配置表解析失败:{0}", _task.taskName);
            Thread.Sleep(30);
            ReadFile(_task, OnEndReadFile<T>);
        }
    }
    private void StartSyncTask<T>() where T : ConfigBase, new()
    {
        var typeName = typeof(T).Name;
        var fileName = typeName.Substring(0, typeName.Length - 6);
        var path = string.Empty;
        if (Application.isEditor || AssetSource.refdataFromEditor)
        {
            path = ResourcesPath.CONFIG_FODLER + "/" + fileName + ".txt";
        }
        else
        {
            path = AssetVersionUtility.GetAssetFilePath(StringUtility.Contact("config/", fileName, ".txt"));
        }
        var task = new ConfigTask(typeof(T), path);
        task.contentLines = File.ReadAllLines(path, Encoding.UTF8);
        if (task.contentLines != null && task.contentLines.Length > 3)
        {
            var length = Mathf.Max(task.contentLines.Length - 3, 0);
            task.capacity = length;
            task.container = new Dictionary<string, ConfigBase>(length);
            task.state = TaskState.ReadFileSuccess;
            SyncParseConfig<T>(task);
        }
        else
        {
            task.state = TaskState.ReadFileFailure;
        }
    }
    private void SyncParseConfig<T>(ConfigTask _task) where T : ConfigBase, new()
    {
        try
        {
            for (int i = 3; i < _task.contentLines.Length; i++)
            {
                var newConfig = new T();
                newConfig.Parse(_task.contentLines[i]);
                if (newConfig is IConfigPostProcess)
                {
                    (newConfig as IConfigPostProcess).OnConfigParseCompleted();
                }
                _task.container[newConfig.getKey()] = newConfig;
            }
        }
        catch (Exception ex)
        {
            DebugEx.Log(ex);
        }
        finally
        {
            _task.state = TaskState.ParseSuccess;
            configDictionary[_task.token] = _task.container;
        }
    }
    public T Get<T>(string _dwTemplateID) where T : ConfigBase, new()
    {
        if (string.IsNullOrEmpty(_dwTemplateID))
        {
            return null;
        }
        var token = typeof(T).MetadataToken;
        if (configDictionary.ContainsKey(token) == false)
        {
            DebugEx.LogErrorFormat("not find the config:{0}", token);
            return null;
        }
        ConfigBase config = null;
        var dic = configDictionary[token];
        if (!dic.ContainsKey(_dwTemplateID))
        {
            DebugEx.LogFormat("not find the config:{0},<color=#ff0000ff>ID:{1}</color>", typeof(T).Name, _dwTemplateID);
        }
        else
        {
            config = dic[_dwTemplateID];
        }
        return config as T;
    }
    public T Get<T>(int _dwTemplateID) where T : ConfigBase, new()
    {
        return Get<T>(_dwTemplateID.ToString());
    }
    public bool ContainKey<T>(int _id)
    {
        return ContainKey<T>(_id.ToString());
    }
    public bool ContainKey<T>(string _key)
    {
        if (string.IsNullOrEmpty(_key))
        {
            return false;
        }
        var token = typeof(T).MetadataToken;
        if (configDictionary.ContainsKey(token) == false)
        {
            return false;
        }
        var dic = configDictionary[token];
        return dic.ContainsKey(_key);
    }
    public List<string> GetAllKeys<T>() where T : ConfigBase
    {
        var token = typeof(T).MetadataToken;
        if (!configDictionary.ContainsKey(token))
        {
            DebugEx.LogErrorFormat("not find the dic of config: {0}", typeof(T).Name);
            return null;
        }
        var dicTemplate = configDictionary[token];
        return new List<string>(dicTemplate.Keys);
    }
    public List<T> GetAllValues<T>() where T : ConfigBase, new()
    {
        var token = typeof(T).MetadataToken;
        if (!configDictionary.ContainsKey(token))
        {
            DebugEx.LogErrorFormat("not find the dic of config: {0}", typeof(T).Name);
            return null;
        }
        var configs = new List<T>();
        var dicTemplate = configDictionary[token];
        foreach (var value in dicTemplate.Values)
        {
            configs.Add(value as T);
        }
        return configs;
    }
    class ConfigTask
    {
        public readonly Type type;
        public readonly int token;
        public readonly string taskName;
        public readonly string filePath;
        public Action<ConfigTask> launch;
        public int capacity = 0;
        public string[] contentLines;
        public Dictionary<string, ConfigBase> container;
        public TaskState state = TaskState.None;
        public ConfigTask(Type _type, string _filePath)
        {
            type = _type;
            taskName = type.Name;
            token = type.MetadataToken;
            filePath = _filePath;
        }
    }
    public enum TaskState
    {
        None = 0,
        Working = 1,
        ReadFileSuccess = 2,
        ReadFileFailure = 3,
        ParseFailure = 4,
        ParseSuccess = 5,
    }
    private void OnApplicationOut()
Fight/GameActor/PartialModelResConfig.cs
@@ -2,7 +2,7 @@
using System.Collections.Generic;
public partial class ModelResConfig : ConfigBase, IConfigPostProcess
public partial class ModelResConfig : IConfigPostProcess
{
    private static readonly Dictionary<string, ModelResConfig> suitDict = new Dictionary<string, ModelResConfig>();
    private static readonly Dictionary<string, ModelResConfig> petDict = new Dictionary<string, ModelResConfig>();