-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc2lua.cpp
More file actions
1035 lines (918 loc) · 28.9 KB
/
Copy pathc2lua.cpp
File metadata and controls
1035 lines (918 loc) · 28.9 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// c2lua.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <vector>
#include <set>
#include <sstream>
extern "C" {
#include <pcre2.h>
};
#pragma comment(lib, "d:/library/pcre/src/Debug/pcre.lib")
void read_file(const char *sFn, std::string &sContent)
{
FILE *f = fopen(sFn, "r");
if(NULL == f)
return;
fseek(f, 0, SEEK_END);
unsigned size = ftell(f);
fseek(f, 0, SEEK_SET);
sContent.resize(size);
fread(&sContent[0], 1, size, f);
if (size >= 3
&& (unsigned char)sContent[0] == 0xEF
&& (unsigned char)sContent[1] == 0xBB
&& (unsigned char)sContent[2] == 0xBF) {
sContent.erase(0, 3);
}
fclose(f);
f = NULL;
}
struct RegexResult {
int nFrom, nTo;
};
int find_by_regex(const std::string &src, const char *pattern, std::vector<RegexResult> &result)
{
int error;
PCRE2_SIZE erroffset;
pcre2_compile_context *ccontext = pcre2_compile_context_create(NULL);
pcre2_set_newline(ccontext, PCRE2_NEWLINE_ANYCRLF);
int ops = 0;
if (src.find('\n') != std::string::npos) {
ops = PCRE2_MULTILINE;
}
/*
PCRE2_EXP_DEFN pcre2_code * PCRE2_CALL_CONVENTION
pcre2_compile(PCRE2_SPTR pattern, PCRE2_SIZE patlen, uint32_t options,
int *errorptr, PCRE2_SIZE *erroroffset, pcre2_compile_context *ccontext);
*/
pcre2_code *re = pcre2_compile((PCRE2_SPTR)pattern, strlen(pattern), ops, &error, &erroffset, ccontext);
if (re == NULL) {
//printf("PCRE compilation failed(%d) at offset %d\n", error, erroffset);
return 0;
}
//rc = pcre2_exec(re, NULL, src, strlen(src), 0, 0, ovector, OVECCOUNT);
/*
PCRE2_EXP_DEFN int PCRE2_CALL_CONVENTION
pcre2_match(const pcre2_code *code, PCRE2_SPTR subject, PCRE2_SIZE length,
PCRE2_SIZE start_offset, uint32_t options, pcre2_match_data *match_data,
pcre2_match_context *mcontext);
*/
pcre2_match_data *match_data = pcre2_match_data_create_from_pattern(re, NULL);
int rc = pcre2_match(re, (PCRE2_SPTR8)src.c_str(), src.size(), 0, 0, match_data, NULL);
if (rc < 0)
{
//if (rc == PCRE2_ERROR_NOMATCH)
// printf("Sorry, no match ...\n");
//else
// printf("Matching error %d\n", rc);
pcre2_code_free(re);
return 0;
}
int oveccount = pcre2_get_ovector_count(match_data);
PCRE2_SIZE *ovec = pcre2_get_ovector_pointer(match_data);
result.resize(rc);
for (int i = 0; i < rc; i++) {
result[i].nFrom = ovec[2*i];
result[i].nTo = ovec[2*i+1];
}
pcre2_code_free(re);
pcre2_match_data_free(match_data);
return rc;
}
int find_char(const char *src, int from, char ch, char pair_l = 0)
{
int ret = -1;
int depth = 0;
for (int i = from; src[i] != 0; ++i)
{
if (pair_l && src[i] == pair_l) {
++depth;
continue;
}
if (src[i] == ch)
{
if (depth) {
--depth;
continue;
}
ret = i;
break;
}
};
return ret;
}
void erase_by_regex(std::string &src, const char *pattern)
{
std::vector<RegexResult> result;
while (1)
{
int count = find_by_regex(src, pattern, result);
if (0 == count) {
break;
}
src.erase(result[0].nFrom, result[0].nTo - result[0].nFrom);
};
}
void parse_vars(const std::string &line, std::vector<std::pair<std::string, std::string> > &vars)
{
std::vector<RegexResult> result;
int count = find_by_regex(line, "(\\w+)\\s+([\\w\\s\\:\\+\\[\\]]+)(?:\\s*,\\s*([\\w\\s\\:\\+\\[\\]]+))*;", result);
if (count < 3) {
return;
}
std::string t;
t.assign(line.c_str(), result[1].nFrom, result[1].nTo - result[1].nFrom);
for (int i = 2; i < count; ++i)
{
std::string v;
v.assign(line.c_str(), result[i].nFrom, result[i].nTo - result[i].nFrom);
vars.push_back(std::pair<std::string, std::string>(t, v));
}
}
void parse_enums(const std::string &line, std::vector<std::pair<std::string, std::string> > &enums)
{
std::vector<RegexResult> result;
int count = find_by_regex(line, "(\\w+)\\s*(?:=\\s*(\\w+\\s*[<|-|+]*\\s*\\w*\\s*[-|+]*\\s*\\w*)\\s*)*[,]*", result);
if (count < 2) {
return;
}
std::string t;
t.assign(line.c_str(), result[1].nFrom, result[1].nTo - result[1].nFrom);
std::string v;
if (count > 2) {
v.assign(line.c_str(), result[2].nFrom, result[2].nTo - result[2].nFrom);
}
enums.push_back(std::pair<std::string, std::string>(t, v));
}
static void output(std::string &out, const char *fmt, ...)
{
char buffer[2048];
va_list arg;
va_start(arg, fmt);
int nRet = vsnprintf(buffer, 2047, fmt, arg);
va_end(arg);
out += "\n";
out += buffer;
}
static void output_test(std::string &out, const char *test_type, std::string &name, int &test_counter, const char *type_name = NULL)
{
char tmp[512];
if (strcmp(test_type, "READ_TEST_Struct") == 0 && type_name)
{
const char *naked_type = type_name;
const char *ns = strrchr(type_name, ':');
if (ns) {
naked_type = ns + 1;
}
sprintf(tmp, "%s(%s, %s)", test_type, name.c_str(), naked_type);
}
else {
sprintf(tmp, "%s(%s)", test_type, name.c_str());
}
out += (test_counter == 0 ? "\n\t" : "\n\telse ");
out += tmp;
++test_counter;
}
void save_file(const char *fn, const std::string &out)
{
FILE *f = fopen(fn, "w");
if (f)
{
fwrite(out.c_str(), out.size(), 1, f);
fclose(f);
}
}
void split_string(const char *str, std::vector<std::string> &v)
{
std::string t;
const char *from = NULL;
for (const char *i = str; ; ++i)
{
if (*i == ' ' || *i == '\t' || *i == '\r' || *i == '\n' || *i == 0)
{
if (from) {
t.assign(from, 0, i - from);
v.push_back(t);
from = NULL;
}
}
else if (NULL == from) {
from = i;
}
if (*i == 0) {
break;
}
}
}
bool start_with(std::string str, std::string start) {
return str.compare(0, start.size(), start) == 0;
}
int read_line(const std::string &src, int from, std::string &line)
{
int line_end = find_char(src.c_str(), from, '\n');
if (line_end < 0){
line_end = src.size();
}
if (line_end > from) {
std::string line_tmp;
line_tmp.assign(src.c_str(), from, line_end - from);
if (find_char(line_tmp.c_str(), 0, '(') >= 0) {
const char *s = src.c_str() + from;
int count = 0;
bool bHasSeg = false;
while (s) {
if (*s == '{') {
count++;
bHasSeg = true;
}
if (*s == '}') {
count--;
}
if ((!bHasSeg && *s == ';') || (bHasSeg && count == 0)) {
break;
}
from++;
s++;
}
line_end = find_char(src.c_str(), from, '\n');
if (line_end < 0){
line_end = src.size();
}
line.clear();
}else {
line.assign(src.c_str(), from, line_end - from);
}
}
else {
line.clear();
}
return line_end + 1;
}
struct StructMemberInfo
{
std::string sName;
std::string sType;
std::string sArrayLen;
std::string sArrayLen1;
std::string sLuaDataFunc;
std::string sLen;
void setType(std::string typ)
{
sType = typ;
sLen = "0";
if ((sType == "char" && sArrayLen.empty()) || sType == "Sint8") {
sLuaDataFunc = "GetSint8";
sLen = "1";
}
else if (sType == "Uint8") {
sLuaDataFunc = "GetUint8";
sLen = "1";
}
else if (sType == "Sint16") {
sLuaDataFunc = "GetSint16";
sLen = "2";
}
else if (sType == "Uint16" || sType == "DataHead") {
sLuaDataFunc = "GetUint16";
sLen = "2";
}
else if (sType == "Sint32") {
sLuaDataFunc = "GetSint32";
sLen = "4";
}
else if (sType == "Uint32") {
sLuaDataFunc = "GetUint32";
sLen = "4";
}
else if (sType == "float") {
sLuaDataFunc = "GetFloat";
sLen = "4";
}
else if (sType == "double") {
sLuaDataFunc = "GetDouble";
sLen = "4";
}
else if (sType == "char" && !sArrayLen.empty()) {
sLuaDataFunc = "GetString";
}
else if (sType == "bool") {
sLuaDataFunc = "GetBoolean";
sLen = "1";
}
else {
std::ostringstream o;
o << "_G.C2Lua.readData(t, \\\"" << sType <<"\\\")";
sLuaDataFunc = o.str();
o.str("");
o << "_G.C2Lua.sizeof(\\\"" << sType <<"\\\")";
sLen = o.str();
}
}
int getType() const
{
int typ = -1;
bool bArray = !sArrayLen.empty();
if (sType == "char" || sType == "Sint8") {
typ = 0;
}
else if (sType == "Uint8") {
typ = 1;
}
else if (sType == "Sint16") {
typ = 2;
}
else if (sType == "Uint16" || sType == "DataHead") {
typ = 3;
}
else if (sType == "Sint32") {
typ = 4;
}
else if (sType == "Uint32") {
typ = 5;
}
else if (sType == "float") {
typ = 6;
}
else if (sType == "double") {
typ = 7;
}
else if (sType == "bool") {
typ = 10;
}
if (bArray) {
if (sType == "char")
{
typ = 8;
if (!sArrayLen1.empty()) {
typ = 9;
}
}else {
typ += 100;
}
}
return typ;
}
};
struct StructInfo
{
std::string sName, sNamespaceStruct, sNamespace;
std::vector<StructMemberInfo> vMember;
};
int parse_struct_definition(std::set<std::string> &vHeader, std::vector<StructInfo> &vStructs, const char *sNamespaceStruct, const char *sHeader, const std::set<std::string> &filter_vars)
{
printf("Export %s in %s to lua...\n", sNamespaceStruct, sHeader);
// extract struct name
std::string struct_name = sNamespaceStruct;
std::string struct_namespace;
const char *namespace_end = strrchr(sNamespaceStruct, ':');
if (namespace_end) {
struct_name.assign(namespace_end+1, 0, sNamespaceStruct + strlen(sNamespaceStruct) - (namespace_end+1));
struct_namespace.assign(sNamespaceStruct, 0, namespace_end - sNamespaceStruct - 1);
}
// extract header
std::string header = sHeader;
const char *slash = NULL;
const char *have_include = strstr(sHeader, "/include/");
if (have_include) {
slash = strchr(have_include + 10, '/');
}
else {
slash = strrchr(sHeader, '/');
}
if (slash) {
header.assign(slash+1, 0, sHeader + strlen(sHeader) - (slash+1));
}
// load header
std::string src;
read_file(sHeader, src);
// lookup struct definition
std::vector<RegexResult> result;
char pattern[256];
_snprintf(pattern, 255, "\\bstruct\\s*(?:SU_LIB_COMMON)?\\s*%s\\s*\\{", struct_name.c_str());
int count = find_by_regex(src, pattern, result);
if (0 == count) {
_snprintf(pattern, 255, "\\bclass\\s*(?:SU_LIB_COMMON)?\\s*%s\\s*\\{", struct_name.c_str());
count = find_by_regex(src, pattern, result);
}
if (0 == count) {
_snprintf(pattern, 255, "\\bstruct\\s*(?:SU_LIB_COMMON)?\\s*%s\\s*:\\s*(?:public|private|protected)?\\s*(?:\\w*::)*(\\w+)\\s*\\{", struct_name.c_str());
count = find_by_regex(src, pattern, result);
}
if (0 == count) {
_snprintf(pattern, 255, "\\bclass\\s*(?:SU_LIB_COMMON)?\\s*%s\\s*:\\s*(?:public|private|protected)?\\s*(?:\\w*::)*(\\w+)\\s*\\{", struct_name.c_str());
count = find_by_regex(src, pattern, result);
}
if (0 == count) {
printf("Can't find %s definition in %s\r\n", sNamespaceStruct, sHeader);
return 0;
}
int struct_end = find_char(src.c_str(), result[0].nTo + 1, '}', '{');
if (struct_end < 0)
{
printf("Can't find %s definition-end\r\n", pattern);
return 0;
}
std::string struct_content;
struct_content.assign(src.c_str() + result[0].nTo, struct_end - 1 - result[0].nTo);
//struct inherit
std::string sNamespaceStructInherit;
std::string sStructInherit;
if (count > 1) {
sNamespaceStructInherit.assign(src.c_str(), result[1].nFrom, result[1].nTo - result[1].nFrom);
const char *namespace_end = strrchr(sNamespaceStructInherit.c_str(), ':');
if (namespace_end) {
sStructInherit.assign(namespace_end+1, 0, sNamespaceStruct + strlen(sNamespaceStruct) - (namespace_end+1));
}
}
//vStructs.push_back(struct_name);
vHeader.insert(header);
StructInfo si;
si.sName = struct_name;
si.sNamespaceStruct = sNamespaceStruct;
si.sNamespace = struct_namespace;
// 去除注释
erase_by_regex(struct_content, "\\/\\*(.*\\s*)+?\\*\\/");
erase_by_regex(struct_content, "\\/\\/.*");
// 读入每行并分析
std::vector<std::pair<std::string, std::string> > vars;
int line_from = 0;
std::string line;
do
{
line_from = read_line(struct_content, line_from, line);
if (!line.empty()) {
parse_vars(line, vars);
}
} while (line_from < (int)struct_content.size());
for (unsigned i = 0; i < vars.size(); ++i)
{
StructMemberInfo mi;
//mi.sType = vars[i].first;
std::string &v = vars[i].second;
std::string::size_type f = v.find('[');
if (f != std::string::npos) {
mi.sName.assign(v.c_str(), 0, f);
std::string sArrayLen;
sArrayLen.assign(v.c_str(), f+1, v.size() - f - 2);
std::string::size_type af = sArrayLen.find(']');
if (af != std::string::npos)
{
mi.sArrayLen.assign(sArrayLen.c_str(), 0, af);
mi.sArrayLen1.assign(sArrayLen.c_str(), af + 2, sArrayLen.length() - af - 1);
}
else
{
mi.sArrayLen = sArrayLen;
mi.sArrayLen1.clear();
}
}
else {
mi.sName = v;
}
mi.setType(vars[i].first);
if (filter_vars.empty() || filter_vars.find(mi.sName) != filter_vars.end()) {
si.vMember.push_back(mi);
}
}
if (!sNamespaceStructInherit.empty()) {
std::vector<StructInfo>::iterator it = vStructs.begin();
for (;it != vStructs.end(); ++it) {
if (it->sNamespaceStruct.compare(sNamespaceStructInherit) == 0
|| it->sName.compare(sNamespaceStructInherit) == 0) {
for (unsigned i = 0; i < it->vMember.size(); ++i) {
si.vMember.push_back(it->vMember[i]);
}
break;
}
}
}
vStructs.push_back(si);
/*
// export to cpp
//luaSizeofStruct
output(out, "int luaSizeof%s(lua_State *L_) {", struct_name.c_str());
output(out, "\tlua_pushinteger(L_, sizeof(%s));", sNamespaceStruct);
output(out, "\treturn 1;");
output(out, "}\n");
//luaReadStruct
output(out, "int luaRead%s(lua_State *L_) {", struct_name.c_str());
output(out, "\tC2LuaValue val;");
output(out, "\t%s *data = (%s*)getC2LuaStructPtr(L_, val, 1);", sNamespaceStruct, sNamespaceStruct);
output(out, "\tconst char *member = luaL_checkstring(L_, 2);\n");
int test_count = 0;
for (unsigned i = 0; i < vars.size(); ++i)
{
if (!filter_vars.empty() && filter_vars.find(vars[i].second) == filter_vars.end()) {
continue;
}
if (vars[i].first == "char" || vars[i].first == "Sint8") {
output_test(out, "READ_TEST_Sint8", vars[i].second, test_count);
}
else if (vars[i].first == "Uint8") {
output_test(out, "READ_TEST_Uint8", vars[i].second, test_count);
}
else if (vars[i].first == "Sint16") {
output_test(out, "READ_TEST_Sint16", vars[i].second, test_count);
}
else if (vars[i].first == "Uint16" || vars[i].first == "DataHead") {
output_test(out, "READ_TEST_Uint16", vars[i].second, test_count);
}
else if (vars[i].first == "Sint32") {
output_test(out, "READ_TEST_Sint32", vars[i].second, test_count);
}
else if (vars[i].first == "Uint32") {
output_test(out, "READ_TEST_Uint32", vars[i].second, test_count);
}
else if (vars[i].first == "float") {
output_test(out, "READ_TEST_float", vars[i].second, test_count);
}
else if (vars[i].first == "double") {
output_test(out, "READ_TEST_double", vars[i].second, test_count);
}
else
{
if (vars[i].first.find("[]") != std::string::npos)
{
if (vars[i].first == "char[]") {
output_test(out, "READ_TEST_String", vars[i].second, test_count);
}
else {
output_test(out, "READ_TEST_Array", vars[i].second, test_count);
}
}
else {
output_test(out, "READ_TEST_Struct", vars[i].second, test_count, vars[i].first.c_str());
}
}
}
output(out, "\treturn val.tolua(L_);\n}\n");
//luaWriteStruct
output(out, "int luaWrite%s(lua_State *L_) {", struct_name.c_str());
output(out, "\tC2LuaValue val;");
output(out, "\t%s *data = (%s*)getC2LuaStructPtr(L_, val, 1);", sNamespaceStruct, sNamespaceStruct);
output(out, "\tconst char *member = luaL_checkstring(L_, 2);\n");
test_count = 0;
for (unsigned i = 0; i < vars.size(); ++i)
{
if (!filter_vars.empty() && filter_vars.find(vars[i].second) == filter_vars.end()) {
continue;
}
if (vars[i].first == "char" || vars[i].first == "Sint8") {
output_test(out, "WRITE_TEST_Sint8", vars[i].second, test_count);
}
else if (vars[i].first == "Uint8") {
output_test(out, "WRITE_TEST_Uint8", vars[i].second, test_count);
}
else if (vars[i].first == "Sint16") {
output_test(out, "WRITE_TEST_Sint16", vars[i].second, test_count);
}
else if (vars[i].first == "Uint16" || vars[i].first == "DataHead") {
output_test(out, "WRITE_TEST_Uint16", vars[i].second, test_count);
}
else if (vars[i].first == "Sint32") {
output_test(out, "WRITE_TEST_Sint32", vars[i].second, test_count);
}
else if (vars[i].first == "Uint32") {
output_test(out, "WRITE_TEST_Uint32", vars[i].second, test_count);
}
else if (vars[i].first == "float") {
output_test(out, "WRITE_TEST_float", vars[i].second, test_count);
}
else if (vars[i].first == "double") {
output_test(out, "WRITE_TEST_double", vars[i].second, test_count);
}
else if (vars[i].first == "char[]") {
output_test(out, "WRITE_TEST_String", vars[i].second, test_count);
}
}
output(out, "\tval.toc();");
output(out, "\treturn 0;\n}\n");
*/
return 0;
}
struct EnumMemberInfo
{
std::string sName;
std::string sExpression;
int nType;
};
struct EnumInfo
{
std::string sName, sNamespaceEnum, sNamespace;
std::vector<EnumMemberInfo> vMember;
};
int parse_enum_definition(std::set<std::string> &vHeader, std::vector<EnumInfo> &vEnums, const char *sNamespaceEnum, const char *sHeader)
{
printf("Export %s in %s to lua...\n", sNamespaceEnum, sHeader);
// extract struct name
std::string enum_name = sNamespaceEnum;
std::string enum_namespace;
const char *namespace_end = strrchr(sNamespaceEnum, ':');
if (namespace_end) {
enum_name.assign(namespace_end+1, 0, sNamespaceEnum + strlen(sNamespaceEnum) - (namespace_end+1));
enum_namespace.assign(sNamespaceEnum, 0, namespace_end - sNamespaceEnum - 1);
}
// extract header
std::string header = sHeader;
const char *slash = NULL;
const char *have_include = strstr(sHeader, "/include/");
if (have_include) {
slash = strchr(have_include + 10, '/');
}
else {
slash = strrchr(sHeader, '/');
}
if (slash) {
header.assign(slash+1, 0, sHeader + strlen(sHeader) - (slash+1));
}
// load header
std::string src;
read_file(sHeader, src);
// lookup struct definition
std::vector<RegexResult> result;
char pattern[256];
_snprintf(pattern, 255, "\\benum\\s*%s\\s*\\{", enum_name.c_str());
int count = find_by_regex(src, pattern, result);
if (0 == count) {
printf("Can't find %s definition in %s\r\n", sNamespaceEnum, sHeader);
return 0;
}
int struct_end = find_char(src.c_str(), result[0].nTo + 1, '}', '{');
if (struct_end < 0)
{
printf("Can't find %s definition-end\r\n", pattern);
return 0;
}
std::string struct_content;
struct_content.assign(src.c_str() + result[0].nTo, struct_end - 1 - result[0].nTo);
//vStructs.push_back(struct_name);
vHeader.insert(header);
EnumInfo ei;
ei.sName = enum_name;
ei.sNamespaceEnum = sNamespaceEnum;
ei.sNamespace = enum_namespace;
// 去除注释
erase_by_regex(struct_content, "\\/\\*(.*\\s*)+?\\*\\/");
erase_by_regex(struct_content, "\\/\\/.*");
// 读入每行并分析
std::vector<std::pair<std::string, std::string> > enums;
int line_from = 0;
std::string line;
do
{
line_from = read_line(struct_content, line_from, line);
if (!line.empty()) {
parse_enums(line, enums);
}
} while (line_from < (int)struct_content.size());
for (unsigned i = 0; i < enums.size(); ++i)
{
EnumMemberInfo mi;
//mi.sType = vars[i].first;
std::string &v = enums[i].second;
mi.sName = enums[i].first;
mi.sExpression = enums[i].second;
mi.nType = 0;
if (!mi.sExpression.empty()) {
std::vector<RegexResult> r;
int count = find_by_regex(mi.sExpression, "^[0-9]+$", r);
if (0 == count) {
mi.nType = 1;
} else {
mi.nType = 2;
}
}
ei.vMember.push_back(mi);
}
vEnums.push_back(ei);
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 2) {
printf("Usage: c2lua.exe /path/to/c2lua_list.txt\r\n");
printf("\t c2lua_list.txt content should like this:\n");
printf("\t ../../include/summer/common/nmsgdefine.h summer::ActTimeRange\n");
printf("\t ../../include/summer/common/nmsgother.h summer::PlayerData nDBID szRoleName nIcon\n");
printf("\t ../../include/summer/common/nmsgother.h summer::GMM2C_Login data nMipVip marketingOpen marketingShow peakPVPRestHour\n");
printf("\t ../../include/summer/common/nmsgguild.h summer::GMM2C_Guild_Announce sAnnoucement\n");
return 0;
}
std::set<std::string> headers; //用到的头文件列表
std::vector<StructInfo> structs; //导出的结构名列表
std::vector<EnumInfo> enums; //导出的枚举列表
// for each-line in export_list.c2lua
std::string export_list_src;
read_file(argv[1], export_list_src);
int line_from = 0;
std::string line;
do
{
line_from = read_line(export_list_src, line_from, line);
if (!line.empty())
{
std::vector<std::string> strs;
split_string(line.c_str(), strs);
if (strs.size() >= 2)
{
if (start_with(line, "enum:"))
{
parse_enum_definition(headers, enums, strs[1].c_str(), strs[0].substr(5).c_str());
}
else {
std::set<std::string> filter;
for (unsigned i = 2; i < strs.size(); ++i) {
filter.insert(strs[i]);
}
parse_struct_definition(headers, structs, strs[1].c_str(), strs[0].c_str(), filter);
}
}
}
} while (line_from < (int)export_list_src.size());
// export to cpp
// head
std::string out = "//This file is auto-generated by c2lua.\n//Don't modify anything manually!\n";
output(out, "#include \"stdafx.h\"");
output(out, "#include <kernel/logger.h>");
output(out, "#include <kernel/autofile.h>");
{
std::set<std::string>::iterator i = headers.begin(), iend = headers.end();
for (; i != iend; ++i) {
output(out, "#include \"%s\"", i->c_str());
}
}
output(out, "\nusing namespace new3d::kernel;");
output(out, "using namespace summer;\n");
output(out, "static void output(std::string &out, const char *fmt, ...) {");
output(out, "\tchar buffer[2048];");
output(out, "\tva_list arg;");
output(out, "\tva_start(arg, fmt);");
output(out, "\tint nRet = vsnprintf(buffer, 2047, fmt, arg);");
output(out, "\tva_end(arg);");
output(out, "\tout += \"\\r\\n\";");
output(out, "\tout += buffer;");
output(out, "}\n");
output(out, "void output_c2luaimpl_lua() {");
output(out, "\tstd::string out;");
// for each struct.
for (unsigned i = 0; i < structs.size(); ++i)
{
const StructInfo &si = structs[i];
output(out, "\tout += \"function C2Lua.Sizeof%s()\";", si.sName.c_str());
output(out, "\toutput(out, \"\\treturn %%d;\\r\\n\", sizeof(%s));", si.sNamespaceStruct.c_str());
output(out, "\tout += \"end;\\r\\n\\r\\n\";\n");
//offset of member
output(out, "\tout += \"function C2Lua.Offset%s(sVarName)\\r\\n\";", si.sName.c_str());
output(out, "\tout += \"\\tlocal typ, off = 0;\\r\\n\";");
for (unsigned j = 0; j < si.vMember.size(); ++j)
{
const StructMemberInfo &mi = si.vMember[j];
if (0 == j) {
output(out, "\tout += \"\\tif \\\"%s\\\" == sVarName then\\r\\n\";", mi.sName.c_str());
}
else {
output(out, "\tout += \"\\telseif \\\"%s\\\" == sVarName then\\r\\n\";", mi.sName.c_str());
}
int typ = mi.getType();
if (typ >= 0) {
output(out, "\tout += \"\\t\\ttyp = %d;\";", typ);
}
else {
output(out, "\tout += \"\\t\\ttyp = \\\"%s\\\";\";", mi.sType.c_str());
}
output(out, "\toutput(out, \"\\t\\toff = %%d;\\r\\n\", (int)&((%s*)0)->%s);", si.sNamespaceStruct.c_str(), mi.sName.c_str());
}
output(out, "\tout += \"\\tend;\\r\\n\";");
output(out, "\tout += \"\\treturn typ, off;\\r\\n\";");
output(out, "\tout += \"end;\\r\\n\\r\\n\";\n");
//copy member to table;
output(out, "\tout += \"function C2Lua.Extract%s(obj)\\r\\n\";", si.sName.c_str());
output(out, "\tout += \"\\tlocal extract = {};\\r\\n\";");
for (unsigned j = 0; j < si.vMember.size(); ++j)
{
const StructMemberInfo &mi = si.vMember[j];
int typ = mi.getType();
std::string::size_type pos = 0;
if (typ >= 99 && (pos = mi.sLuaDataFunc.find("_G.C2Lua", pos)) != std::string::npos) {
output(out, "\tout += \"\\textract.%s = C2Lua.readDataEx(obj, \\\"%s\\\", true);\\r\\n\";", mi.sName.c_str(), mi.sName.c_str());
}
else if (typ >= 0) {
output(out, "\tout += \"\\textract.%s = obj.%s;\\r\\n\";", mi.sName.c_str(), mi.sName.c_str());
}
else {
output(out, "\tout += \"\\textract.%s = C2Lua.Extract%s(obj.%s);\\r\\n\";", mi.sName.c_str(), mi.sType.c_str(), mi.sName.c_str());
}
}
output(out, "\tout += \"\\treturn extract;\\r\\n\";");
output(out, "\tout += \"end;\\r\\n\\r\\n\";\n");
//get array data
output(out, "\tout += \"function C2Lua.GetArrayData%s(buff, offset, sVarName, bExtract)\\r\\n\";", si.sName.c_str());
output(out, "\tout += \"\\tlocal ret = {};\\r\\n\";");
unsigned count = 0;
for (unsigned j = 0; j < si.vMember.size(); ++j)
{
const StructMemberInfo &mi = si.vMember[j];
if (mi.sArrayLen.empty())
continue;
if (0 == count) {
output(out, "\tout += \"\\tif \\\"%s\\\" == sVarName then\\r\\n\";", mi.sName.c_str());
}
else {
output(out, "\tout += \"\\telseif \\\"%s\\\" == sVarName then\\r\\n\";", mi.sName.c_str());
}
if (mi.getType() == 8) { //string
output(out, "\tout += \"\\t\\treturn buff:%s(offset);\\r\\n\";", mi.sLuaDataFunc.c_str());
}
else if (mi.getType() == 9) { //string array
output(out, "\toutput(out, \"\\t\\tfor i = 0,%%d do\\r\\n\", %s - 1);", mi.sArrayLen.c_str());
output(out, "\toutput(out, \"\\t\\t\\t\\tret[i] = buff:%s(offset + i*%%d);\\r\\n\", %s);", mi.sLuaDataFunc.c_str(), mi.sArrayLen1.c_str());
output(out, "\tout += \"\\t\\tend;\\r\\n\";");
}
else
{
output(out, "\toutput(out, \"\\t\\tfor i = 0,%%d do\\r\\n\", %s - 1);", mi.sArrayLen.c_str());
if (mi.sArrayLen1.empty()) {
std::string::size_type pos = 0;
if ((pos = mi.sLuaDataFunc.find("_G.C2Lua", pos)) != std::string::npos) {
output(out, "\tout += \"\\t\\t\\tret[i] = C2Lua.locate(buff, offset + i*%s, \\\"%s\\\");\\r\\n\";", mi.sLen.c_str(), mi.sType.c_str());
output(out, "\tout += \"\\t\\t\\tif bExtract == true then\\r\\n\";");
output(out, "\tout += \"\\t\\t\\t\\tlocal tmp = C2Lua.Extract%s(ret[i]);\\r\\n\";", mi.sType.c_str());
output(out, "\tout += \"\\t\\t\\t\\tret[i] = tmp;\\r\\n\";");
output(out, "\tout += \"\\t\\t\\tend;\\r\\n\";");
} else {
output(out, "\tout += \"\\t\\t\\tret[i] = buff:%s(offset + i*%s);\\r\\n\";", mi.sLuaDataFunc.c_str(), mi.sLen.c_str());
}
} else {
output(out, "\tout += \"\\t\\t\\tlocal item = {};\\r\\n\";");
output(out, "\toutput(out, \"\\t\\t\\tfor j = 0,%%d do\\r\\n\", %s - 1);", mi.sArrayLen1.c_str());
std::string::size_type pos = 0;
if ((pos = mi.sLuaDataFunc.find("_G.C2Lua", pos)) != std::string::npos) {
output(out, "\toutput(out, \"\\t\\t\\t\\titem[i] = C2Lua.locate(buff, offset + i*%%d + j*%s, \\\" %s\\\");\\r\\n\", sizeof(((%s*)0)->%s[0]));", mi.sLen.c_str(), mi.sType.c_str(), si.sNamespaceStruct.c_str(), mi.sName.c_str());
output(out, "\tout += \"\\t\\t\\tif bExtract == true then\\r\\n\";");
output(out, "\tout += \"\\t\\t\\tlocal tmp = C2Lua.Extract%s(item[i]);\\r\\n\";", mi.sType.c_str());
output(out, "\tout += \"\\t\\t\\titem[i] = tmp;\\r\\n\";");
output(out, "\tout += \"\\t\\t\\tend;\\r\\n\";");
} else {
output(out, "\toutput(out, \"\\t\\t\\t\\titem[i] = buff:%s(offset + i*%%d + j*%s);\\r\\n\", sizeof(((%s*)0)->%s[0]));", mi.sLuaDataFunc.c_str(), mi.sLen.c_str(), si.sNamespaceStruct.c_str(), mi.sName.c_str());
}
output(out, "\tout += \"\\t\\t\\tend;\\r\\n\";");
output(out, "\tout += \"\\t\\t\\tret[i] = item;\\r\\n\";");
}
output(out, "\tout += \"\\t\\tend;\\r\\n\";");
}
count++;
}
if (count) {
output(out, "\tout += \"\\telse\\r\\n\";");
output(out, "\tout += \"\\t\\t_G.LogInfo(sVarName..\\\" is not a array in %s\\\");\\r\\n\";", si.sName.c_str());
output(out, "\tout += \"\\tend;\\r\\n\";");
}
output(out, "\tout += \"\\treturn ret;\\r\\n\";");
output(out, "\tout += \"end;\\r\\n\\r\\n\";\n");
}
// for each enums.
output(out, "\tint nIndex;");
for (unsigned i = 0; i < enums.size(); ++i)
{
const EnumInfo &ei = enums[i];
output(out, "\tnIndex = -1;");
output(out, "\tout += \"\\r\\n\\r\\n\";");
output(out, "\tout += \"%s = {};\";", ei.sName.c_str());
for (unsigned j = 0; j < ei.vMember.size(); ++j) {