Skip to content

Commit 62b21ed

Browse files
author
Vano
committed
code improvements
1 parent c6639af commit 62b21ed

File tree

2 files changed

+76
-46
lines changed

2 files changed

+76
-46
lines changed

cppscript.py

Lines changed: 76 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def __call__(self, scons_env, source, call_args, cwd = os.getcwd(), *args, **kwa
2020
cppscript_src = os.path.join(os.path.dirname(__file__), 'src')
2121
# Convert scons variables to cppscript's env
2222
scons_env['cppscript_env'] = {
23-
'header_name' : env['header_name'],
24-
'header_dir' : resolve_path(str(env['header_dir']), cwd),
25-
'gen_dir' : resolve_path(str(env['gen_dir']), cwd),
26-
'compile_defs' : {f'{i[0]}={i[1]}' if type(i) is tuple else str(i) for i in env.get('compile_defs', [])},
27-
'include_paths' : {resolve_path(str(i), cwd) for i in [cppscript_src, env['header_dir']] + env.get('include_paths', [])},
28-
'auto_methods' : env['auto_methods']
23+
'header_name' : env['header_name'],
24+
'header_dir' : resolve_path(str(env['header_dir']), cwd),
25+
'gen_dir' : resolve_path(str(env['gen_dir']), cwd),
26+
'compile_defs' : {f'{i[0]}={i[1]}' if type(i) is tuple else str(i) for i in env.get('compile_defs', [])},
27+
'include_paths' : {resolve_path(str(i), cwd) for i in [cppscript_src, env['header_dir']] + env.get('include_paths', [])},
28+
'auto_methods' : env['auto_methods']
2929
}
3030

3131
# Append needed directories
@@ -43,13 +43,41 @@ def GlobRecursive(path, pattern, **kwargs):
4343

4444
return found
4545

46-
47-
KEYWORDS = ['GPROPERTY', 'GMETHOD', 'GGROUP', 'GSUBGROUP', 'GBITFIELD', 'GSIGNAL', 'GRPC', 'GVARARG', 'GIGNORE', 'GBIND_METHODS_APPEND', 'GBIND_METHODS_PREPEND', 'GRESOURCE_LOADER', 'GRESOURCE_SAVER']
48-
INIT_LEVELS = ['GINIT_LEVEL_CORE', 'GINIT_LEVEL_SERVERS', 'GINIT_LEVEL_SCENE', 'GINIT_LEVEL_EDITOR']
46+
CLASS_KEYWORDS = [
47+
'GCLASS',
48+
'GVIRTUAL_CLASS',
49+
'GABSTRACT_CLASS',
50+
'GINTERNAL_CLASS'
51+
]
52+
INIT_LEVELS = [
53+
'GINIT_LEVEL_CORE',
54+
'GINIT_LEVEL_SERVERS',
55+
'GINIT_LEVEL_SCENE',
56+
'GINIT_LEVEL_EDITOR'
57+
]
58+
KEYWORDS = [
59+
'GPROPERTY',
60+
'GMETHOD',
61+
'GBITFIELD',
62+
'GRPC',
63+
'GVARARG',
64+
'GIGNORE'
65+
]
66+
TARGETLESS_KEYWORDS = [
67+
'GGROUP',
68+
'GSUBGROUP',
69+
'GSIGNAL',
70+
'GBIND_METHODS_APPEND',
71+
'GBIND_METHODS_PREPEND',
72+
'GRESOURCE_LOADER',
73+
'GRESOURCE_SAVER'
74+
] + INIT_LEVELS
75+
76+
ALL_KEYWORDS = KEYWORDS + TARGETLESS_KEYWORDS
4977

5078
DONOTEDIT_MSG = "/*-- GENERATED FILE - DO NOT EDIT --*/\n\n"
5179

52-
CPPSCRIPT_BODY= DONOTEDIT_MSG + """#ifndef {0}
80+
CPPSCRIPT_BODY = DONOTEDIT_MSG + """#ifndef {0}
5381
#define {0}
5482
#include <cppscript_defs.h>
5583
#include "properties.gen.h"
@@ -309,13 +337,10 @@ def parse_cursor(parent):
309337
classes_and_Gmacros.append(cursor)
310338

311339
case CursorKind.MACRO_INSTANTIATION:
312-
if cursor.spelling in KEYWORDS:
313-
keyword_macros.append(cursor)
314-
315-
elif cursor.spelling in INIT_LEVELS:
340+
if cursor.spelling in ALL_KEYWORDS:
316341
keyword_macros.append(cursor)
317342

318-
elif cursor.spelling in ['GCLASS', 'GVIRTUAL_CLASS', 'GABSTRACT_CLASS', 'GINTERNAL_CLASS']:
343+
elif cursor.spelling in CLASS_KEYWORDS:
319344
classes_and_Gmacros.append(cursor)
320345

321346
case _:
@@ -388,11 +413,11 @@ def process_macros(item, macros, properties, is_ignored=False):
388413
.format(filename, macro.location.line, macro.location.column, macro.spelling))
389414

