Example #1
0
    /// <summary>
    /// 填充decoder的内容
    /// </summary>
    /// <param name="fieldInfos"></param>
    /// <param name="gValueDic"></param>
    /// <param name="valueDic"></param>
    /// <param name="configName"></param>
    /// <returns></returns>
    private bool FillDecoder(CfgFieldInfo[] fieldInfos, Dictionary <string, string> gValueDic,
                             Dictionary <string, string> valueDic, string configName)
    {
        //创建CfgDecoder文件
        StringBuilder sb = new StringBuilder(fieldInfos.Length);

        for (int i = 0; i < fieldInfos.Length; i++)
        {
            CfgFieldInfo typeInfo = fieldInfos[i];
            string       funCode;
            if (!m_readValueFunMap.TryGetValue(typeInfo.Type, out funCode))
            {
                m_readValueFunMap.TryGetValue("string", out funCode);
            }
            if (string.IsNullOrEmpty(funCode))
            {
                funCode = "";
            }
            sb.Append("            ");
            sb.Append(string.Format(funCode, typeInfo.KeyName, typeInfo.Name));
            sb.Append(i == (fieldInfos.Length - 1) ? ";" : ";\r\n");
        }
        valueDic["ReadValueCode"] = sb.ToString();
        return(true);
    }
Example #2
0
    /// <summary>
    /// 填充dao文件
    /// 主要处理了languageCode
    /// </summary>
    /// <param name="fieldInfos"></param>
    /// <param name="gValueDic"></param>
    /// <param name="valueDic"></param>
    /// <param name="readTemplateCache"></param>
    /// <param name="configName"></param>
    /// <returns></returns>
    private bool FillDao(CfgFieldInfo[] fieldInfos, Dictionary <string, string> gValueDic,
                         Dictionary <string, string> valueDic, string configName)
    {
        List <CfgFieldInfo> langFields = new List <CfgFieldInfo>(fieldInfos.Length);

        for (int i = 0; i < fieldInfos.Length; i++)
        {
            CfgFieldInfo info = fieldInfos[i];
            if (info.FunType == FunType.Lang)
            {
                langFields.Add(info);
            }
        }

        //创建CfgDao文件
        if (fieldInfos.Length < 1)
        {
            UnityEngine.Debug.LogError("xml没有字段信息" + "CfgCodeCreator.FillDao");
            return(false);
        }

        //处理语言代码
        if (langFields.Count > 0)
        {
            string languageCode  = "\r\n\r\n" + TemplateEngine.Do("CfgSwitchLangTemplate.txt", gValueDic, valueDic, false);
            string tryGetCodeStr = "";
            for (int i = 0; i < langFields.Count; i++)
            {
                CfgFieldInfo fieldInfo      = langFields[i];
                string       tryGetCodeItem = "                strDao.TryGetString(\"{0}.{1}.\" + cfg.GetKey(), language, ref cfg.{2});\r\n";
                tryGetCodeItem = string.Format(tryGetCodeItem, configName, fieldInfo.Name, fieldInfo.Name);
                tryGetCodeStr += tryGetCodeItem;
            }
            tryGetCodeStr            = tryGetCodeStr.Remove(tryGetCodeStr.Length - 2);
            valueDic["languageCode"] = languageCode.Replace("${TrGetCode}", tryGetCodeStr);
        }
        else
        {
            valueDic["languageCode"] = "";
        }
        return(true);
    }
