Files @ r27143:ecee9375b5d4
Branch filter:

Location: cpp/openttd-patchpack/source/cmake/scripts/SquirrelExport.cmake

Rubidium
Codechange: use string/fmt instead of printf for squirrel's PRINTFUNC
  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
cmake_minimum_required(VERSION 3.5)

if(NOT SCRIPT_API_SOURCE_FILE)
    message(FATAL_ERROR "Script needs SCRIPT_API_SOURCE_FILE defined")
endif()
if(NOT SCRIPT_API_BINARY_FILE)
    message(FATAL_ERROR "Script needs SCRIPT_API_BINARY_FILE defined")
endif()
if(NOT SCRIPT_API_FILE)
    message(FATAL_ERROR "Script needs SCRIPT_API_FILE defined")
endif()
if(NOT APIUC)
    message(FATAL_ERROR "Script needs APIUC defined")
endif()
if(NOT APILC)
    message(FATAL_ERROR "Script needs APILC defined")
endif()

macro(dump_fileheader)
    get_filename_component(SCRIPT_API_FILE_NAME "${SCRIPT_API_FILE}" NAME)
    string(APPEND SQUIRREL_EXPORT "\n#include \"../${SCRIPT_API_FILE_NAME}\"")
    if(NOT "${APIUC}" STREQUAL "Template")
        string(REPLACE "script_" "template_" SCRIPT_API_FILE_NAME "${SCRIPT_API_FILE_NAME}")
        string(APPEND SQUIRREL_EXPORT "\n#include \"../template/${SCRIPT_API_FILE_NAME}.sq\"")
    endif()
endmacro()

macro(dump_class_templates NAME)
    string(REGEX REPLACE "^Script" "" REALNAME ${NAME})

    string(APPEND SQUIRREL_EXPORT "\n	template <> struct Param<${NAME} *> { static inline ${NAME} *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return  (${NAME} *)instance; } };")
    string(APPEND SQUIRREL_EXPORT "\n	template <> struct Param<${NAME} &> { static inline ${NAME} &Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return *(${NAME} *)instance; } };")
    string(APPEND SQUIRREL_EXPORT "\n	template <> struct Param<const ${NAME} *> { static inline const ${NAME} *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return  (${NAME} *)instance; } };")
    string(APPEND SQUIRREL_EXPORT "\n	template <> struct Param<const ${NAME} &> { static inline const ${NAME} &Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, nullptr); return *(${NAME} *)instance; } };")
    if("${NAME}" STREQUAL "ScriptEvent")
        string(APPEND SQUIRREL_EXPORT "\n	template <> struct Return<${NAME} *> { static inline int Set(HSQUIRRELVM vm, ${NAME} *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } Squirrel::CreateClassInstanceVM(vm, \"${REALNAME}\", res, nullptr, DefSQDestructorCallback<${NAME}>, true); return 1; } };")
    elseif("${NAME}" STREQUAL "ScriptText")
        string(APPEND SQUIRREL_EXPORT "\n")
        string(APPEND SQUIRREL_EXPORT "\n	template <> struct Param<Text *> {")
        string(APPEND SQUIRREL_EXPORT "\n		static inline Text *Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) {")
        string(APPEND SQUIRREL_EXPORT "\n			if (sq_gettype(vm, index) == OT_INSTANCE) {")
        string(APPEND SQUIRREL_EXPORT "\n				return Param<ScriptText *>::Get(vm, index, ptr);")
        string(APPEND SQUIRREL_EXPORT "\n			}")
        string(APPEND SQUIRREL_EXPORT "\n			if (sq_gettype(vm, index) == OT_STRING) {")
        string(APPEND SQUIRREL_EXPORT "\n				return new RawText(Param<const char *>::Get(vm, index, ptr));")
        string(APPEND SQUIRREL_EXPORT "\n			}")
        string(APPEND SQUIRREL_EXPORT "\n			return nullptr;")
        string(APPEND SQUIRREL_EXPORT "\n		}")
        string(APPEND SQUIRREL_EXPORT "\n	};")
    else()
        string(APPEND SQUIRREL_EXPORT "\n	template <> struct Return<${NAME} *> { static inline int Set(HSQUIRRELVM vm, ${NAME} *res) { if (res == nullptr) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, \"${REALNAME}\", res, nullptr, DefSQDestructorCallback<${NAME}>, true); return 1; } };")
    endif()
