Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / script / api / squirrel_export.awk
blobcc8c9f925a335fe5599dda91c88c8e3bddf106df
1 # $Id: squirrel_export.awk 24288 2012-05-26 14:16:27Z frosch $
3 # This file is part of OpenTTD.
4 # OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
5 # OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
6 # See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
9 # Awk script to automatically generate the code needed
10 # to export the script APIs to Squirrel.
12 # Note that arrays are 1 based...
15 # Simple insertion sort.
16 function array_sort(ARRAY, ELEMENTS, temp, i, j)
18 for (i = 2; i <= ELEMENTS; i++)
19 for (j = i; ARRAY[j - 1] > ARRAY[j]; --j) {
20 temp = ARRAY[j]
21 ARRAY[j] = ARRAY[j - 1]
22 ARRAY[j - 1] = temp
24 return
27 function dump_class_templates(name)
29 realname = name
30 gsub("^Script", "", realname)
32 print " template <> inline " name " *GetParam(ForceType<" name " *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (" name " *)instance; }"
33 print " template <> inline " name " &GetParam(ForceType<" name " &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(" name " *)instance; }"
34 print " template <> inline const " name " *GetParam(ForceType<const " name " *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (" name " *)instance; }"
35 print " template <> inline const " name " &GetParam(ForceType<const " name " &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(" name " *)instance; }"
36 if (name == "ScriptEvent") {
37 print " template <> inline int Return<" name " *>(HSQUIRRELVM vm, " name " *res) { if (res == NULL) { sq_pushnull(vm); return 1; } Squirrel::CreateClassInstanceVM(vm, \"" realname "\", res, NULL, DefSQDestructorCallback<" name ">, true); return 1; }"
38 } else if (name == "ScriptText") {
39 print ""
40 print " template <> inline Text *GetParam(ForceType<Text *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) {"
41 print " if (sq_gettype(vm, index) == OT_INSTANCE) {"
42 print " return GetParam(ForceType<ScriptText *>(), vm, index, ptr);"
43 print " }"
44 print " if (sq_gettype(vm, index) == OT_STRING) {"
45 print " return new RawText(GetParam(ForceType<const char *>(), vm, index, ptr));"
46 print " }"
47 print " return NULL;"
48 print " }"
49 } else {
50 print " template <> inline int Return<" name " *>(HSQUIRRELVM vm, " name " *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, \"" realname "\", res, NULL, DefSQDestructorCallback<" name ">, true); return 1; }"
54 function dump_fileheader()
56 # Break the Id tag, so SVN doesn't replace it
57 print "/* $I" "d$ */"
58 print ""
59 print "/*"
60 print " * This file is part of OpenTTD."
61 print " * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2."
62 print " * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
63 print " * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>."
64 print " */"
65 print ""
66 print "/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */"
67 print ""
68 print "#include \"../" filename "\""
69 if (api != "Template") {
70 gsub("script_", "template_", filename)
71 print "#include \"../template/" filename ".sq\""
75 function reset_reader()
77 enum_size = 0
78 enum_value_size = 0
79 enum_string_to_error_size = 0
80 enum_error_to_string_size = 0
81 struct_size = 0
82 method_size = 0
83 static_method_size = 0
84 cls = ""
85 start_squirrel_define_on_next_line = "false"
86 cls_level = 0
87 cls_in_api = ""
90 BEGIN {
91 enum_size = 0
92 enum_value_size = 0
93 enum_string_to_error_size = 0
94 enum_error_to_string_size = 0
95 const_size = 0
96 struct_size = 0
97 method_size = 0
98 static_method_size = 0
99 super_cls = ""
100 cls = ""
101 api_selected = ""
102 cls_in_api = ""
103 start_squirrel_define_on_next_line = "false"
104 has_fileheader = "false"
105 cls_level = 0
106 RS = "\r|\n"
107 apis = tolower(api)
108 if (apis == "gs") apis = "game"
111 /@file/ {
112 filename = $3
113 gsub("^" apis "_", "script_", filename)
116 # Ignore special doxygen blocks
117 /^#ifndef DOXYGEN_API/ { doxygen_skip = "next"; next; }
118 /^#ifdef DOXYGEN_API/ { doxygen_skip = "true"; next; }
119 /^#endif \/\* DOXYGEN_API \*\// { doxygen_skip = "false"; next; }
120 /^#else/ {
121 if (doxygen_skip == "next") {
122 doxygen_skip = "true";
123 } else {
124 doxygen_skip = "false";
126 next;
128 { if (doxygen_skip == "true") next }
130 /^([ ]*)\* @api/ {
131 # By default, classes are not selected
132 if (cls_level == 0) api_selected = "false"
134 gsub("^([ ]*)", "", $0)
135 gsub("* @api ", "", $0)
137 if (api == "Template") {
138 api_selected = "true"
139 if ($0 == "none" || $0 == "-all") api_selected = "false"
140 next
143 if ($0 == "none") {
144 api_selected = "false"
145 } else if ($0 == "-all") {
146 api_selected = "false"
147 } else if (match($0, "-" apis)) {
148 api_selected = "false"
149 } else if (match($0, apis)) {
150 api_selected = "true"
153 next
156 # Remove the old squirrel stuff
157 /#ifdef DEFINE_SQUIRREL_CLASS/ { squirrel_stuff = "true"; next; }
158 /^#endif \/\* DEFINE_SQUIRREL_CLASS \*\// { if (squirrel_stuff == "true") { squirrel_stuff = "false"; next; } }
159 { if (squirrel_stuff == "true") next; }
161 # Ignore forward declarations of classes
162 /^( *)class(.*);/ { next; }
163 # We only want to have public functions exported for now
164 /^( *)class/ {
165 if (cls_level == 0) {
166 if (api_selected == "") {
167 print "Class '"$2"' has no @api. It won't be published to any API." > "/dev/stderr"
168 api_selected = "false"
170 public = "false"
171 cls_param[0] = ""
172 cls_param[1] = 1
173 cls_param[2] = "x"
174 cls_in_api = api_selected
175 api_selected = ""
176 cls = $2
177 if (match($4, "public") || match($4, "protected") || match($4, "private")) {
178 super_cls = $5
179 } else {
180 super_cls = $4
182 } else if (cls_level == 1) {
183 if (api_selected == "") api_selected = cls_in_api
185 if (api_selected == "true") {
186 struct_size++
187 structs[struct_size] = cls "::" $2
189 api_selected = ""
191 cls_level++
192 next
194 /^( *)public/ { if (cls_level == 1) public = "true"; next; }
195 /^( *)protected/ { if (cls_level == 1) public = "false"; next; }
196 /^( *)private/ { if (cls_level == 1) public = "false"; next; }
198 # Ignore the comments
199 /^#/ { next; }
200 /\/\*.*\*\// { comment = "false"; next; }
201 /\/\*/ { comment = "true"; next; }
202 /\*\// { comment = "false"; next; }
203 { if (comment == "true") next }
205 # We need to make specialized conversions for structs
206 /^( *)struct/ {
207 cls_level++
209 # Check if we want to publish this struct
210 if (api_selected == "") api_selected = cls_in_api
211 if (api_selected == "false") {
212 api_selected = ""
213 next
215 api_selected = ""
217 if (public == "false") next
218 if (cls_level != 1) next
220 struct_size++
221 structs[struct_size] = cls "::" $2
223 next
226 # We need to make specialized conversions for enums
227 /^( *)enum/ {
228 cls_level++
230 # Check if we want to publish this enum
231 if (api_selected == "") api_selected = cls_in_api
232 if (api_selected == "false") {
233 api_selected = ""
234 next
236 api_selected = ""
238 if (public == "false") next
240 in_enum = "true"
241 enum_size++
242 enums[enum_size] = cls "::" $2
244 next
247 # Maybe the end of the class, if so we can start with the Squirrel export pretty soon
248 /};/ {
249 cls_level--
250 if (cls_level != 0) {
251 in_enum = "false";
252 next;
254 if (cls == "") {
255 next;
257 start_squirrel_define_on_next_line = "true"
258 next;
261 # Empty/white lines. When we may do the Squirrel export, do that export.
262 /^([ ]*)$/ {
263 if (start_squirrel_define_on_next_line == "false") next
265 if (cls_in_api != "true") {
266 reset_reader()
267 next
269 if (has_fileheader == "false") {
270 dump_fileheader()
271 has_fileheader = "true"
274 spaces = " ";
275 public = "false"
276 namespace_opened = "false"
278 api_cls = cls
279 gsub("^Script", api, api_cls)
280 api_super_cls = super_cls
281 gsub("^Script", api, api_super_cls)
283 print ""
285 if (api == "Template") {
286 # First check whether we have enums to print
287 if (enum_size != 0) {
288 if (namespace_opened == "false") {
289 print "namespace SQConvert {"
290 namespace_opened = "true"
292 print " /* Allow enums to be used as Squirrel parameters */"
293 for (i = 1; i <= enum_size; i++) {
294 print " template <> inline " enums[i] " GetParam(ForceType<" enums[i] ">, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (" enums[i] ")tmp; }"
295 print " template <> inline int Return<" enums[i] ">(HSQUIRRELVM vm, " enums[i] " res) { sq_pushinteger(vm, (int32)res); return 1; }"
296 delete enums[i]
300 # Then check whether we have structs/classes to print
301 if (struct_size != 0) {
302 if (namespace_opened == "false") {
303 print "namespace SQConvert {"
304 namespace_opened = "true"
306 print " /* Allow inner classes/structs to be used as Squirrel parameters */"
307 for (i = 1; i <= struct_size; i++) {
308 dump_class_templates(structs[i])
309 delete structs[i]
313 if (namespace_opened == "false") {
314 print "namespace SQConvert {"
315 namespace_opened = "true"
316 } else {
317 print ""
319 print " /* Allow " cls " to be used as Squirrel parameter */"
320 dump_class_templates(cls)
322 print "} // namespace SQConvert"
324 reset_reader()
325 next
328 print "";
329 print "template <> const char *GetClassName<" cls ", ST_" toupper(api) ">() { return \"" api_cls "\"; }"
330 print "";
332 # Then do the registration functions of the class. */
333 print "void SQ" api_cls "_Register(Squirrel *engine)"
334 print "{"
335 print " DefSQClass<" cls ", ST_" toupper(api) "> SQ" api_cls "(\"" api_cls "\");"
336 if (super_cls == "Text" || super_cls == "ScriptObject" || super_cls == "AIAbstractList::Valuator") {
337 print " SQ" api_cls ".PreRegister(engine);"
338 } else {
339 print " SQ" api_cls ".PreRegister(engine, \"" api_super_cls "\");"
341 if (super_cls != "ScriptEvent") {
342 if (cls_param[2] == "v") {
343 print " SQ" api_cls ".AddSQAdvancedConstructor(engine);"
344 } else {
345 print " SQ" api_cls ".AddConstructor<void (" cls "::*)(" cls_param[0] "), " cls_param[1]">(engine, \"" cls_param[2] "\");"
348 print ""
350 # Enum values
351 mlen = 0
352 for (i = 1; i <= enum_value_size; i++) {
353 if (mlen <= length(enum_value[i])) mlen = length(enum_value[i])
355 for (i = 1; i <= enum_value_size; i++) {
356 print " SQ" api_cls ".DefSQConst(engine, " cls "::" enum_value[i] ", " substr(spaces, 1, mlen - length(enum_value[i])) "\"" enum_value[i] "\");"
357 delete enum_value[i]
359 if (enum_value_size != 0) print ""
361 # Const values
362 mlen = 0
363 for (i = 1; i <= const_size; i++) {
364 if (mlen <= length(const_value[i])) mlen = length(const_value[i])
366 for (i = 1; i <= const_size; i++) {
367 print " SQ" api_cls ".DefSQConst(engine, " cls "::" const_value[i] ", " substr(spaces, 1, mlen - length(const_value[i])) "\"" const_value[i] "\");"
368 delete const_value[i]
370 if (const_size != 0) print ""
372 # Mapping of OTTD strings to errors
373 mlen = 0
374 for (i = 1; i <= enum_string_to_error_size; i++) {
375 if (mlen <= length(enum_string_to_error_mapping_string[i])) mlen = length(enum_string_to_error_mapping_string[i])
377 for (i = 1; i <= enum_string_to_error_size; i++) {
378 print " ScriptError::RegisterErrorMap(" enum_string_to_error_mapping_string[i] ", " substr(spaces, 1, mlen - length(enum_string_to_error_mapping_string[i])) cls "::" enum_string_to_error_mapping_error[i] ");"
380 delete enum_string_to_error_mapping_string[i]
382 if (enum_string_to_error_size != 0) print ""
384 # Mapping of errors to human 'readable' strings.
385 mlen = 0
386 for (i = 1; i <= enum_error_to_string_size; i++) {
387 if (mlen <= length(enum_error_to_string_mapping[i])) mlen = length(enum_error_to_string_mapping[i])
389 for (i = 1; i <= enum_error_to_string_size; i++) {
390 print " ScriptError::RegisterErrorMapString(" cls "::" enum_error_to_string_mapping[i] ", " substr(spaces, 1, mlen - length(enum_error_to_string_mapping[i])) "\"" enum_error_to_string_mapping[i] "\");"
391 delete enum_error_to_string_mapping[i]
393 if (enum_error_to_string_size != 0) print ""
395 # Static methods
396 mlen = 0
397 for (i = 1; i <= static_method_size; i++) {
398 if (mlen <= length(static_methods[i, 0])) mlen = length(static_methods[i, 0])
400 for (i = 1; i <= static_method_size; i++) {
401 if (static_methods[i, 2] == "v") {
402 print " SQ" api_cls ".DefSQAdvancedStaticMethod(engine, &" cls "::" static_methods[i, 0] ", " substr(spaces, 1, mlen - length(static_methods[i, 0]) - 8) "\"" static_methods[i, 0] "\");"
403 } else {
404 print " SQ" api_cls ".DefSQStaticMethod(engine, &" cls "::" static_methods[i, 0] ", " substr(spaces, 1, mlen - length(static_methods[i, 0])) "\"" static_methods[i, 0] "\", " substr(spaces, 1, mlen - length(static_methods[i, 0])) "" static_methods[i, 1] ", \"" static_methods[i, 2] "\");"
406 delete static_methods[i]
408 if (static_method_size != 0) print ""
410 # Non-static methods
411 mlen = 0
412 for (i = 1; i <= method_size; i++) {
413 if (mlen <= length(methods[i, 0])) mlen = length(methods[i, 0])
415 for (i = 1; i <= method_size; i++) {
416 if (methods[i, 2] == "v") {
417 print " SQ" api_cls ".DefSQAdvancedMethod(engine, &" cls "::" methods[i, 0] ", " substr(spaces, 1, mlen - length(methods[i, 0]) - 8) "\"" methods[i, 0] "\");"
418 } else {
419 print " SQ" api_cls ".DefSQMethod(engine, &" cls "::" methods[i, 0] ", " substr(spaces, 1, mlen - length(methods[i, 0])) "\"" methods[i, 0] "\", " substr(spaces, 1, mlen - length(methods[i, 0])) "" methods[i, 1] ", \"" methods[i, 2] "\");"
421 delete methods[i]
423 if (method_size != 0) print ""
425 print " SQ" api_cls ".PostRegister(engine);"
426 print "}"
428 reset_reader()
430 next
433 # Skip non-public functions
434 { if (public == "false") next }
436 # Add enums
438 if (in_enum == "true") {
439 enum_value_size++
440 sub(",", "", $1)
441 enum_value[enum_value_size] = $1
443 # Check if this a special error enum
444 if (match(enums[enum_size], ".*::ErrorMessages") != 0) {
445 # syntax:
446 # enum ErrorMessages {
447 # ERR_SOME_ERROR, // [STR_ITEM1, STR_ITEM2, ...]
450 # Set the mappings
451 if (match($0, "\\[.*\\]") != 0) {
452 mappings = substr($0, RSTART, RLENGTH);
453 gsub("([\\[[:space:]\\]])", "", mappings);
455 split(mappings, mapitems, ",");
456 for (i = 1; i <= length(mapitems); i++) {
457 enum_string_to_error_size++
458 enum_string_to_error_mapping_string[enum_string_to_error_size] = mapitems[i]
459 enum_string_to_error_mapping_error[enum_string_to_error_size] = $1
462 enum_error_to_string_size++
463 enum_error_to_string_mapping[enum_error_to_string_size] = $1
466 next
470 # Add a const (non-enum) value
471 /^[ ]*static const \w+ \w+ = -?\(?\w*\)?\w+;/ {
472 const_size++
473 const_value[const_size] = $4
474 next
477 # Add a method to the list
478 /^.*\(.*\).*$/ {
479 if (cls_level != 1) next
480 if (match($0, "~")) {
481 if (api_selected != "") {
482 print "Destructor for '"cls"' has @api. Tag ignored." > "/dev/stderr"
483 api_selected = ""
485 next
488 is_static = match($0, "static")
489 gsub("\\yvirtual\\y", "", $0)
490 gsub("\\ystatic\\y", "", $0)
491 gsub("\\yconst\\y", "", $0)
492 gsub("{.*", "", $0)
493 param_s = $0
494 gsub("\\*", "", $0)
495 gsub("\\(.*", "", $0)
497 sub(".*\\(", "", param_s)
498 sub("\\).*", "", param_s)
500 funcname = $2
501 if ($1 == cls && funcname == "") {
502 if (api_selected != "") {
503 print "Constructor for '"cls"' has @api. Tag ignored." > "/dev/stderr"
504 api_selected = ""
506 cls_param[0] = param_s
507 if (param_s == "") next
508 } else if (funcname == "") next
510 split(param_s, params, ",")
511 if (is_static) {
512 types = "."
513 } else {
514 types = "x"
516 for (len = 1; params[len] != ""; len++) {
517 sub("^[ ]*", "", params[len])
518 if (match(params[len], "\\*") || match(params[len], "&")) {
519 if (match(params[len], "^char")) {
520 # Many types can be converted to string, so use '.', not 's'. (handled by our glue code)
521 types = types "."
522 } else if (match(params[len], "^void")) {
523 types = types "p"
524 } else if (match(params[len], "^Array")) {
525 types = types "a"
526 } else if (match(params[len], "^struct Array")) {
527 types = types "a"
528 } else if (match(params[len], "^Text")) {
529 types = types "."
530 } else {
531 types = types "x"
533 } else if (match(params[len], "^bool")) {
534 types = types "b"
535 } else if (match(params[len], "^HSQUIRRELVM")) {
536 types = "v"
537 } else {
538 types = types "i"
542 # Check if we want to publish this function
543 if (api_selected == "") api_selected = cls_in_api
544 if (api_selected == "false") {
545 api_selected = ""
546 next
548 api_selected = ""
550 if ($1 == cls && funcname == "") {
551 cls_param[1] = len;
552 cls_param[2] = types;
553 } else if (substr(funcname, 0, 1) == "_" && types != "v") {
554 } else if (is_static) {
555 static_method_size++
556 static_methods[static_method_size, 0] = funcname
557 static_methods[static_method_size, 1] = len
558 static_methods[static_method_size, 2] = types
559 } else {
560 method_size++
561 methods[method_size, 0] = funcname
562 methods[method_size, 1] = len
563 methods[method_size, 2] = types
565 next