Пример #1
0
    /// <summary>
    /// 导出自动生成脚本
    /// </summary>
    public string ExportScript()
    {
        var sb = new StringBuilder();

        sb.Append(SheetEditor.LineText(string.Format("//{0}", mSheetName), 1));
        string initFuncName  = StringUtil.Concat("Init", mSheetName);
        string ListParamName = StringUtil.Concat(mSheetName, "List");
        string dictParamName = StringUtil.Concat("m" + mSheetName, "Dict");
        bool   exportList    = (mExportDataType == EExportDataType.ONLY_ARRAY || mExportDataType == EExportDataType.BOTH);
        bool   exportDict    = (mExportDataType == EExportDataType.ONLY_DICT || mExportDataType == EExportDataType.BOTH);

        // List
        if (exportList)
        {
            sb.Append(SheetEditor.LineText(string.Format("private List<{0}> m{1};", mSheetName, ListParamName), 1));
            sb.Append(SheetEditor.LineText(string.Format("public List<{0}> Get{1}()", mSheetName, ListParamName), 1));
            sb.Append(SheetEditor.LineText("{", 1));
            sb.Append(SheetEditor.LineText(string.Format("if (m{0} == null)", ListParamName), 2));
            sb.Append(SheetEditor.LineText("{", 2));
            sb.Append(SheetEditor.LineText(string.Format("{0}();", initFuncName), 3));
            sb.Append(SheetEditor.LineText("}", 2));
            sb.Append(SheetEditor.LineText(string.Format("return m{0};", ListParamName), 2));
            sb.Append(SheetEditor.LineText("}", 1));
        }

        // Dictionary
        if (exportDict)
        {
            sb.Append(SheetEditor.LineText(string.Format("private Dictionary<{0}, Sheet.{1}> {2};", mKeyType, mSheetName, dictParamName), 1));
            sb.Append(SheetEditor.LineText(string.Format("public Sheet.{0} Get{0}({1} key)", mSheetName, mKeyType), 1));
            sb.Append(SheetEditor.LineText("{", 1));
            sb.Append(SheetEditor.LineText(string.Format("if ({0} == null)", dictParamName), 2));
            sb.Append(SheetEditor.LineText("{", 2));
            sb.Append(SheetEditor.LineText(string.Format("{0}();", initFuncName), 3));
            sb.Append(SheetEditor.LineText("}", 2));
            sb.Append(SheetEditor.LineText(string.Format("return {0}[key];", dictParamName), 2));
            sb.Append(SheetEditor.LineText("}", 1));
        }

        // 初始化方法
        sb.Append(SheetEditor.LineText(string.Format("private void {0}()", initFuncName), 1));
        sb.Append(SheetEditor.LineText("{", 1));
        if (exportList || exportDict)
        {
            sb.Append(SheetEditor.LineText(string.Format("var items = GetSheetInfo<{0}List>(\"{0}\").Items;", mSheetName), 2));
            if (exportList)
            {
                sb.Append(SheetEditor.LineText(string.Format("m{0} = items;", ListParamName), 2));
            }
            if (exportDict)
            {
                sb.Append(SheetEditor.LineText(string.Format("{0} = new Dictionary<{1}, Sheet.{2}>();", dictParamName, mKeyType, mSheetName), 2));
                sb.Append(SheetEditor.LineText(string.Format("items.ForEach(item => {0}[item.{1}] = item);", dictParamName, mKey), 2));
            }
        }
        sb.Append(SheetEditor.LineText("}", 1));
        sb.Append(SheetEditor.LineText("", 1));

        return(sb.ToString());
    }
Пример #2
0
    private static void ExportCSNetMsg()
    {
        ExportCS();
        var config = Base.Utils.FileUtil.ReadAllText(NETMSGCONFIG);

        config = "{ \"list\": " + config + "}";
        var netMsg = UnityEngine.JsonUtility.FromJson <NetMsgConfig <Editor.Proto.NetMsg> >(config);

        // 生成NetMsg.cs
        var sb = new StringBuilder();

        sb.Append(SheetEditor.LineText("/**"));
        sb.Append(SheetEditor.LineText(" * Tool generation, do not modify!!!"));
        sb.Append(SheetEditor.LineText(" */\n"));
        sb.Append(SheetEditor.LineText("using System;\nusing Base.Debug;\nusing Base.Utils;\nusing System.Collections.Generic;\n"));
        sb.Append(SheetEditor.LineText("public class NetMsg\n{"));
        foreach (var msg in netMsg.list)
        {
            sb.Append(SheetEditor.LineText("public const int " + msg.MsgName.ToUpper() + " = " + msg.MsgId + ";", 1));
        }
        sb.Append(SheetEditor.LineText("//=============================================================================", 1));
        sb.Append(SheetEditor.LineText("private static Dictionary<int, Type> MsgIdTypeDict;\n", 1));
        sb.Append(SheetEditor.LineText("public static void Init()", 1));
        sb.Append(SheetEditor.LineText("{", 1));
        sb.Append(SheetEditor.LineText("MsgIdTypeDict = new Dictionary<int, Type>();", 2));
        foreach (var msg in netMsg.list)
        {
            var msgName = msg.MsgName.ToUpper();
            var str     = "MsgIdTypeDict.Add({0}, typeof({1}));";
            sb.Append(SheetEditor.LineText(string.Format(str, msgName, msg.Response), 2));
        }
        sb.Append(SheetEditor.LineText("}\n", 1));
        sb.Append(SheetEditor.LineText("public static Type GetTypeByMsgId(int msgId)", 1));
        sb.Append(SheetEditor.LineText("{", 1));
        sb.Append(SheetEditor.LineText("if (MsgIdTypeDict == null) Init();", 2));
        sb.Append(SheetEditor.LineText("if (MsgIdTypeDict.ContainsKey(msgId))", 2));
        sb.Append(SheetEditor.LineText("{", 2));
        sb.Append(SheetEditor.LineText("return MsgIdTypeDict[msgId];", 3));
        sb.Append(SheetEditor.LineText("}", 2));
        sb.Append(SheetEditor.LineText("Debugger.LogError(StringUtil.Concat(\"Not Find Msg Type! Error MsgId: \", msgId));", 2));
        sb.Append(SheetEditor.LineText("return null;", 2));
        sb.Append(SheetEditor.LineText("}", 1));
        // 写入
        sb.Append(SheetEditor.LineText("}"));
        Base.Utils.FileUtil.WriteAllText(OUTNETMSGPATH, sb.ToString());
        Debugger.Log("Proto ExportNetMsg Done!");
    }
