protoc-gen-lua 生成的lua文件提示错误: main function has more than 200 local variables
2015-12-12 02:20
627 查看
在使用protoc-gen-lua 生成的lua文件时提示错误:
main function has more than 200 local variables
意思就是说,这个lua 脚本中的 local 变量超过了 200个,不知是不是lua的限定还是 Protobuf 的限定。总之很奇妙。
到StackOverFlow上面搜索到相关的问题,很多朋友表示把 local 变量装在Table中可以很完美的解决这个问题,尝试了一下确实OK的运行起来的。
于是写了一个小工具,把当前目录下的所有的 Protobuf – lua 文件里面的local 变量都状在 Table 里面。
代码如下:
小工具的工程打包下载: http://pan.baidu.com/s/1ntmy01V
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中使用 Repeated 标签
- Protobuf-Lua 中使用 Enum
- protoc-gen-lua extensions正确的使用方式
- [stack]Evaluate Reverse Polish Notation
- lua 中pairs 和 ipairs区别
- lua学习06——lua中的面向对象
- PB中的Describe,Evaluatate,LookUpDisplay小结
- lua读取xml
- lua——userdata使用
- lua学习05——库s
- Learning Game AI Programming with lua(一)
- Lua中的基本函数库
- LUA脚本在游戏中的应用场合
- lua使用总结
- leetcode Evaluate Reverse Polish Notation
- Ubuntu编译Lua
- lua 链表 迭代器
- Luajit反编译之探索记
- lua 5.2 GC 源码分析 一
- 使用luabind生成dll文件