Esempio n. 1
0
    static void SyncList(List <string> keyList)
    {
        Init();
        List <string> addList = new List <string>();

        foreach (string key in keyList)
        {
            PathConfig config = _configs[key];
            Sync(config);
            if (!config.toResmap)
            {
                continue;
            }
            //收集需要动态到resmap的文件
            foreach (OptCmdProp cmdProp in cmdPropList)
            {
                if (cmdProp.isDir)
                {
                    continue;                //文件夹直接跳过
                }
                switch (cmdProp.opt)
                {
                case Opt.New:
                    addList.Add(cmdProp.dstPath);
                    break;
                }
            }
        }
        AssetDatabase.Refresh();
        if (addList.Count > 0)
        {
            ResmapUtility.ToResmapExternal(addList);
        }
    }
    private static void CreateTxt()
    {
        List <string> addList = new List <string>();

        //清理旧的txt文件:保留文件名称首字母小写的数据文件
        foreach (string dir in ResmapUtility.LocalTypeList)
        {
            string dirPath = string.Format(TxtDir, dir);
            if (Directory.Exists(dirPath))
            {
                ClearOldTxt(dirPath);
            }
            else
            {
                Directory.CreateDirectory(dirPath);
            }
        }

        localIdDic.Clear();
        localValueDic.Clear();

        //生成新的txt文件
        for (int k = 0; k < tableList.Count; k++)
        {
            TableInfo tableInfo = tableList[k];
            EditorUtility.DisplayProgressBar("正在生成Txt文件", tableInfo.fileName + ".txt", (k + 1) * 1f / tableList.Count);
            StringBuilder sb        = new StringBuilder();
            DataRow       exportRow = tableInfo.dataTable.Rows[0];
            DataRow       typeRow   = tableInfo.dataTable.Rows[1];
            //记录字段名称
            DataRow fieldRow = tableInfo.dataTable.Rows[2];
            for (int i = 0; i < tableInfo.colCount; i++)
            {
                if (!exportRow[i].ToString().Trim().ToLower().Contains("c"))
                {
                    continue;                                                         //注释用字段,不导入txt文件中
                }
                string value = fieldRow[i].ToString().Trim();
                if (string.IsNullOrEmpty(value))
                {
                    Debug.LogError(tableInfo.fileName + "表格中字段名不能为空!");
                    return;
                }
                sb.Append(value);
                if (i != tableInfo.colCount - 1)
                {
                    sb.Append("\t");
                }
            }
            sb.Append("\n");
            //主键:表格中第一列含有c的为主键
            int mainKey = 0;
            for (int j = 0; j < tableInfo.colCount; j++)
            {
                string value = exportRow[j].ToString().ToLower();
                string type  = typeRow[j].ToString().ToLower();
                if (value.Contains("c") && !type.Contains("[]") && !type.Contains("bool"))
                {
                    mainKey = j;
                    break;
                }
            }
            //多语言文本结构
            string[] dirSplits       = tableInfo.dir.Split(new[] { '/' }, 1);
            string   localModuleName = dirSplits[0];
            if (!localIdDic.ContainsKey(localModuleName))
            {
                localIdDic.Add(localModuleName, new Dictionary <string, string>());
                localValueDic.Add(localModuleName, new Dictionary <string, string>());
            }

            //记录内容
            for (int i = StartRow; i < tableInfo.rowCount; i++)
            {
                DataRow rowValue   = tableInfo.dataTable.Rows[i];
                string  mainKeyStr = rowValue[mainKey].ToString().Trim();
                //记录每行的值
                for (int j = 0; j < tableInfo.colCount; j++)
                {
                    string format = exportRow[j].ToString().Trim().ToLower();
                    if (!format.Contains("c"))
                    {
                        continue;                       //注释用字段,不导入txt文件中
                    }
                    string type  = typeRow[j].ToString().Trim().ToLower();
                    string value = rowValue[j].ToString().Trim();
                    //内容空的时候,设置默认值
                    if (string.IsNullOrEmpty(value))
                    {
                        if (format.Contains("t") && !format.Contains("e"))
                        {
                            Debug.LogErrorFormat("表 {0} 中第 {1} 行的 {2} 值不能为空,请检查!", tableInfo.fileName, i + 1, fieldRow[j].ToString().Trim());
                        }
                        else
                        {
                            if (type.Contains("[]"))
                            {
                                value = "";
                            }
                            else
                            {
                                if (type.Contains("string"))
                                {
                                    value = "";
                                }
                                else
                                {
                                    value = "0";
                                }
                            }
                        }
                    }
                    else
                    {
                        //检查数据是否合法
                        if (type.Contains("int") || type.Contains("float") || type.Contains("double"))
                        {
                            if (Regex.IsMatch(value, @"[^ 0123456789.|-]"))
                            {
                                Debug.LogErrorFormat("表 {0} 中第 {1} 行的 {2} 值不合法,请检查!", tableInfo.fileName, i + 1, fieldRow[j].ToString().Trim());
                            }
                        }
                        else if (type.Contains("bool"))
                        {
                            if (type.Contains("[]"))
                            {
                                value = TransBoolArray(value);
                            }
                            else
                            {
                                value = TransBool(value);
                            }
                        }
                        else if (type.Contains("string") && format.Contains("t"))//含有多语言文本
                        {
                            if (type.Contains("[]"))
                            {
                                string[] splits = value.Split('|');
                                value = "";
                                for (int m = 0; m < splits.Length; m++)
                                {
                                    string str = splits[m];
                                    string localId;
                                    if (!localValueDic[localModuleName].TryGetValue(str, out localId))
                                    {//判断字符串是否已经存在
                                        localId = tableInfo.fileName + "_" + fieldRow[j] + "_" + mainKeyStr + "_" + (m + 1);
                                        localIdDic[localModuleName].Add(localId, str);
                                        localValueDic[localModuleName].Add(str, localId);
                                    }
                                    if (m == splits.Length - 1)
                                    {
                                        value += localId;
                                    }
                                    else
                                    {
                                        value += localId + "|";
                                    }
                                }
                            }
                            else
                            {
                                string localId;
                                if (!localValueDic[localModuleName].TryGetValue(value, out localId))
                                {//判断字符串是否已经存在
                                    localId = tableInfo.fileName + "_" + fieldRow[j] + "_" + mainKeyStr;
                                    localIdDic[localModuleName].Add(localId, value);
                                    localValueDic[localModuleName].Add(value, localId);
                                }
                                value = localId;
                            }
                        }
                    }
                    //保存内容
                    sb.Append(value);
                    if (j != tableInfo.colCount - 1)
                    {
                        sb.Append("\t");
                    }
                }
                //换行
                if (i != tableInfo.rowCount - 1)
                {
                    sb.Append("\n");
                }
            }
            //根据多语言,分割txt文件
            string subDirPath = dirSplits.Length > 1 ? dirSplits[1] : "";
            if (string.IsNullOrEmpty(subDirPath))
            {
                string filePath = string.Format(TxtDir, dirSplits[0]) + "/" + tableInfo.fileName + ".txt";
                File.WriteAllText(filePath, sb.ToString());
                addList.Add(filePath);
            }
            else
            {
                string dirPath = string.Format(TxtDir, dirSplits[0]) + "/" + subDirPath;
                Directory.CreateDirectory(dirPath);
                string filePath = dirPath + "/" + tableInfo.fileName + ".txt";
                File.WriteAllText(filePath, sb.ToString());
                addList.Add(filePath);
            }
        }
        CreateLocalFile(addList);//创建多语言文件
        AssetDatabase.Refresh();

        if (addList.Count > 0)
        {
            ResmapUtility.ToResmapExternal(addList);
        }
    }