summaryrefslogtreecommitdiff
path: root/Source/tools/bindingGen/main.cs
blob: cf78f8922077a5ea8e6aac4773e73ad074df445d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using System.IO;

namespace bindingGen
{
	/// <summary>
	/// 输入目录,在目录下生成./binding目录,存放导出的binding代码
	/// </summary>
	class Program
	{
		// {0} 文件名
		// {1} 第一个名称空间
		// {2} 第二个名称空间
		// {3} 内容
		static string output = @"#include ""../{0}.h""

using namespace std;

namespace {1} 
{{
	namespace {2} 
	{{
		{3}
	}}
}}
";

		// {0} 类名
		// {1} 内容
		static string registry = @"
		LUAX_REGISTRY({0})
		{{
{1}
		}}
";

		// {0} 类名
		// {1} 内容
		static string postprocess = @"
		LUAX_POSTPROCESS({0})
		{{
{1}
		}}
";

		// {0} 类名
		// {1} 去掉_的函数名
		// {2} 小写的类名
		static string method = @"
		// {2}:{1}()
		LUAX_IMPL_METHOD({0}, _{1})
		{{
			LUAX_PREPARE(L, {0});

		}}
";
		// {0} 类名
		// {1} 去掉_的函数名
		static string method_new = @"
		// {0}.{1}()
		LUAX_IMPL_METHOD({0}, _{1})
		{{
			LUAX_STATE(L);

		}}
";

		static string make_register_methods(MatchCollection methods)
		{
			if (methods.Count == 0)
				return "";
			string register_methods = "";
			register_methods += "\t\t\tLUAX_REGISTER_METHODS(state,\n";
			int maxlen = 0; 
			foreach(var m in methods)
			{
				string method = m.ToString();
				if (method.Count() > maxlen)
					maxlen = method.Count(); 
			}
			for(int i = 0; i < methods.Count; ++i)
			{
				Match m = methods[i];
				string method = m.ToString();
				register_methods += "\t\t\t\t";
				register_methods += "{ ";
				register_methods += ('"' + method.Substring(1, method.Count() - 1) + "\",").PadRight(maxlen + 3, ' ');
				register_methods += method.PadRight(maxlen + 1, ' ');
				register_methods += "}";
				if (i != methods.Count - 1)
					register_methods += ',';
				register_methods += '\n';
			}
			register_methods += "\t\t\t);";
			return register_methods;
		}

		static string make_impl_methods(MatchCollection mc, string cname)
		{
			if (mc.Count == 0)
				return "";

			string methods = ""; 

			foreach(var m in mc)
			{
				string name = m.ToString();
				if(name != "_New")
				{
					methods += String.Format(method, cname, name.Substring(1, name.Count() - 1), cname.ToLower());
				}
				else
				{
					methods += String.Format(method_new, cname, name.Substring(1, name.Count() - 1));
				}
			}

			return methods;
		}

		static string make_register_enum(MatchCollection mc, string src)
		{
			if (mc.Count == 0)
				return "";

			string reg_enum_l = @"(?<=enum\s";
			string reg_enum_r = @"[\s\n]*\{((?!\{)[\s\S])*)[A-Z_]+(?=\s*,)";
			string enums = "";

			foreach(var m in mc)
			{
				string name = m.ToString();
				enums += "\t\t\tLUAX_REGISTER_ENUM(state, ";
				enums += "\"E" + name + "\",\n";
				MatchCollection values = Regex.Matches(src, reg_enum_l + name + reg_enum_r);
				string[] enames = new string[values.Count];
				string[] keys = new string[values.Count];
				int maxEname = 0, maxKey = 0;
				for (int i = 0; i < values.Count; ++i)
				{
					enames[i] = values[i].ToString(); 
					keys[i] = enames[i].Substring(enames[i].LastIndexOf('_') + 1, enames[i].Count() - enames[i].LastIndexOf('_') -1);
					if (enames[i].Count() > maxEname) maxEname = enames[i].Count();
					if (keys[i].Count() > maxKey) maxKey = keys[i].Count();
				}
				for(int i = 0; i < values.Count; ++i)
				{
					enums += "\t\t\t\t{ ";
					enums += ('"' + keys[i] + "\",").PadRight(maxKey + 4);
					enums += enames[i].PadRight(maxEname + 1);
					enums += "}";
					if (i != values.Count - 1)
						enums += ",";
					enums += "\n";
				}
				enums += "\t\t\t);\n";
			}
			return enums;
		}

		/// <summary>
		/// 用法: 
		///	bindingGen <目录>
		/// </summary>
		/// <param name="args"></param>
		static void Main(string[] args)
		{
			if (args.Length < 1)
				return;

			string dir = args[0];

			Console.WriteLine("源目录: " + dir);

			if (!Directory.Exists(dir))
				return;

			string reg_class = @"(?<=Portable\<)[0-9a-zA-Z]+(?=\>)";
			string reg_methods = @"(?<=LUAX_DECL_METHOD\()[0-9a-zA-Z_]+(?=\))";
			string reg_enums = @"(?<=LUAX_DECL_ENUM\()[0-9a-zA-Z_]+(?=\))";
			string reg_namespace = @"(?<=namespace\s)[0-9a-zA-Z]+(?=[\s\n]*)";

			if(!Directory.Exists(dir + "/binding"))
			{
				Directory.CreateDirectory(dir + "/binding");
			}

			string[] files = Directory.GetFiles(dir);
			for(int i = 0; i < files.Count(); ++i)
			{
				string file = files[i];
				if (!File.Exists(file))
					continue;
				file = file.Replace('\\', '/');
				string name = file.Substring(file.LastIndexOf('/') + 1, file.LastIndexOf('.') - file.LastIndexOf('/') - 1);
				string bindingFile = dir + "/binding/_" + name + ".cpp";
				if (File.Exists(bindingFile))
					continue;
				string code = File.ReadAllText(file);
				//
				Match m = Regex.Match(code, reg_class);
				if (!m.Success)
					continue;
				string className = m.ToString();
				MatchCollection mc = Regex.Matches(code, reg_namespace);
				// 应该两个名称空间
				if(mc.Count != 2)
				{
					Console.WriteLine("Error: 源文件没有两个名称空间 " + file);
					continue;
				}
				string namespace1 = mc[0].ToString();
				string namespace2 = mc[1].ToString();
				// 名称空间内的内容
				string content = "";
				mc = Regex.Matches(code, reg_methods);
				content += String.Format(registry, className, make_register_methods(mc));
				mc = Regex.Matches(code, reg_enums);
				content += String.Format(postprocess, className, make_register_enum(mc, code));
				mc = Regex.Matches(code, reg_methods);
				content += make_impl_methods(mc, className);
				string binding = String.Format(output, name, namespace1, namespace2, content);
				Console.WriteLine("输出: "+bindingFile);
				File.WriteAllText(bindingFile, binding);
			}

		}
	}
}