main function has more than 200 local variables
意思就是说,这个lua 脚本中的 local 变量超过了 200个,不知是不是lua的限定还是 Protobuf 的限定。总之很奇妙。
到StackOverFlow上面搜索到相关的问题,很多朋友表示把 local 变量装在table中可以很完美的解决这个问题,尝试了一下确实OK的运行起来的。
于是写了一个小工具,把当前目录下的所有的 Protobuf – lua 文件里面的local 变量都状在 table 里面。
代码如下:
using System; using System.Collections.Generic; using System.linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Text.RegularExpressions; namespace CMGE { class Program { static string ReplaceWholeWord(string original,string wordToFind,string replacement,RegexOptions regexOptions = RegexOptions.None) { string pattern = String.Format(@"\b{0}\b",wordToFind); string ret = Regex.Replace(original,pattern,replacement,regexOptions); return ret; } static voID Generatortable(string path) { path=path.Replace("\","/"); string filename = path.Substring(path.LastIndexOf('/')+1,path.Length - path.LastIndexOf('/')-1); filename = filename.Substring(0,filename.LastIndexOf('.')); string tablename = filename + "table"; //创建新目录; string dirpath = path.Substring(0,path.LastIndexOf('/')) + "/newlua"; DirectoryInfo dirinfo = new DirectoryInfo(dirpath); if (!dirinfo.Exists) { dirinfo.Create(); } string newfilename = filename + ".lua"; string newfilepath = dirpath + "/" + newfilename; List<string> lineList = new List<string>(); List<string> msgnameList = new List<string>(); StreamReader reader = new StreamReader(path); while (!reader.EndOfStream) { string line = reader.Readline(); lineList.Add(line); } //判断第一行; if (lineList[0] != "-- Generated By protoc-gen-lua Do not Edit") { return; } //修改第一行 添加local表; lineList[0] = lineList[0] + "\n\nlocal " + tablename + " = {}\n\n"; //寻找"protobuf.Message" for (int index = 0; index < lineList.Count; index++) { string line = lineList[index]; if (line.StartsWith("local ") && ( line.EndsWith(" = protobuf.Descriptor();") || line.EndsWith(" = protobuf.FIEldDescriptor();") || line.EndsWith("protobuf.EnumDescriptor();") || line.EndsWith(" = protobuf.EnumValueDescriptor();"))) { //找到这一行中 括号中的值; string begintag = "local "; string endtag = " = protobuf."; int begintagindex = line.IndexOf(begintag); //后半段; string part2 = line.Substring(begintagindex + begintag.Length); int endtagindex = part2.IndexOf(endtag); //获取到需要的; string messagename = part2.Substring(0,endtagindex); msgnameList.Add(messagename); } } //寻找 protobuf.Descriptor(); for (int index = 0; index < lineList.Count; index++) { string line = lineList[index]; if (line.StartsWith("local ") && ( line.EndsWith(" = protobuf.Descriptor();") || line.EndsWith(" = protobuf.FIEldDescriptor();") || line.EndsWith(" = protobuf.EnumDescriptor();") || line.EndsWith(" = protobuf.EnumValueDescriptor();"))) { lineList[index] = line.Replace("local ",""); } } //全局替换; for (int index = 0; index < lineList.Count; index++) { for (int msgnameindex = 0; msgnameindex < msgnameList.Count; msgnameindex++) { string msgname = msgnameList[msgnameindex]; if (lineList[index].Contains(msgname)) { //不是完整单词不能替换 lineList[index] = ReplaceWholeWord(lineList[index],msgname,tablename + "." + msgname); } } } //存储; StreamWriter writer = new StreamWriter(newfilepath); for (int index = 0; index < lineList.Count; index++) { writer.Writeline(lineList[index]); } writer.Flush(); } static voID Main(string[] args) { DirectoryInfo info = new DirectoryInfo("./"); fileInfo[] fileList = info.Getfiles("*.lua"); for (int index = 0; index < fileList.Length; index++) { Generatortable(fileList[index].Fullname); } } } }
小工具的工程打包下载:
http://pan.baIDu.com/s/1ntmy01V总结
以上是内存溢出为你收集整理的protoc-gen-lua 生成的lua文件提示错误: main function has more than 200 local variables全部内容,希望文章能够帮你解决protoc-gen-lua 生成的lua文件提示错误: main function has more than 200 local variables所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)