您的位置:首页 > 编程语言 > Lua

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 里面。

代码如下:

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: