コード例 #1
0
    public static void CreateConfigScrpt(List <CreateConfigScrptData> scriptDataList)
    {
        string temp = "";

        temp += "using System.Collections.Generic;\n";
        temp += "using " + PackageName + ";\n";
        temp += "public class GetConfig{ \n";
        temp += "private ConfigDatabase AllConfig;\n";
        temp += "public void InitConfig(ConfigDatabase data){\n";
        temp += "AllConfig = data;\n";
        temp += "}\n";
        for (int i = 0; i < scriptDataList.Count; i++)
        {
            CreateConfigScrptData tempData = scriptDataList[i];
            string dicname      = tempData.excelName + "Dic";
            string dataListName = "AllConfig.m_" + tempData.excelName + "_data.config";
            string camelcaseStr = tempData.excelName;
            string configName   = tempData.excelName + "Config";
            temp += "\n";
            temp += string.Format("private Dictionary<{0}, {1}Config> {2};\n", tempData.TypeName, tempData.excelName, dicname);
            temp += string.Format("public {0}Config Get{1}({2} id)\n", tempData.excelName, camelcaseStr, tempData.TypeName);
            temp += "{\n";
            temp += string.Format("if(null == {0})", dicname);
            temp += "{";
            temp += string.Format("{0} = new Dictionary<{1}, {2}Config>();", dicname, tempData.TypeName, tempData.excelName);
            temp += string.Format(" foreach({0} oneConfig in {1})  {2}[oneConfig.{3}] = oneConfig;", configName, dataListName, dicname, tempData.VariableName);
            temp += "}\n";
            temp += string.Format("if({0}.ContainsKey(id))  return {1}[id]; return null;", dicname, dicname);
            temp += "\n}\n";
            temp += string.Format("public List<{0}> Get{1}List()", configName, camelcaseStr);
            temp += "{";
            temp += string.Format("return {0};", dataListName);
            temp += "}\n";
        }
        temp += "}";
        string filePath = HotFixConfigCSPath + "/GetConfig.cs";

        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }
        FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);

        //获得字节数组
        byte[] wiritedata = System.Text.Encoding.Default.GetBytes(temp);
        //开始写入
        fs.Write(wiritedata, 0, wiritedata.Length);
        //清空缓冲区、关闭流
        fs.Flush();
        fs.Close();
    }
コード例 #2
0
    /// <summary>
    /// 导出proto文件
    /// </summary>
    /// <param name="platform"></param>
    /// <param name="selectList"></param>
    public static void ExportProto(List <string> selectList = null)
    {
        string serverConfigProtoPath = PlayerPrefs.GetString(ExcelPathKey);

        if (string.IsNullOrEmpty(serverConfigProtoPath))
        {
            Debug.Log("没有设置服务器SNV目录");
            return;
        }

        DirectoryInfo serverDirectoryInfo = new DirectoryInfo(serverConfigProtoPath);

        System.IO.FileInfo[] serverFileInfos = serverDirectoryInfo.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly);

        //可导配置列表
        List <string> excelNames = new List <string>();
        //可导配置相关信息(用于创建初始化代码)
        List <CreateConfigScrptData> scriptDataList = new List <CreateConfigScrptData>();

        #region 遍历所有配置导出单个proto文件
        foreach (System.IO.FileInfo fileInfo in serverFileInfos)
        {
            if (fileInfo.Name.StartsWith("~"))
            {
                continue;
            }

            if (Excel2ProtoTool.ingoreList.Contains(fileInfo.Name))
            {
                continue;
            }

            string name = fileInfo.Name.Replace(".xlsx", "");

            if (!fileInfo.Name.EndsWith(".xlsx"))
            {
                Debug.Log("跳过文件  " + name + " 因为它不是一个表文件");
                continue;
            }
            //单个配置相关数据
            CreateConfigScrptData scriptData = new CreateConfigScrptData();
            scriptDataList.Add(scriptData);
            scriptData.excelName = name;
            excelNames.Add(name);

            if (selectList != null && !selectList.Contains(fileInfo.Name))
            {
                continue;
            }

            Debug.Log("build Proto:" + name);

            var start = Time.realtimeSinceStartup;

            FileStream stream = File.Open(serverConfigProtoPath + "/" + fileInfo.Name, FileMode.Open, FileAccess.Read);

            ExcelPackage   excelReader = new ExcelPackage(stream);
            ExcelWorkbook  result      = excelReader.Workbook;
            ExcelWorksheet workSheet   = result.Worksheets.First();
            int            columns     = workSheet.Dimension.End.Column;
            int            rows        = workSheet.Dimension.End.Row;

            if (rows < 3)
            {
                Debug.LogError("选择的excel表行数小于4");
                return;
            }
            string SaveValue = "package " + PackageName + ";\n\n";
            SaveValue += "message " + name + "Config{\n";

            for (int j = 0; j <= columns; j++)
            {
                if (j == 0)
                {
                    continue;
                }
                if (string.IsNullOrEmpty(workSheet.GetValue <string>(1, j)))
                {
                    continue;
                }
                string valueName = workSheet.GetValue <string>(3, j);
                if (string.IsNullOrEmpty(valueName))
                {
                    Debug.LogError(fileInfo.Name + "第" + (j + 1) + "列变量名为空");
                    return;
                }
                valueName = valueName.TrimEnd(' ');
                string explain = workSheet.GetValue <string>(1, j);
                string type    = workSheet.GetValue <string>(2, j);
                //保存第一个字段的类型以及变量名
                if (j == 1)
                {
                    scriptData.VariableName = valueName;
                    scriptData.TypeName     = type;
                }
                if (type == "int")
                {
                    type = "int32";
                }
                else if (type == "long")
                {
                    type = "int64";
                }
                SaveValue += "\trequired " + type + " " + valueName + " = " + (j) + ";\t\t//" + explain + "\n";
            }

            SaveValue += "}\n\n";

            SaveValue += "message " + name + "ConfigData{\n";
            SaveValue += "\trepeated " + name + "Config config = 1;\n";
            SaveValue += "}\n";

            System.IO.FileStream Fstream = System.IO.File.Create(Application.dataPath + "/" + scriptsProtoConfigProto + "/" + name + ".proto");
            char[] data = SaveValue.ToCharArray();
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(Fstream);
            bw.Write(data);
            bw.Close();
            Fstream.Close();

            var end = Time.realtimeSinceStartup;
            Debug.LogFormat("Build,Start:{0}s end:{1}s duration:{2}s name:{3}", start, end, end - start, name);
        }
        #endregion

        #region 创建一个总表proto文件
        string protoNewValue = "package " + PackageName + ";\r\n\r\n";
        foreach (string str in excelNames)
        {
            protoNewValue += " import \"" + str + ".proto\";\r\n";
        }
        protoNewValue += "\r\nmessage ConfigDatabase {\r\n";
        for (int i = 0; i < excelNames.Count; i++)
        {
            protoNewValue += "\trequired " + excelNames[i] + "ConfigData m_" + excelNames[i] + "_data = " + (i + 1) + ";\r\n";
        }
        protoNewValue += "}\r\n";

        System.IO.FileStream Fstream2 = System.IO.File.Create(Application.dataPath + "/" + scriptsProtoConfigProto + "/ConfigDatabaseFile.proto");
        char[] data2 = protoNewValue.ToCharArray();
        System.IO.BinaryWriter bw2 = new System.IO.BinaryWriter(Fstream2);
        bw2.Write(data2);
        bw2.Close();
        Fstream2.Close();
        #endregion

        #region 自动补齐初始化代码
        CreateConfigScrpt(scriptDataList);
        #endregion
    }