string InsertContent(string path, string beginFlag, string endFlag, string insertStr, ref int allEndIndex)
    {
        if (!File.Exists(path))
        {
            allEndIndex = -1;
            return(insertStr);
        }
        string content = FileUtilityEditor.ReadToEnd(path);
        int    bIndex  = content.IndexOf(beginFlag);
        int    eIndex  = content.IndexOf(endFlag);

        allEndIndex = content.IndexOf("//End");
        if (bIndex == -1 || eIndex == -1)
        {
            if (allEndIndex == -1)
            {
                return(insertStr);
            }
            return(content.Insert(allEndIndex, insertStr + "\r\n\r\n"));
        }
        eIndex = eIndex + content.Substring(eIndex).IndexOf("\r\n\r\n");
        if (eIndex == -1)
        {
            return(insertStr);
        }
        return(content.Substring(0, bIndex) + insertStr + content.Substring(eIndex + 4));
    }
Ejemplo n.º 2
0
    //public string java_package;
    //public string java_outer_classname;

    //public string namespaceStr;//package task;

    public static List <CSProtoMessageData> ParseProtoFile(string file)
    {
        List <CSProtoMessageData> list = new List <CSProtoMessageData>();

        string fileContent = FileUtilityEditor.ReadToEnd(file);

        //匹配出
        //message VeinBean {
        //    required int32 newId = 1;//现在的配置表id
        //    optional int32 fightValue = 2;//战斗力
        //}

        MatchCollection matchs = null;

        matchs = Regex.Matches(fileContent, pattern);

        if (matchs != null && matchs.Count > 0)
        {
            for (int i = 0; i < matchs.Count; i++)
            {
                CSProtoMessageData data = new CSProtoMessageData(matchs[i].Groups[0].Value);
                list.Add(data);
                break;
            }
        }

        return(list);
    }
    /// <summary>
    /// 将内容枚举内容写入生成文件中
    /// </summary>
    /// <param name="outFolder">生成文件存放路径</param>
    /// <param name="name">写入内容的xml文件名字</param>
    /// <param name="id">写入内容的xml所以功能ID</param>
    /// <param name="content">写入的枚举内容</param>
    /// <param name="fileContent">文本内容</param>
    /// <param name="IsWrite">是否直接写入文件,当批量增加xml的时候,反复的写入会造成大量的开销</param>
    private void ParseToESocketEventEnum(string outFolder, string name, int id, string content, ref string fileContent, bool IsWrite = true)
    {
        string outFile = outFolder + "ESocketEvent.cs";

        if (string.IsNullOrEmpty(fileContent))
        {
            fileContent = FileUtilityEditor.ReadToEnd(outFile);
        }

        if (string.IsNullOrEmpty(fileContent))
        {
            string defaultContext = "/// <summary>\r\n/// 网络事件\r\n/// </summary>\r\npublic enum ESocketEvent\r\n{\r\n}";

            FileUtilityEditor.Write(outFile, defaultContext, false);
            fileContent = defaultContext;
        }

        string startFlag = GetServerProtoEnumStart(name, id);
        string endFlag   = GetServerProtoEnumEnd(name, id);

        pattern = startFlag + "([\\s\\S]*)" + endFlag;

        content = startFlag + content + endFlag;
        matchs  = Regex.Matches(fileContent, pattern);
        if (matchs != null && matchs.Count > 0)
        {
            string enumContent = matchs[0].Groups[0].Value;
            fileContent = fileContent.Replace(enumContent, content);
        }
        else//沒有找到之前的生成内容,说明这个xml是新加入的,那么插入到最下面
        {
            pattern = "public enum ESocketEvent\r\n{([^}]+)}";
            matchs  = Regex.Matches(fileContent, pattern);
            //拿到之前的ESocketEvent枚举内所有内容
            if (matchs != null && matchs.Count > 0)
            {
                string enumContent = matchs[0].Groups[0].Value;
                content     = enumContent.Insert(enumContent.Length - 1, "\r\n" + content);
                fileContent = fileContent.Replace(enumContent, content);
            }
        }

        if (IsWrite)
        {
            FileUtilityEditor.Write(outFile, fileContent, false);
        }
    }