390415
properties |= {
391-
'setter' : args[0],
392-
'getter' : args[1],
393-
'hint' : 'PROPERTY_HINT_' + args[2].upper() if len(args) > 2 else None,
394-
'hint_string' : args[3] if len(args) > 3 else '""'
395-
}
416+
'setter' : args[0],
417+
'getter' : args[1],
418+
'hint' : 'PROPERTY_HINT_' + args[2].upper() if len(args) > 2 else None,
419+
'hint_string' : args[3] if len(args) > 3 else '""'
420+
}
396421
is_ignored = False
397422

398423
case 'GGROUP':
@@ -469,11 +494,12 @@ def process_macros(item, macros, properties, is_ignored=False):
469494

470495
channel = arg
471496

472-
473-
rpc_config = { 'rpc_mode' : 'RPC_MODE_' + rpc_mode if rpc_mode != None else 'RPC_MODE_AUTHORITY',
474-
'transfer_mode' : 'TRANSFER_MODE_' + transfer_mode if transfer_mode != None else 'TRANSFER_MODE_UNRELIABLE',
475-
'call_local' : call_local if call_local != None else 'false',
476-
'channel' : channel if channel != None else '0'}
497+
rpc_config = {
498+
'rpc_mode' : 'RPC_MODE_' + rpc_mode if rpc_mode != None else 'RPC_MODE_AUTHORITY',
499+
'transfer_mode' : 'TRANSFER_MODE_' + transfer_mode if transfer_mode != None else 'TRANSFER_MODE_UNRELIABLE',
500+
'call_local' : call_local if call_local != None else 'false',
501+
'channel' : channel if channel != None else '0'
502+
}
477503

478504
properties['rpc_config'] = rpc_config
479505

@@ -512,13 +538,14 @@ def apply_macros(item, macros):
512538
is_virtual = is_virtual_method(item)
513539
properties = {}
514540
if process_macros(item, macros, properties, (is_virtual and item.spelling.startswith('_')) or not env['auto_methods'] or item.access_specifier != AccessSpecifier.PUBLIC):
515-
properties |= { 'name' : item.spelling,
516-
'bind_name' : item.spelling,
517-
'return' : item.result_type.spelling,
518-
'args' : [(arg.type.spelling, arg.spelling, find_default_arg(filecontent, arg)) for arg in item.get_arguments()],
519-
'is_static' : item.is_static_method(),
520-
'is_virtual' : is_virtual
521-
}
541+
properties |= {
542+
'name' : item.spelling,
543+
'bind_name' : item.spelling,
544+
'return' : item.result_type.spelling,
545+
'args' : [(arg.type.spelling, arg.spelling, find_default_arg(filecontent, arg)) for arg in item.get_arguments()],
546+
'is_static' : item.is_static_method(),
547+
'is_virtual' : is_virtual
548+
}
522549
class_defs['methods'].append(properties)
523550