endmacro()

macro(reset_reader)
    unset(ENUMS)
    unset(ENUM_VALUES)
    unset(CONST_VALUES)
    unset(STRUCTS)
    unset(ENUM_STRING_TO_ERRORS)
    unset(ENUM_ERROR_TO_STRINGS)
    unset(METHODS)
    unset(STATIC_METHODS)
    unset(CLS)
    unset(START_SQUIRREL_DEFINE_ON_NEXT_LINE)
    set(CLS_LEVEL 0)
    unset(CLS_IN_API)
endmacro()

reset_reader()

file(STRINGS "${SCRIPT_API_FILE}" SOURCE_LINES)

foreach(LINE IN LISTS SOURCE_LINES)
    # Ignore special doxygen blocks
    if("${LINE}" MATCHES "^#ifndef DOXYGEN_API")
        set(DOXYGEN_SKIP "next")
        continue()
    endif()
    if("${LINE}" MATCHES "^#ifdef DOXYGEN_API")
        set(DOXYGEN_SKIP "true")
        continue()
    endif()
    if("${LINE}" MATCHES "^#endif /\\* DOXYGEN_API \\*/")
        unset(DOXYGEN_SKIP)
        continue()
    endif()
    if("${LINE}" MATCHES "^#else")
        if("${DOXYGEN_SKIP}" STREQUAL "next")
            set(DOXYGEN_SKIP "true")
        else()
            unset(DOXYGEN_SKIP)
        endif()
        continue()
    endif()
    if("${DOXYGEN_SKIP}" STREQUAL "true")
        continue()
    endif()

    if("${LINE}" MATCHES "^([	 ]*)\\* @api (.*)$")
        set(LINE ${CMAKE_MATCH_2})
        # By default, classes are not selected
        if(NOT CLS_LEVEL)
            set(API_SELECTED FALSE)
        endif()

        if("${APIUC}" STREQUAL "Template")
            set(API_SELECTED TRUE)
            if("${LINE}" STREQUAL "none" OR "${LINE}" STREQUAL "-all")
                set(API_SELECTED FALSE)
            endif()
            continue()
        endif()

        if("${LINE}" STREQUAL "none" OR "${LINE}" STREQUAL "-all")
            set(API_SELECTED FALSE)
        elseif("${LINE}" MATCHES "-${APILC}")
            set(API_SELECTED FALSE)
        elseif("${LINE}" MATCHES "${APILC}")
            set(API_SELECTED TRUE)
        endif()
        continue()
    endif()

    # Remove the old squirrel stuff
    if("${LINE}" MATCHES "#ifdef DEFINE_SQUIRREL_CLASS")
        set(SQUIRREL_STUFF TRUE)
        continue()
    endif()
    if("${LINE}" MATCHES "^#endif /\\* DEFINE_SQUIRREL_CLASS \\*/")
        unset(SQUIRREL_STUFF)
        continue()
    endif()
    if(SQUIRREL_STUFF)
        continue()
    endif()

    # Ignore forward declarations of classes
    if("${LINE}" MATCHES "^(	*)class(.*);")
        continue()
    endif()

    # We only want to have public functions exported for now
    if("${LINE}" MATCHES "^(	*)class (.*) (: public|: protected|: private|:) ([^ ]*)")
        if(NOT CLS_LEVEL)
            if(NOT DEFINED API_SELECTED)
                message(WARNING "Class '${CMAKE_MATCH_2}' has no @api. It won't be published to any API.")
                set(API_SELECTED FALSE)
            endif()
            unset(IS_PUBLIC)
            unset(CLS_PARAM_0)
            set(CLS_PARAM_1 1)
            set(CLS_PARAM_2 "x")
            set(CLS_IN_API ${API_SELECTED})
            unset(API_SELECTED)
            set(CLS "${CMAKE_MATCH_2}")
            set(SUPER_CLS "${CMAKE_MATCH_4}")
        elseif(CLS_LEVEL EQUAL 1)
            if(NOT DEFINED API_SELECTED)
                set(API_SELECTED ${CLS_IN_API})
            endif()

            if(API_SELECTED)
                list(APPEND STRUCTS "${CLS}::${CMAKE_MATCH_2}")
            endif()
            unset(API_SELECTED)
        endif()
        math(EXPR CLS_LEVEL "${CLS_LEVEL} + 1")
        continue()
    endif()
    if("${LINE}" MATCHES "^(	*)public")
        if(CLS_LEVEL EQUAL 1)
            set(IS_PUBLIC TRUE)
        endif()
        continue()
    endif()
    if("${LINE}" MATCHES "^(	*)protected")
        if(CLS_LEVEL EQUAL 1)
            unset(IS_PUBLIC)
        endif()
        continue()
    endif()
    if("${LINE}" MATCHES "^(	*)private")
        if(CLS_LEVEL EQUAL 1)
            unset(IS_PUBLIC)
        endif()
        continue()
    endif()

    # Ignore the comments
    if("${LINE}" MATCHES "^#")
        continue()
    endif()
    if("${LINE}" MATCHES "/\\*.*\\*/")
        unset(COMMENT)
        continue()
    endif()
    if("${LINE}" MATCHES "/\\*")
        set(COMMENT TRUE)
        continue()
    endif()
    if("${LINE}" MATCHES "\\*/")
        unset(COMMENT)
        continue()
    endif()
    if(COMMENT)
        continue()
    endif()

    # We need to make specialized conversions for structs
    if("${LINE}" MATCHES "^(	*)struct ([^ ]*)")
        math(EXPR CLS_LEVEL "${CLS_LEVEL} + 1")

        # Check if we want to publish this struct
        if(NOT DEFINED API_SELECTED)
            set(API_SELECTED ${CLS_IN_API})
        endif()
        if(NOT API_SELECTED)
            unset(API_SELECTED)
            continue()
        endif()
        unset(API_SELECTED)

        if(NOT IS_PUBLIC OR NOT CLS_LEVEL EQUAL 1)
            continue()
        endif()

        list(APPEND STRUCTS "${CLS}::${CMAKE_MATCH_2}")
        continue()
    endif()

    # We need to make specialized conversions for enums
    if("${LINE}" MATCHES "^(	*)enum ([^ ]*)")
        math(EXPR CLS_LEVEL "${CLS_LEVEL} + 1")

        # Check if we want to publish this enum
        if(NOT DEFINED API_SELECTED)
            set(API_SELECTED ${CLS_IN_API})
        endif()
        if(NOT API_SELECTED)
            unset(API_SELECTED)
            continue()
        endif()
        unset(API_SELECTED)

        if(NOT IS_PUBLIC)
            continue()
        endif()

        set(IN_ENUM TRUE)
        list(APPEND ENUMS "${CLS}::${CMAKE_MATCH_2}")
        continue()
    endif()

    # Maybe the end of the class, if so we can start with the Squirrel export pretty soon
    if("${LINE}" MATCHES "};")
        math(EXPR CLS_LEVEL "${CLS_LEVEL} - 1")
        if(CLS_LEVEL)
            unset(IN_ENUM)
            continue()
        endif()

        if(CLS)
            set(START_SQUIRREL_DEFINE_ON_NEXT_LINE TRUE)
        endif()
        continue()
    endif()

    # Empty/white lines. When we may do the Squirrel export, do that export.
    if("${LINE}" MATCHES "^([ 	]*)$")
        if(NOT START_SQUIRREL_DEFINE_ON_NEXT_LINE)
            continue()
        endif()

        if(NOT CLS_IN_API)
            reset_reader()
            continue()
        endif()

        if(NOT HAS_FILEHEADER)
            dump_fileheader()
            set(HAS_FILEHEADER TRUE)
        endif()

        unset(IS_PUBLIC)
        unset(NAMESPACE_OPENED)

        string(REGEX REPLACE "^Script" "${APIUC}" API_CLS "${CLS}")
        string(REGEX REPLACE "^Script" "${APIUC}" API_SUPER_CLS "${SUPER_CLS}")

        string(APPEND SQUIRREL_EXPORT "\n")

        if("${APIUC}" STREQUAL "Template")
            # First check whether we have enums to print
            if(DEFINED ENUMS)
                if(NOT NAMESPACE_OPENED)
                    string(APPEND SQUIRREL_EXPORT "\nnamespace SQConvert {")
                    set(NAMESPACE_OPENED TRUE)
                endif()
                string(APPEND SQUIRREL_EXPORT "\n	/* Allow enums to be used as Squirrel parameters */")
                foreach(ENUM IN LISTS ENUMS)
                    string(APPEND SQUIRREL_EXPORT "\n	template <> struct Param<${ENUM}> { static inline ${ENUM} Get(HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (${ENUM})tmp; } };")
                    string(APPEND SQUIRREL_EXPORT "\n	template <> struct Return<${ENUM}> { static inline int Set(HSQUIRRELVM vm, ${ENUM} res) { sq_pushinteger(vm, res); return 1; } };")
                endforeach()
            endif()

            # Then check whether we have structs/classes to print
            if(DEFINED STRUCTS)
                if(NOT NAMESPACE_OPENED)
                    string(APPEND SQUIRREL_EXPORT "\nnamespace SQConvert {")
                    set(NAMESPACE_OPENED TRUE)
                endif()
                string(APPEND SQUIRREL_EXPORT "\n	/* Allow inner classes/structs to be used as Squirrel parameters */")
                foreach(STRUCT IN LISTS STRUCTS)
                    dump_class_templates(${STRUCT})
                endforeach()
            endif()

            if(NOT NAMESPACE_OPENED)
                string(APPEND SQUIRREL_EXPORT "\nnamespace SQConvert {")
                set(NAMESPACE_OPENED TRUE)
            else()
                string(APPEND SQUIRREL_EXPORT "\n")
            endif()
            string(APPEND SQUIRREL_EXPORT "\n	/* Allow ${CLS} to be used as Squirrel parameter */")
            dump_class_templates(${CLS})

            string(APPEND SQUIRREL_EXPORT "\n} // namespace SQConvert")

            reset_reader()
            continue()
        endif()

        string(APPEND SQUIRREL_EXPORT "\n")
        string(APPEND SQUIRREL_EXPORT "\ntemplate <> const char *GetClassName<${CLS}, ScriptType::${APIUC}>() { return \"${API_CLS}\"; }")
        string(APPEND SQUIRREL_EXPORT "\n")

        # Then do the registration functions of the class.
        string(APPEND SQUIRREL_EXPORT "\nvoid SQ${API_CLS}_Register(Squirrel *engine)")
        string(APPEND SQUIRREL_EXPORT "\n{")
        string(APPEND SQUIRREL_EXPORT "\n	DefSQClass<${CLS}, ScriptType::${APIUC}> SQ${API_CLS}(\"${API_CLS}\");")
        if("${SUPER_CLS}" STREQUAL "Text" OR "${SUPER_CLS}" STREQUAL "ScriptObject" OR "${SUPER_CLS}" STREQUAL "AIAbstractiveList::Valuator")
            string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.PreRegister(engine);")
        else()
            string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.PreRegister(engine, \"${API_SUPER_CLS}\");")
        endif()
        if(NOT "${SUPER_CLS}" MATCHES "^ScriptEvent")
            if("${CLS_PARAM_2}" STREQUAL "v")
                string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.AddSQAdvancedConstructor(engine);")
            else()
                string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.AddConstructor<void (${CLS}::*)(${CLS_PARAM_0}), ${CLS_PARAM_1}>(engine, \"${CLS_PARAM_2}\");")
            endif()
        endif()
        string(APPEND SQUIRREL_EXPORT "\n")

        # Enum values
        set(MLEN 0)
        foreach(ENUM_VALUE IN LISTS ENUM_VALUES)
            string(LENGTH "${ENUM_VALUE}" LEN)
            if(MLEN LESS LEN)
                set(MLEN ${LEN})
            endif()
        endforeach()
        foreach(ENUM_VALUE IN LISTS ENUM_VALUES)
            string(LENGTH "${ENUM_VALUE}" LEN)
            math(EXPR LEN "${MLEN} - ${LEN}")
            unset(SPACES)
            foreach(i RANGE ${LEN})
                string(APPEND SPACES " ")
            endforeach()
            string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.DefSQConst(engine, ${CLS}::${ENUM_VALUE},${SPACES}\"${ENUM_VALUE}\");")
        endforeach()
        if(MLEN)
            string(APPEND SQUIRREL_EXPORT "\n")
        endif()

        # Const values
        set(MLEN 0)
        foreach(CONST_VALUE IN LISTS CONST_VALUES)
            string(LENGTH "${CONST_VALUE}" LEN)
            if(MLEN LESS LEN)
                set(MLEN ${LEN})
            endif()
        endforeach()
        foreach(CONST_VALUE IN LISTS CONST_VALUES)
            string(LENGTH "${CONST_VALUE}" LEN)
            math(EXPR LEN "${MLEN} - ${LEN}")
            unset(SPACES)
            foreach(i RANGE ${LEN})
                string(APPEND SPACES " ")
            endforeach()
            string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.DefSQConst(engine, ${CLS}::${CONST_VALUE},${SPACES}\"${CONST_VALUE}\");")
        endforeach()
        if(MLEN)
            string(APPEND SQUIRREL_EXPORT "\n")
        endif()

        # Mapping of OTTD strings to errors
        set(MLEN 0)
        foreach(ENUM_STRING_TO_ERROR IN LISTS ENUM_STRING_TO_ERRORS)
            string(REPLACE ":" ";" ENUM_STRING_TO_ERROR "${ENUM_STRING_TO_ERROR}")
            list(GET ENUM_STRING_TO_ERROR 0 ENUM_STRING)
            string(LENGTH "${ENUM_STRING}" LEN)
            if(MLEN LESS LEN)
                set(MLEN ${LEN})
            endif()
        endforeach()
        foreach(ENUM_STRING_TO_ERROR IN LISTS ENUM_STRING_TO_ERRORS)
            string(REPLACE ":" ";" ENUM_STRING_TO_ERROR "${ENUM_STRING_TO_ERROR}")
            list(GET ENUM_STRING_TO_ERROR 0 ENUM_STRING)
            list(GET ENUM_STRING_TO_ERROR 1 ENUM_ERROR)
            string(LENGTH "${ENUM_STRING}" LEN)
            math(EXPR LEN "${MLEN} - ${LEN}")
            unset(SPACES)
            foreach(i RANGE ${LEN})
                string(APPEND SPACES " ")
            endforeach()
            string(APPEND SQUIRREL_EXPORT "\n	ScriptError::RegisterErrorMap(${ENUM_STRING},${SPACES}${CLS}::${ENUM_ERROR});")
        endforeach()
        if(MLEN)
            string(APPEND SQUIRREL_EXPORT "\n")
        endif()

        # Mapping of errors to human 'readable' strings.
        set(MLEN 0)
        foreach(ENUM_ERROR_TO_STRING IN LISTS ENUM_ERROR_TO_STRINGS)
            string(LENGTH "${ENUM_ERROR_TO_STRING}" LEN)
            if(MLEN LESS LEN)
                set(MLEN ${LEN})
            endif()
        endforeach()
        foreach(ENUM_ERROR_TO_STRING IN LISTS ENUM_ERROR_TO_STRINGS)
            string(LENGTH "${ENUM_ERROR_TO_STRING}" LEN)
            math(EXPR LEN "${MLEN} - ${LEN}")
            unset(SPACES)
            foreach(i RANGE ${LEN})
                string(APPEND SPACES " ")
            endforeach()
            string(APPEND SQUIRREL_EXPORT "\n	ScriptError::RegisterErrorMapString(${CLS}::${ENUM_ERROR_TO_STRING},${SPACES}\"${ENUM_ERROR_TO_STRING}\");")
        endforeach()
        if(MLEN)
            string(APPEND SQUIRREL_EXPORT "\n")
        endif()

        # Static methods
        set(MLEN 0)
        foreach(STATIC_METHOD IN LISTS STATIC_METHODS)
            string(REPLACE ":" ";" STATIC_METHOD "${STATIC_METHOD}")
            list(GET STATIC_METHOD 0 FUNCNAME)
            string(LENGTH "${FUNCNAME}" LEN)
            if(MLEN LESS LEN)
                set(MLEN ${LEN})
            endif()
        endforeach()
        foreach(STATIC_METHOD IN LISTS STATIC_METHODS)
            string(REPLACE ":" ";" STATIC_METHOD "${STATIC_METHOD}")
            list(GET STATIC_METHOD 0 FUNCNAME)
            list(GET STATIC_METHOD 1 ARGC)
            list(GET STATIC_METHOD 2 TYPES)
            string(LENGTH "${FUNCNAME}" LEN)
            math(EXPR LEN "${MLEN} - ${LEN}")
            if("${TYPES}" STREQUAL "v")
                if(LEN GREATER 8)
                    math(EXPR LEN "${LEN} - 8")
                else()
                    set(LEN 0)
                endif()
            endif()
            unset(SPACES)
            foreach(i RANGE ${LEN})
                string(APPEND SPACES " ")
            endforeach()
            if("${TYPES}" STREQUAL "v")
                string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.DefSQAdvancedStaticMethod(engine, &${CLS}::${FUNCNAME},${SPACES}\"${FUNCNAME}\");")
            else()
                string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.DefSQStaticMethod(engine, &${CLS}::${FUNCNAME},${SPACES}\"${FUNCNAME}\",${SPACES}${ARGC}, \"${TYPES}\");")
            endif()
        endforeach()
        if(MLEN)
            string(APPEND SQUIRREL_EXPORT "\n")
        endif()

        # Non-static methods
        set(MLEN 0)
        foreach(METHOD IN LISTS METHODS)
            string(REPLACE ":" ";" METHOD "${METHOD}")
            list(GET METHOD 0 FUNCNAME)
            string(LENGTH "${FUNCNAME}" LEN)
            if(MLEN LESS LEN)
                set(MLEN ${LEN})
            endif()
        endforeach()
        foreach(METHOD IN LISTS METHODS)
            string(REPLACE ":" ";" METHOD "${METHOD}")
            list(GET METHOD 0 FUNCNAME)
            list(GET METHOD 1 ARGC)
            list(GET METHOD 2 TYPES)
            string(LENGTH "${FUNCNAME}" LEN)
            math(EXPR LEN "${MLEN} - ${LEN}")
            if("${TYPES}" STREQUAL "v")
                if(LEN GREATER 8)
                    math(EXPR LEN "${LEN} - 8")
                else()
                    set(LEN 0)
                endif()
            endif()
            unset(SPACES)
            foreach(i RANGE ${LEN})
                string(APPEND SPACES " ")
            endforeach()
            if("${TYPES}" STREQUAL "v")
                string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.DefSQAdvancedMethod(engine, &${CLS}::${FUNCNAME},${SPACES}\"${FUNCNAME}\");")
            else()
                string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.DefSQMethod(engine, &${CLS}::${FUNCNAME},${SPACES}\"${FUNCNAME}\",${SPACES}${ARGC}, \"${TYPES}\");")
            endif()
        endforeach()
        if(MLEN)
            string(APPEND SQUIRREL_EXPORT "\n")
        endif()

        string(APPEND SQUIRREL_EXPORT "\n	SQ${API_CLS}.PostRegister(engine);")
        string(APPEND SQUIRREL_EXPORT "\n}")

        reset_reader()

        continue()
    endif()

    # Skip non-public functions
    if(NOT IS_PUBLIC)
        continue()
    endif()

    # Add enums
    if(IN_ENUM)
        string(REGEX MATCH "([^,	 ]+)" ENUM_VALUE "${LINE}")
        list(APPEND ENUM_VALUES "${ENUM_VALUE}")

        # Check if this a special error enum
        list(GET ENUMS -1 ENUM)
        if("${ENUM}" MATCHES ".*::ErrorMessages")
            # syntax:
            # enum ErrorMessages {
            #	ERR_SOME_ERROR,	// [STR_ITEM1, STR_ITEM2, ...]
            # }

            # Set the mappings
            if("${LINE}" MATCHES "\\[(.*)\\]")
                string(REGEX REPLACE "[ 	]" "" MAPPINGS "${CMAKE_MATCH_1}")
                string(REPLACE "," ";" MAPPINGS "${MAPPINGS}")

                foreach(MAPPING IN LISTS MAPPINGS)
                    list(APPEND ENUM_STRING_TO_ERRORS "${MAPPING}:${ENUM_VALUE}")
                endforeach()

                list(APPEND ENUM_ERROR_TO_STRINGS "${ENUM_VALUE}")
            endif()
        endif()
        continue()
    endif()

    # Add a const (non-enum) value
    if("${LINE}" MATCHES "^[ 	]*static const [^ ]+ ([^ ]+) = -?\\(?[^ ]*\\)?[^ ]+;")
        list(APPEND CONST_VALUES "${CMAKE_MATCH_1}")
        continue()
    endif()

    # Add a method to the list
    if("${LINE}" MATCHES "^.*\\(.*\\).*$")
        if(NOT CLS_LEVEL EQUAL 1)
            continue()
        endif()
        if("${LINE}" MATCHES "~")
            if(DEFINED API_SELECTED)
                message(WARNING "Destructor for '${CLS}' has @api. Tag ignored.")
                unset(API_SELECTED)
            endif()
            continue()
        endif()

        unset(IS_STATIC)
        if("${LINE}" MATCHES "static")
            set(IS_STATIC TRUE)
        endif()

        string(REGEX REPLACE "(virtual|static|const)[ 	]+" "" LINE "${LINE}")
        string(REGEX REPLACE "{.*" "" LINE "${LINE}")
        set(PARAM_S "${LINE}")
        string(REGEX REPLACE "\\*" "" LINE "${LINE}")
        string(REGEX REPLACE "\\(.*" "" LINE "${LINE}")

        string(REGEX REPLACE ".*\\(" "" PARAM_S "${PARAM_S}")
        string(REGEX REPLACE "\\).*" "" PARAM_S "${PARAM_S}")

        string(REGEX MATCH "([^ 	]+)( ([^ ]+))?" RESULT "${LINE}")
        set(FUNCTYPE "${CMAKE_MATCH_1}")
        set(FUNCNAME "${CMAKE_MATCH_3}")
        if("${FUNCTYPE}" STREQUAL "${CLS}" AND NOT FUNCNAME)
            if(DEFINED API_SELECTED)
                message(WARNING "Constructor for '${CLS}' has @api. Tag ignored.")
                unset(API_SELECTED)
            endif()
            set(CLS_PARAM_0 "${PARAM_S}")
            if(NOT PARAM_S)
                continue()
            endif()
        elseif(NOT FUNCNAME)
            continue()
		endif()

        string(REPLACE "," ";" PARAMS "${PARAM_S}")
        if(IS_STATIC)
            set(TYPES ".")
        else()
            set(TYPES "x")
        endif()

        set(LEN 1)
        foreach(PARAM IN LISTS PARAMS)
            math(EXPR LEN "${LEN} + 1")
            string(STRIP "${PARAM}" PARAM)
            if("${PARAM}" MATCHES "\\*|&")
                if("${PARAM}" MATCHES "^char")
                    # Many types can be converted to string, so use '.', not 's'. (handled by our glue code)
                    string(APPEND TYPES ".")
                elseif("${PARAM}" MATCHES "^void")
                    string(APPEND TYPES "p")
                elseif("${PARAM}" MATCHES "^Array")
                    string(APPEND TYPES "a")
                elseif("${PARAM}" MATCHES "^const Array")
                    string(APPEND TYPES "a")
                elseif("${PARAM}" MATCHES "^Text")
                    string(APPEND TYPES ".")
                else()
                    string(APPEND TYPES "x")
                endif()
            elseif("${PARAM}" MATCHES "^bool")
                string(APPEND TYPES "b")
            elseif("${PARAM}" MATCHES "^HSQUIRRELVM")
                set(TYPES "v")
            else()
                string(APPEND TYPES "i")
            endif()
        endforeach()

        # Check if we want to publish this function
        if(NOT DEFINED API_SELECTED)
            set(API_SELECTED ${CLS_IN_API})
        endif()
        if(NOT API_SELECTED)
            unset(API_SELECTED)
            continue()
        endif()
        unset(API_SELECTED)

        if("${FUNCTYPE}" STREQUAL "${CLS}" AND NOT FUNCNAME)
            set(CLS_PARAM_1 ${LEN})
            set(CLS_PARAM_2 "${TYPES}")
        elseif("${FUNCNAME}" MATCHES "^_" AND NOT "${TYPES}" STREQUAL "v")
        elseif(IS_STATIC)
            list(APPEND STATIC_METHODS "${FUNCNAME}:${LEN}:${TYPES}")
        else()
            list(APPEND METHODS "${FUNCNAME}:${LEN}:${TYPES}")
        endif()
        continue()
    endif()
endforeach()

configure_file(${SCRIPT_API_SOURCE_FILE} ${SCRIPT_API_BINARY_FILE})