Skip to content

Commit 08f04aa

Browse files
committed
Use pre-generated spec for classes.pyx
1 parent c04ce42 commit 08f04aa

File tree

10 files changed

+420
-411
lines changed

10 files changed

+420
-411
lines changed

scripts/generate_tmpl.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818
"gdtypes.pxd": (False, HAZMAT_DIR),
1919
"gdapi.pxd": (False, HAZMAT_DIR),
2020
"builtins.pyi": (False, GODOT_DIR),
21-
"builtins.pxd": (True, GODOT_DIR),
22-
"builtins.pyx": (True, GODOT_DIR),
21+
"builtins.pxd": (False, GODOT_DIR),
22+
"builtins.pyx": (False, GODOT_DIR),
2323
"classes.pyi": (True, GODOT_DIR),
24-
"classes.pxd": (True, GODOT_DIR),
25-
"classes.pyx": (True, GODOT_DIR),
24+
"_classes_api.py": (True, GODOT_DIR),
2625
"conversion.pyx": (False, GODOT_DIR),
2726
"conversion.pxd": (False, GODOT_DIR),
2827
}
@@ -41,6 +40,7 @@
4140
"Font",
4241
"Image",
4342
"InputEvent",
43+
"JSON",
4444
"MainLoop",
4545
"Node",
4646
"Node2D",
@@ -86,6 +86,7 @@ def make_jinja_env(import_dir: Path) -> Environment:
8686
)
8787
env.filters["merge"] = lambda x, **kwargs: {**x, **kwargs}
8888
env.filters["gd_description_to_py_doc"] = gd_description_to_py_doc
89+
env.filters["repr"] = repr
8990
return env
9091

9192

