Skip to content

Commit

Permalink
PythonScript&PythonScriptLanguage are correctly registered
Browse files Browse the repository at this point in the history
  • Loading branch information
touilleMan committed Dec 25, 2023
1 parent 19df58f commit 56f3ebc
Show file tree
Hide file tree
Showing 7 changed files with 426 additions and 325 deletions.
7 changes: 6 additions & 1 deletion scripts/extension_api_parser/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ class NativeStructureSpec(TypeSpec):
# Format is basically a dump of the C struct content, so don't try to be clever by parsing it
fields: Dict[str, TypeInUse]

@property
def is_native_structure(self) -> bool:
return True

def __getattribute__(self, name: str):
if name in ("variant_type_name", "py_type", "cy_type"):
raise RuntimeError(
Expand Down Expand Up @@ -156,7 +160,8 @@ def parse(cls, item: dict) -> "NativeStructureSpec":
fields=fields,
py_type="", # Never accessed dummy value
cy_type="", # Never accessed dummy value
c_type=f"gd_{camel_to_snake(name)}",
# c_type=f"gd_{camel_to_snake(name)}_t",
c_type=name,
is_stack_only=True,
variant_type_name="", # Never accessed dummy value
)
Expand Down
4 changes: 4 additions & 0 deletions scripts/extension_api_parser/type_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def is_transparent_builtin(self) -> bool:
def is_opaque_builtin(self) -> bool:
return False

@property
def is_native_structure(self) -> bool:
return False


class ScalarTypeSpec(TypeSpec):
"""
Expand Down
28 changes: 20 additions & 8 deletions scripts/gdextension_cython_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
PRAGMA_RE = re.compile(r"^(?P<indentation>\s*)# godot_extension:\s+(?P<pragma>.*)$")
CLASS_RE = re.compile(r"^cdef class (?P<class_name>\w+)")
METHOD_RE = re.compile(
r"^cdef\s+(inline\s+)?(?P<return_type>\w+)\s+(?P<method_name>\w+)\((?P<param>.*)\):"
r"^cdef\s+(inline\s+)?(?P<return_type>\w+\s*\*?)\s+(?P<method_name>\w+)\((?P<param>.*)\):"
)

INJECT_CODE_PRAGMA = "generate_code()"
Expand Down Expand Up @@ -55,8 +55,8 @@ def generate_injected_code_method(spec: MethodDef, class_spec: ClassDef) -> str:
else:
code += f"(<{class_spec.class_name}>p_instance).{spec.method_name}(\n"

for _, param_type in spec.parameters.items():
code += f" (<{param_type}>p_args[0])[0],\n"
for i, (_, param_type) in enumerate(spec.parameters.items()):
code += f" (<{param_type}*>p_args[{i}])[0],\n"
code += " )\n"

return code
Expand Down Expand Up @@ -91,7 +91,7 @@ def __godot_extension_register_class():
cooked_params = "["
for param_name, param_type in method.parameters.items():
cooked_params += "("
cooked_params += f'b"{param_type}", b"{param_name}"'
cooked_params += f'b"{param_name}", b"{param_type}"'
cooked_params += "), "
cooked_params += "]"

Expand Down Expand Up @@ -125,6 +125,18 @@ def generate_injected_code(spec: ClassDef) -> str:
return code


def handle_pointer_type(raw_type: str) -> str:
# Type is not a real Godot object, but we still use `gd_object_t`
# to represent the fact it is a pointer
# if raw_type.endswith("*"):
# return "gd_object_t"
try:
pointed_type, _ = raw_type.split()
return f"{pointed_type}*"
except ValueError:
return raw_type.strip()


def extract_classes_from_code(code_lines: List[str]) -> List[ClassDef]:
code_lines = enumerate(code_lines)
classes: List[ClassDef] = []
Expand Down Expand Up @@ -181,7 +193,7 @@ def _method(const: bool = False) -> MethodDef:
meth_signature = line[: line.index("(") + 1].strip()
except ValueError:
raise RuntimeError(
f"expected method signature `cdef [inline] gd_xxx_t foo({'' if is_staticmethod else 'self, '}, gd_yyy_t bar, ...)"
f"expected method signature `cdef [inline] gd_xxx_t foo({'' if is_staticmethod else 'self, '}gd_yyy_t bar, ...)"
)
open_parenthesises = 1
while open_parenthesises > 0:
Expand All @@ -200,7 +212,7 @@ def _method(const: bool = False) -> MethodDef:
match = METHOD_RE.match(meth_signature)
if not match:
raise RuntimeError(
f"expected method signature `cdef [inline] gd_xxx_t foo({'' if is_staticmethod else 'self, '}, gd_yyy_t bar, ...)"
f"expected method signature `cdef [inline] gd_xxx_t foo({'' if is_staticmethod else 'self, '}gd_yyy_t bar, ...)"
)

params = {}
Expand All @@ -216,14 +228,14 @@ def _method(const: bool = False) -> MethodDef:
param_type, param_name = raw_param.split()
except ValueError:
raise RuntimeError(f"bad parameter {raw_param!r}")
params[param_name] = param_type
params[param_name] = handle_pointer_type(param_type)

current_class.methods.append(
MethodDef(
method_name=match.group("method_name"),
is_staticmethod=is_staticmethod,
is_const=is_const,
return_type=match.group("return_type"),
return_type=handle_pointer_type(match.group("return_type")),
parameters=params,
)
)
Expand Down
Loading

0 comments on commit 56f3ebc

Please sign in to comment.