524551
case CursorKind.ENUM_DECL:
@@ -535,21 +562,22 @@ def apply_macros(item, macros):
535562
case CursorKind.FIELD_DECL:
536563
properties = {}
537564
if process_macros(item, macros, properties, True):
538-
properties |= { 'name': item.spelling,
539-
'group' : group,
540-
'subgroup' : subgroup,
541-
'is_static' : item.is_static_method()
542-
}
565+
properties |= {
566+
'name': item.spelling,
567+
'group' : group,
568+
'subgroup' : subgroup,
569+
'is_static' : item.is_static_method()
570+
}
543571

544572
class_defs['properties'].append(properties)
545573

546574

547575

548576
leftover = collapse_list(class_macros, lambda x: x.kind != CursorKind.MACRO_INSTANTIATION, apply_macros)
549577
for macro in leftover:
550-
if macro.spelling not in ['GSIGNAL', 'GGROUP', 'GSUBGROUP', 'GBIND_METHODS_APPEND', 'GBIND_METHODS_PREPEND', 'GRESOURCE_LOADER', 'GRESOURCE_SAVER'] + INIT_LEVELS:
551-
raise CppScriptException('{}:{}:{}: error: macro without target member'
552-
.format(filename, macro.location.line, macro.location.column))
578+
if macro.spelling not in TARGETLESS_KEYWORDS:
579+
raise CppScriptException('{}:{}:{}: error: macro "{}" without target member'
580+
.format(filename, macro.location.line, macro.location.column, macro.spelling))
553581
process_macros(None, leftover, None)
554582

555583

@@ -571,11 +599,10 @@ def write_header(file, defs, env):
571599
for class_name_full, content in defs.items():
572600
class_name = content['class_name']
573601
Hmethod, Hstatic_method, Hvirtual_method, Hvaragr_method, Hprop, Hsignal, Henum, Hbitfield, Hconst = '', '', '', '', '', '', '', '', ''
574-
outside_bind = ''
575-
header_rpc_config = ''
602+
outside_bind, header_rpc_config, property_set_get_defs = '', '', ''
576603
gen_setters, gen_getters = [], []
577-
property_set_get_defs = ''
578604
methods_list = [method['bind_name'] for method in content['methods']]
605+
has_rpc_config = False
579606

580607
for method in content['methods']:
581608
if 'varargs' not in method.keys():
@@ -593,6 +620,7 @@ def write_header(file, defs, env):
593620
Hmethod += f'\tMethod<&{class_name}::{method["name"]}>::bind(D_METHOD("{method["bind_name"]}"{args}){defvals});\n'
594621

595622
if 'rpc_config' in method.keys():
623+
has_rpc_config = True
596624
header_rpc_config += RPC_CONFIG_BODY.format(
597625
method['rpc_config']['rpc_mode'],
598626
method['rpc_config']['transfer_mode'],
@@ -678,6 +706,11 @@ def write_header(file, defs, env):
678706
.format(
679707
os.path.relpath(file, os.path.dirname(gen_filename)).replace('\\', '/'),
680708
('\n'.join(global_variables) + '\n\n' if global_variables != [] else ''))
709+
710+
if has_rpc_config != '':
711+
header_include = '#include <godot_cpp/classes/multiplayer_api.hpp>\n' + header_include
712+
header_include = '#include <godot_cpp/classes/multiplayer_peer.hpp>\n' + header_include
713+
681714
content = DONOTEDIT_MSG + header_include + '\n'.join(header_defs)
682715

683716
os.makedirs(os.path.dirname(gen_filename), exist_ok=True)

src/cppscript_defs.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#ifndef CPPSCRIPT_HEADER
22
#define CPPSCRIPT_HEADER
3-
#include <godot_cpp/classes/multiplayer_api.hpp>
4-
#include <godot_cpp/classes/multiplayer_peer.hpp>
5-
63

74
#define GCLASS(CLASS_NAME, CLASS_NAME_INH) \
85
GDCLASS(CLASS_NAME , CLASS_NAME_INH) \

0 commit comments

Comments
 (0)