src/godot/_classes_api.py.j2

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
# Constant definition:
2+
# [ ...
3+
# "_c",
4+
# <constant_name as str>,
5+
# <constant_value as int>,
6+
# ...]
7+
_tag_constant = "_c"
8+
9+
# Enum definition:
10+
# [ ...
11+
# "_e",
12+
# <enum_name as str>,
13+
# <enum_items_count as int>,
14+
# # First item
15+
# <enum_item_name as str>,
16+
# <enum_item_value as int>,
17+
# # Additional items
18+
# ...
19+
# ...]
20+
_tag_enum = "_e"
21+
22+
# Property definition:
23+
# [ ...
24+
# "_p",
25+
# <property_name as str>,
26+
# <property_type as str>,
27+
# <property_getter as str>,
28+
# <property_setter as str or None>,
29+
# <property_index as int or None>,
30+
# ...]
31+
_tag_property = "_p"
32+
33+
# Signal definition:
34+
# [ ...
35+
# "_s",
36+
# <signal_name as str>,
37+
# <signal_arguments_count as int>,
38+
# # First argument
39+
# <argument_name as str>,
40+
# <argument_type as str>,
41+
# <argument_default_value as str or None>,
42+
# # Additional argument
43+
# ...
44+
# ...]
45+
_tag_signal = "_s"
46+
47+
# Method definition:
48+
# [ ...
49+
# "_m",
50+
# <method_name as str>,
51+
# <method_hash as int>,
52+
# <method_flags as int>,
53+
# <method_return_type as str>,
54+
# <method_arguments_count as int>,
55+
# # First argument
56+
# <argument_name as str>,
57+
# <argument_type as str>,
58+
# <argument_default_value as str or None>,
59+
# # Additional argument
60+
# ...
61+
# ...]
62+
_tag_method = "_m"
63+
_tag_method_flag_is_const = 1 << 1
64+
_tag_method_flag_is_vararg = 1 << 2
65+
_tag_method_flag_is_static = 1 << 3
66+
_tag_method_flag_is_virtual = 1 << 4
67+
_tag_method_flag_is_required = 1 << 5
68+
_tag_method_flag_is_property_accessor = 1 << 6
69+
70+
71+
{% for c in api.classes %}
72+
{{ c.original_name }} = [
73+
# Parent
74+
{% if c.inherits is none %}
75+
None,
76+
{% else %}
77+
"{{ c.inherits.type_name }}",
78+
{% endif %}
79+
80+
# constants
81+
{% for constant in c.constants %}
82+
83+
_tag_constant,
84+
"{{ constant.name }}",
85+
{{ constant.value }},
86+
{% endfor %}
87+
88+
# enums
89+
{% for enum in c.enums %}
90+
91+
_tag_enum,
92+
"{{ enum.original_name }}",
93+
{{ enum.items | length }}, # Items count
94+
{% for item in enum.items %}
95+
"{{ item.py_name }}",
96+
{{ item.value }},
97+
{% endfor %}
98+
{% endfor %}
99+
100+
# properties
101+
{% for prop in c.properties %}
102+
103+
_tag_property,
104+
"{{ prop.name }}",
105+
# type
106+
"{{ prop.type.type_name }}",
107+
# getter
108+
"{{ prop.getter }}",
109+
# setter
110+
{% if prop.setter is none %}
111+
None,
112+
{% else %}
113+
"{{ prop.setter }}",
114+
{% endif %}
115+
# index
116+
{% if prop.index is none %}
117+
None,
118+
{% else %}
119+
{{ prop.index }},
120+
{% endif %}
121+
{% endfor %}
122+
123+
# signals
124+
{% for signal in c.signals %}
125+
126+
_tag_signal,
127+
"{{ signal.name }}",
128+
{{ signal.arguments | length }}, # Arguments count
129+
{% for signal_arg in signal.arguments %}
130+
"{{ signal_arg.name }}",
131+
"{{ signal_arg.type.type_name }}",
132+
{% if signal_arg.default_value is none %}
133+
None, # No default value
134+
{% else %}
135+
{{ signal_arg.default_value.py_value | repr }}, # Default value
136+
{% endif %}
137+
{% endfor %}
138+
{% endfor %}
139+
140+
# methods
141+
{% for method in c.methods %}
142+
143+
_tag_method,
144+
"{{ method.name }}",
145+
{{ method.hash }}, # Hash
146+
(
147+
0
148+
{% if method.is_const %}
149+
| _tag_method_flag_is_const
150+
{% endif %}
151+
{% if method.is_vararg %}
152+
| _tag_method_flag_is_vararg
153+
{% endif %}
154+
{% if method.is_static %}
155+
| _tag_method_flag_is_static
156+
{% endif %}
157+
{% if method.is_virtual %}
158+
| _tag_method_flag_is_virtual
159+
{% endif %}
160+
{% if method.is_required %}
161+
| _tag_method_flag_is_required
162+
{% endif %}
163+
{% if method.is_property_accessor %}
164+
| _tag_method_flag_is_property_accessor
165+
{% endif %}
166+
),
167+
"{{ method.return_type.type_name }}", # Return type
168+
{{ method.arguments | length }}, # Arguments count
169+
{% for method_arg in method.arguments %}
170+
"{{ method_arg.name }}",
171+
"{{ method_arg.type.type_name }}",
172+
{% if method_arg.default_value is none %}
173+
None, # No default value
174+
{% else %}
175+
{{ method_arg.default_value.py_value | repr }}, # Default value
176+
{% endif %}
177+
{% endfor %}
178+
{% endfor %}
179+
]
180+
{% endfor %}

src/godot/classes.pxd

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .hazmat cimport gdapi
2+
from .hazmat.gdtypes cimport *
3+
4+
5+
cdef class BaseGDObject:
6+
cdef gd_object_t _gd_ptr
7+
8+
@staticmethod
9+
cdef inline object cast_from_variant(const gd_variant_t *gdvar):
10+
# TODO: cast to remove const due to `GDExtensionTypeFromVariantConstructorFunc`
11+
cdef gd_object_t obj = gdapi.gd_object_from_variant(<gd_variant_t *>gdvar)
12+
if obj == NULL:
13+
return None
14+
cdef object class_name = _object_call(obj, "get_class", [])
15+
# TODO: Kind of wasteful to have to convert Godot string here...
16+
cdef object klass = _load_class(str(class_name))
17+
return klass._from_ptr(<size_t>obj)
18+
19+
@staticmethod
20+
cdef inline object cast_from_object(gd_object_t obj):
21+
if obj == NULL:
22+
return None
23+
cdef object class_name = _object_call(obj, "get_class", [])
24+
# TODO: Kind of wasteful to have to convert Godot string here...
25+
cdef object klass = _load_class(str(class_name))
26+
return klass._from_ptr(<size_t>obj)
27+
28+
29+
cdef object _load_class(str name)
30+
cpdef BaseGDObject _load_singleton(str name)
31+
cdef void _cleanup_loaded_classes_and_singletons()
32+
cdef object _object_call(gd_object_t obj, str meth, list args)

src/godot/classes.pxd.j2

Lines changed: 0 additions & 111 deletions
This file was deleted.

0 commit comments

Comments
 (0)