Example #3
0
    /// <summary>
    /// 从xml文件中读出配置信息
    /// </summary>
    /// <param name="name"></param>
    /// <param name="formatFieldName"></param>
    /// <returns></returns>
    private CfgFieldInfo[] ReadFieldInfo(string name, bool formatFieldName)
    {
        string path  = EditorHelper.GetProjPath("Assets/Config/" + name + ".csv");
        Excel  excel = new Excel(-1);

        if (!excel.Load(path))
        {
            return(null);
        }

        if (excel.RowCount < 3)
        {
            UnityEngine.Debug.LogError("表定义错误,缺少必要的表头" + "CfgCodeCreator.ReadType");
            return(null);
        }

        uint len = excel.ColCount;
        List <CfgFieldInfo> infos = new List <CfgFieldInfo>((int)len);

        for (uint i = 0; i < len; i++)
        {
            IRowReader desc  = excel.GetRow(0);
            IRowReader type  = excel.GetRow(1);
            IRowReader field = excel.GetRow(2);

            CfgFieldInfo info = new CfgFieldInfo();
            info.Type = type.Get(i);
            switch (info.Type)
            {
            case "sint":
                info.Type = "int";
                break;

            case "sint[]":
                info.Type = "int[]";
                break;

            case "sint[][]":
            case "int[][]":
                info.Type = IntArrayCode;
                break;

            case "uint[][]":
                info.Type = UintArrayCode;
                break;

            case "string[][]":
                info.Type = StringArrayCode;
                break;

            case "float[][]":
                info.Type = FloatArrayCode;
                break;
            }
            info.KeyName = field.Get(i);
            if (string.IsNullOrEmpty(info.KeyName))
            {
                UnityEngine.Debug.LogWarning("表定义错误,字段名为空,程序自动忽略" + "CfgCodeCreator.ReadType");
                continue;
            }

            if (m_funMap.TryGetValue(info.KeyName[0], out info.FunType))
            {
                info.Name = info.KeyName.Substring(1);
                if (info.Name == "")
                {
                    UnityEngine.Debug.LogWarning("表定义错误,字段名只有功能符,程序自动忽略" + "CfgCodeCreator.ReadType");
                    continue;
                }
            }
            else
            {
                info.Name = info.KeyName;
            }
            if (formatFieldName)
            {
                info.Name = FormatName(info.Name);
            }
            info.Desc = desc.Get(i);
            infos.Add(info);
        }
        return(infos.ToArray());
    }
Example #4
0
    /// <summary>
    /// 填充config的数据,主要填充以下字段
    /// FieldList,KeyType,KeyFieldName
    ///
    /// </summary>
    /// <param name="fieldInfos"></param>
    /// <param name="gValueDic"></param>
    /// <param name="valueDic"></param>
    /// <param name="configName"></param>
    /// <returns></returns>
    private bool FillConfig(CfgFieldInfo[] fieldInfos, Dictionary <string, string> gValueDic,
                            Dictionary <string, string> valueDic, string configName)
    {
        List <CfgFieldInfo> keyFields = new List <CfgFieldInfo>(fieldInfos.Length);

        for (int i = 0; i < fieldInfos.Length; i++)
        {
            CfgFieldInfo info = fieldInfos[i];
            if (info.FunType == FunType.Key)
            {
                keyFields.Add(info);
            }

            //处理注释中的换行
            if (!string.IsNullOrEmpty(info.Desc) &&
                (info.Desc.Contains("\r") || info.Desc.Contains("\n")))
            {
                info.Desc = info.Desc.Replace("\r\n", "${newLine}");
                info.Desc = info.Desc.Replace("\n\r", "${newLine}");
                info.Desc = info.Desc.Replace("\n", "${newLine}");
                info.Desc = info.Desc.Replace("\r", "${newLine}");
                info.Desc = info.Desc.Replace("${newLine}", "\r\n        /// ");
            }
        }

        //没有指定主键,默认第一个
        if (keyFields.Count == 0)
        {
            keyFields.Add(fieldInfos[0]);
        }

        //创建CfgDao文件
        if (fieldInfos.Length < 1)
        {
            UnityEngine.Debug.LogError("xml没有字段信息" + "CfgCodeCreator.Do");
            return(false);
        }

        if (keyFields.Count == 1)
        {
            //只有一个key的情况
            CfgFieldInfo keyField = keyFields[0];
            valueDic["KeyType"]      = keyField.Type;
            valueDic["KeyFieldName"] = keyField.Name;
        }
        else
        {
            valueDic["KeyType"] = "string";
            string keyCode = "";
            for (int i = 0; i < keyFields.Count; i++)
            {
                CfgFieldInfo keyField = keyFields[i];
                if (i == 0)
                {
                    keyCode += keyField.Name + ".ToString()";
                }
                else
                {
                    keyCode += " + \"_\" + " + keyField.Name + ".ToString()";
                }
            }
            valueDic["KeyFieldName"] = keyCode;
        }

        //处理字段声名
        valueDic["FieldList"] = TemplateEngine.DoList("        public ${Type} ${Name};\r\n\r\n",
                                                      fieldInfos, new[] { "Type", "Name" }, (s, info) =>
        {
            if (!string.IsNullOrEmpty(info.Desc))
            {
                string desc = "        /// <summary>\r\n        /// " + info.Desc + "\r\n        /// </summary>\r\n";
                s           = desc + s;
            }
            return(s);
        });
        return(true);
    }