Пример #3
0
    public static void ExportCS()
    {
        var protoDir = new DirectoryInfo(PROTOPATH);
        var files    = protoDir.GetFiles();
        var fileSet  = new HashSet <string>();

        foreach (var file in files)
        {
            var sb = new StringBuilder();
            sb.Append(SheetEditor.LineText("/**"));
            sb.Append(SheetEditor.LineText(" * Tool generation, do not modify!!!"));
            sb.Append(SheetEditor.LineText(string.Format(" * Generated from: {0}", file.Name)));
            sb.Append(SheetEditor.LineText(" */\n"));
            sb.Append(SheetEditor.LineText("using ProtoBuf;\nusing System.Collections.Generic;\n"));

            // 读取proto文件
            var text = Base.Utils.FileUtil.ReadAllText(file.FullName);
            // 分离出命名空间
            var nameSpace = Regex.Match(text, "(?<=package ).*(?=;)").Groups[0].Value;
            sb.Append(SheetEditor.LineText("namespace " + nameSpace + " {\n"));

            // 分离出协议名
            var protocols = Regex.Matches(text, "(?<=message ).*(?=\r*\n*{)");
            foreach (var protocol in protocols)
            {
                var protocolStr = protocol.ToString().Replace("\r", "");
                protocolStr = protocolStr.Replace("\n", "");
                sb.Append(SheetEditor.LineText("[ProtoContract]", 1));
                sb.Append(SheetEditor.LineText("public class " + protocolStr + " {", 1));
                var regex  = "(?<=message " + protocolStr + "\r*\n*{)[^}]*(?=})";
                var keyStr = Regex.Match(text, regex).Groups[0].Value;
                // 分离协议字段
                keyStr = keyStr.Replace("\r", "").Replace("\n", "").Replace("\t", "");
                var keyLines = keyStr.Split(';');
                var count    = 0;
                foreach (var line in keyLines)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        count++;
                        var keys     = line.Trim().Split(' ');
                        var keyCode  = keys[0];
                        var keyType  = keys[1];
                        var keyValue = keys[2];
                        sb.Append(SheetEditor.LineText(string.Format("[ProtoMember({0})]", count), 2));
                        if (keyCode.Equals("repeated"))
                        {
                            sb.Append(SheetEditor.LineText(string.Format("public List<{0}> {1};", ConvertType(keyType), keyValue), 2));
                        }
                        else
                        {
                            sb.Append(SheetEditor.LineText(string.Format("public {0} {1};", ConvertType(keyType), keyValue), 2));
                        }
                    }
                }
                sb.Append(SheetEditor.LineText("}\n", 1));
            }

            sb.Append(SheetEditor.LineText("}"));
            var fileName = file.Name.Replace(file.Extension, "");
            fileSet.Add(fileName);
            // 写入
            Base.Utils.FileUtil.WriteAllText(OUTCSPATH + "/" + fileName + ".cs", sb.ToString());
        }
        //删除没用的协议 这里把NetMsg.cs和协议放在了一些,暂时手动删除没用的协议
        //var csProtoDir = new DirectoryInfo(OUTCSPATH);
        //var csFiles = csProtoDir.GetFiles();
        //foreach(var file in csFiles)
        //{
        //    var fileName = file.Name.Replace(file.Extension, "");
        //    if (!fileSet.Contains(fileName))
        //    {
        //        FileUtil.DeleteFileOrDirectory(file.FullName);
        //    }
        //}
        AssetDatabase.Refresh();
        Debugger.Log("Proto ExportCS Done!");
    }
Пример #4
0
 private static void ExportBytes()
 {
     SheetEditor.ExportBytes();
 }