From 34eca3bf352309be36665f34e3d353ebdb74cf45 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 2 Jun 2023 17:59:23 +0200 Subject: [PATCH] Fix templates. --- generation/builtins_templates/array.tmpl.pxi | 6 +- generation/builtins_templates/basis.tmpl.pxi | 2 +- .../builtins_templates/builtins.tmpl.pxd | 49 ++- .../builtins_templates/builtins.tmpl.pyi | 14 +- .../builtins_templates/projection.tmpl.pxi | 2 +- .../builtins_templates/quaternion.tmpl.pxi | 2 +- generation/builtins_templates/rect2i.tmpl.pxi | 6 +- .../builtins_templates/string_name.tmpl.pxi | 8 +- .../builtins_templates/vector2i.tmpl.pxi | 40 +- .../builtins_templates/vector3i.tmpl.pxi | 40 +- .../builtins_templates/vector4i.tmpl.pxi | 40 +- pythonscript/_pandemonium_script.pxi | 4 +- .../pandemonium/_hazmat/conversion.pxd | 15 +- .../pandemonium/_hazmat/conversion.pyx | 4 +- pythonscript/pythonscript.c | 379 +++++++++--------- 15 files changed, 310 insertions(+), 301 deletions(-) diff --git a/generation/builtins_templates/array.tmpl.pxi b/generation/builtins_templates/array.tmpl.pxi index 931ea0a..4595e16 100644 --- a/generation/builtins_templates/array.tmpl.pxi +++ b/generation/builtins_templates/array.tmpl.pxi @@ -57,7 +57,7 @@ cdef class Array: if not iterable: gdapi10.pandemonium_array_new(&self._gd_data) elif isinstance(iterable, Array): - self._gd_data = gdapi10.pandemonium_array_duplicate(&(iterable)._gd_data, False) + self._gd_data = gdapi10.pandemonium_array_duplicate(&(iterable)._gd_data) # TODO: handle Pool*Array else: gdapi10.pandemonium_array_new(&self._gd_data) @@ -96,7 +96,9 @@ cdef class Array: cdef inline Array operator_getslice(self, pandemonium_int start, pandemonium_int stop, pandemonium_int step): {{ force_mark_rendered("pandemonium_array_slice") }} cdef Array ret = Array.__new__(Array) - ret._gd_data = gdapi10.pandemonium_array_slice(&self._gd_data, start, stop, step, False) + #TODO + #ret._gd_data = gdapi10.pandemonium_array_slice_step(&self._gd_data, start, stop, step, False) + ret._gd_data = gdapi10.pandemonium_array_slice(&self._gd_data, start, stop) return ret # TODO: support slice diff --git a/generation/builtins_templates/basis.tmpl.pxi b/generation/builtins_templates/basis.tmpl.pxi index 5e4b8e3..c7e57d9 100644 --- a/generation/builtins_templates/basis.tmpl.pxi +++ b/generation/builtins_templates/basis.tmpl.pxi @@ -191,7 +191,7 @@ cdef class Basis: return Basis_multiply_scalar(self, val) else: - return Basis_multiply_vector(self, _val) + return Basis_multiply(self, _val) {{ render_method("as_string") | indent }} {{ render_method("inverse") | indent }} diff --git a/generation/builtins_templates/builtins.tmpl.pxd b/generation/builtins_templates/builtins.tmpl.pxd index 99053be..e33bc8d 100644 --- a/generation/builtins_templates/builtins.tmpl.pxd +++ b/generation/builtins_templates/builtins.tmpl.pxd @@ -9,52 +9,57 @@ from pandemonium.pool_arrays cimport ( PoolRealArray, PoolByteArray, PoolVector2Array, + PoolVector2iArray, PoolVector3Array, + PoolVector3iArray, + PoolVector4Array, + PoolVector4iArray, PoolColorArray, PoolStringArray, ) + {% set render_target = "rid" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "vector3" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "vector2" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "vector2i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "vector3i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "vector4" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "vector4i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "aabb" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "basis" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "color" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "gdstring" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "rect2" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "rect2i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "projection" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "transform2d" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "plane" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "quaternion" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "transform" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "node_path" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "string_name" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "dictionary" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} {% set render_target = "array" %} -{% include 'render.tmpl.pyi' with context %} +{% include 'render.tmpl.pxd' with context %} diff --git a/generation/builtins_templates/builtins.tmpl.pyi b/generation/builtins_templates/builtins.tmpl.pyi index 120c135..14ba9ba 100644 --- a/generation/builtins_templates/builtins.tmpl.pyi +++ b/generation/builtins_templates/builtins.tmpl.pyi @@ -10,13 +10,13 @@ from typing import Union {% set render_target = "vector2" %} {% include 'render.tmpl.pyi' with context %} {% set render_target = "vector2i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "vector3i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "vector4" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "vector4i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "aabb" %} {% include 'render.tmpl.pyi' with context %} {% set render_target = "basis" %} @@ -28,9 +28,9 @@ from typing import Union {% set render_target = "rect2" %} {% include 'render.tmpl.pyi' with context %} {% set render_target = "rect2i" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "projection" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "transform2d" %} {% include 'render.tmpl.pyi' with context %} {% set render_target = "plane" %} @@ -42,7 +42,7 @@ from typing import Union {% set render_target = "node_path" %} {% include 'render.tmpl.pyi' with context %} {% set render_target = "string_name" %} -{% include 'render.tmpl.pyx' with context %} +{% include 'render.tmpl.pyi' with context %} {% set render_target = "dictionary" %} {% include 'render.tmpl.pyi' with context %} {% set render_target = "array" %} diff --git a/generation/builtins_templates/projection.tmpl.pxi b/generation/builtins_templates/projection.tmpl.pxi index 60d0333..d5d1eb8 100644 --- a/generation/builtins_templates/projection.tmpl.pxi +++ b/generation/builtins_templates/projection.tmpl.pxi @@ -89,7 +89,7 @@ cdef class Projection: &(x_axis)._gd_data, &(y_axis)._gd_data, &(z_axis)._gd_data, - &(origin)._gd_data, + &(w_axis)._gd_data, ) def __repr__(Projection self): diff --git a/generation/builtins_templates/quaternion.tmpl.pxi b/generation/builtins_templates/quaternion.tmpl.pxi index ab0b0e3..a06165f 100644 --- a/generation/builtins_templates/quaternion.tmpl.pxi +++ b/generation/builtins_templates/quaternion.tmpl.pxi @@ -31,7 +31,7 @@ @cython.final cdef class Quaternion: {% block cdef_attributes %} - cdef pandemonium_quat _gd_data + cdef pandemonium_quaternion _gd_data {% endblock %} {% block python_defs %} diff --git a/generation/builtins_templates/rect2i.tmpl.pxi b/generation/builtins_templates/rect2i.tmpl.pxi index 4bc9407..1c172a6 100644 --- a/generation/builtins_templates/rect2i.tmpl.pxi +++ b/generation/builtins_templates/rect2i.tmpl.pxi @@ -43,7 +43,7 @@ cdef class Rect2i: gdapi10.pandemonium_rect2i_new(&self._gd_data, x, y, width, height) @staticmethod - def from_pos_size(Vector2 position not None, Vector2 size not None): + def from_pos_size(Vector2i position not None, Vector2i size not None): {{ force_mark_rendered("pandemonium_rect2i_new_with_position_and_size") }} cdef Rect2i ret = Rect2i.__new__(Rect2i) gdapi10.pandemonium_rect2i_new_with_position_and_size(&ret._gd_data, &position._gd_data, &size._gd_data) @@ -59,10 +59,10 @@ cdef class Rect2i: {{ render_property("position", getter="get_position", setter="set_position") | indent }} @property - def end(Rect2i self) -> Vector2: + def end(Rect2i self) -> Vector2i: cdef pandemonium_vector2i position = gdapi10.pandemonium_rect2i_get_position(&self._gd_data) cdef pandemonium_vector2i size = gdapi10.pandemonium_rect2i_get_size(&self._gd_data) - cdef Vector2 ret = Vector2.__new__(Vector2) + cdef Vector2i ret = Vector2i.__new__(Vector2i) ret._gd_data = gdapi10.pandemonium_vector2i_operator_add(&position, &size) return ret diff --git a/generation/builtins_templates/string_name.tmpl.pxi b/generation/builtins_templates/string_name.tmpl.pxi index e95ddae..6e4c4ce 100644 --- a/generation/builtins_templates/string_name.tmpl.pxi +++ b/generation/builtins_templates/string_name.tmpl.pxi @@ -14,12 +14,12 @@ cdef class StringName: {{ force_mark_rendered("pandemonium_string_name_new") }} cdef pandemonium_string gd_from try: - gdapi10.pandemonium_string_name_new(&self._gd_data, &(from_)._gd_data) + gdapi10.pandemonium_string_name_new_data_string(&self._gd_data, &(from_)._gd_data) except TypeError: if not isinstance(from_, str): raise TypeError("`from_` must be str or GDString") pyobj_to_pandemonium_string(from_, &gd_from) - gdapi10.pandemonium_string_name_new(&self._gd_data, &gd_from) + gdapi10.pandemonium_string_name_new_data_string(&self._gd_data, &gd_from) gdapi10.pandemonium_string_destroy(&gd_from) def __dealloc__(StringName self): @@ -34,8 +34,8 @@ cdef class StringName: def __str__(StringName self): return str(self.get_name()) - {{ render_operator_eq() | indent }} - {{ render_operator_ne() | indent }} + #{{ render_operator_eq() | indent }} + #{{ render_operator_ne() | indent }} {{ render_method("destroy") | indent }} {{ render_method("get_name") | indent }} diff --git a/generation/builtins_templates/vector2i.tmpl.pxi b/generation/builtins_templates/vector2i.tmpl.pxi index 22e9537..4ae92db 100644 --- a/generation/builtins_templates/vector2i.tmpl.pxi +++ b/generation/builtins_templates/vector2i.tmpl.pxi @@ -58,27 +58,27 @@ cdef class Vector2i: {{ render_method("operator_add", py_name="__add__") | indent }} {{ render_method("operator_subtract", py_name="__sub__") | indent }} - def __mul__(Vector2i self, val): - cdef Vector2i _val - try: - _val = val - except TypeError: - return Vector2i_multiply_scalar(self, val) - else: - return Vector2i_multiply_vector(self, _val) + #def __mul__(Vector2i self, val): + # cdef Vector2i _val + # try: + # _val = val + # except TypeError: + # return Vector2i_multiply_scalar(self, val) + # else: + # return Vector2i_multiply_vector(self, _val) - def __truediv__(Vector2i self, val): - cdef Vector2i _val - try: - _val = val - except TypeError: - if val is 0: - raise ZeroDivisionError() - return Vector2i_divide_scalar(self, val) - else: - if _val.x == 0 or _val.y == 0: - raise ZeroDivisionError() - return Vector2i_divide_vector(self, _val) + #def __truediv__(Vector2i self, val): + # cdef Vector2i _val + # try: + # _val = val + # except TypeError: + # if val is 0: + # raise ZeroDivisionError() + # return Vector2i_divide_scalar(self, val) + # else: + # if _val.x == 0 or _val.y == 0: + # raise ZeroDivisionError() + # return Vector2i_divide_vector(self, _val) {{ render_property("x", "get_x", "set_x") | indent }} {{ render_property("y", "get_y", "set_y") | indent }} diff --git a/generation/builtins_templates/vector3i.tmpl.pxi b/generation/builtins_templates/vector3i.tmpl.pxi index 1e01651..e6a77d0 100644 --- a/generation/builtins_templates/vector3i.tmpl.pxi +++ b/generation/builtins_templates/vector3i.tmpl.pxi @@ -90,27 +90,27 @@ cdef class Vector3i: {{ render_method("operator_add", py_name="__add__") | indent }} {{ render_method("operator_subtract", py_name="__sub__") | indent }} - def __mul__(Vector3i self, val): - cdef Vector3i _val - try: - _val = val - except TypeError: - return Vector3i_multiply_scalar(self, val) - else: - return Vector3i_multiply_vector(self, _val) + #def __mul__(Vector3i self, val): + # cdef Vector3i _val + # try: + # _val = val + # except TypeError: + # return Vector3i_multiply_scalar(self, val) + # else: + # return Vector3i_multiply_vector(self, _val) - def __truediv__(Vector3i self, val): - cdef Vector3i _val - try: - _val = val - except TypeError: - if val is 0: - raise ZeroDivisionError() - return Vector3i_divide_scalar(self, val) - else: - if _val.x == 0 or _val.y == 0 or _val.z == 0: - raise ZeroDivisionError() - return Vector3i_divide_vector(self, _val) + #def __truediv__(Vector3i self, val): + # cdef Vector3i _val + # try: + # _val = val + # except TypeError: + # if val is 0: + # raise ZeroDivisionError() + # return Vector3i_divide_scalar(self, val) + # else: + # if _val.x == 0 or _val.y == 0 or _val.z == 0: + # raise ZeroDivisionError() + # return Vector3i_divide_vector(self, _val) {{ render_method("as_string") | indent }} {% endblock %} diff --git a/generation/builtins_templates/vector4i.tmpl.pxi b/generation/builtins_templates/vector4i.tmpl.pxi index 70c1bbb..a35c3fb 100644 --- a/generation/builtins_templates/vector4i.tmpl.pxi +++ b/generation/builtins_templates/vector4i.tmpl.pxi @@ -92,27 +92,27 @@ cdef class Vector4i: {{ render_method("operator_add", py_name="__add__") | indent }} {{ render_method("operator_subtract", py_name="__sub__") | indent }} - def __mul__(Vector4i self, val): - cdef Vector4i _val - try: - _val = val - except TypeError: - return Vector4i_multiply_scalar(self, val) - else: - return Vector4i_multiply_vector(self, _val) + #def __mul__(Vector4i self, val): + # cdef Vector4i _val + # try: + # _val = val + # except TypeError: + # return Vector4i_multiply_scalar(self, val) + # else: + # return Vector4i_multiply_vector(self, _val) - def __truediv__(Vector4i self, val): - cdef Vector4i _val - try: - _val = val - except TypeError: - if val is 0: - raise ZeroDivisionError() - return Vector4i_divide_scalar(self, val) - else: - if _val.x == 0 or _val.y == 0 or _val.z == 0: - raise ZeroDivisionError() - return Vector4i_divide_vector(self, _val) + #def __truediv__(Vector4i self, val): + # cdef Vector4i _val + # try: + # _val = val + # except TypeError: + # if val is 0: + # raise ZeroDivisionError() + # return Vector4i_divide_scalar(self, val) + # else: + # if _val.x == 0 or _val.y == 0 or _val.z == 0: + # raise ZeroDivisionError() + # return Vector4i_divide_vector(self, _val) {{ render_method("as_string") | indent }} {% endblock %} diff --git a/pythonscript/_pandemonium_script.pxi b/pythonscript/_pandemonium_script.pxi index 39d534e..3600b2b 100644 --- a/pythonscript/_pandemonium_script.pxi +++ b/pythonscript/_pandemonium_script.pxi @@ -48,9 +48,9 @@ from pandemonium.tags import ExportedField, SignalField cdef inline pandemonium_pluginscript_script_manifest _build_empty_script_manifest(): cdef pandemonium_pluginscript_script_manifest manifest manifest.data = NULL - gdapi10.pandemonium_string_name_new_data(&manifest.name, "") + gdapi10.pandemonium_string_name_new_data_char(&manifest.name, "") manifest.is_tool = False - gdapi10.pandemonium_string_name_new_data(&manifest.base, "") + gdapi10.pandemonium_string_name_new_data_char(&manifest.base, "") gdapi10.pandemonium_dictionary_new(&manifest.member_lines) gdapi10.pandemonium_array_new(&manifest.methods) gdapi10.pandemonium_array_new(&manifest.signals) diff --git a/pythonscript/pandemonium/_hazmat/conversion.pxd b/pythonscript/pandemonium/_hazmat/conversion.pxd index 4e2d69f..499b698 100644 --- a/pythonscript/pandemonium/_hazmat/conversion.pxd +++ b/pythonscript/pandemonium/_hazmat/conversion.pxd @@ -1,5 +1,9 @@ from libc.stddef cimport wchar_t from libc.stdio cimport printf +from libc.stdint cimport uint_least16_t, uint_least32_t + +ctypedef uint_least16_t char16_t +ctypedef uint_least32_t char32_t from pandemonium._hazmat.gdapi cimport pythonscript_gdapi10 as gdapi10 from pandemonium._hazmat.gdnative_api_struct cimport ( @@ -31,11 +35,11 @@ DEF _STRING_CODEPOINT_LENGTH = 4 cdef inline str pandemonium_string_to_pyobj(const pandemonium_string *p_gdstr): # TODO: unicode&windows support is most likely broken... - cdef char *raw = gdapi10.pandemonium_string_wide_str(p_gdstr) + cdef char *raw = gdapi10.pandemonium_string_get_data(p_gdstr) cdef pandemonium_int length = gdapi10.pandemonium_string_length(p_gdstr) return raw[:length * _STRING_CODEPOINT_LENGTH].decode(_STRING_ENCODING) - # cdef char *raw = gdapi10.pandemonium_string_wide_str(p_gdstr) + # cdef char *raw = gdapi10.pandemonium_string_get_data(p_gdstr) # cdef pandemonium_int length = gdapi10.pandemonium_string_length(p_gdstr) # printf("==========> pandemonium_string_to_pyobj ") # cdef int i @@ -50,8 +54,8 @@ cdef inline str pandemonium_string_to_pyobj(const pandemonium_string *p_gdstr): cdef inline void pyobj_to_pandemonium_string(str pystr, pandemonium_string *p_gdstr): # TODO: unicode&windows support is most likely broken... cdef bytes raw = pystr.encode(_STRING_ENCODING) - gdapi10.pandemonium_string_new_with_wide_string( - p_gdstr, (raw), len(pystr) + gdapi10.pandemonium_string_newc_clip_to_len( + p_gdstr, (raw), len(pystr) ) @@ -61,11 +65,10 @@ cdef inline str pandemonium_string_name_to_pyobj(const pandemonium_string_name * gdapi10.pandemonium_string_destroy(&strname) return ret - cdef inline void pyobj_to_pandemonium_string_name(str pystr, pandemonium_string_name *p_gdname): cdef pandemonium_string strname pyobj_to_pandemonium_string(pystr, &strname) - gdapi10.pandemonium_string_name_new(p_gdname, &strname) + gdapi10.pandemonium_string_name_new_data_string(p_gdname, &strname) gdapi10.pandemonium_string_destroy(&strname) diff --git a/pythonscript/pandemonium/_hazmat/conversion.pyx b/pythonscript/pandemonium/_hazmat/conversion.pyx index 46beafd..b01720a 100644 --- a/pythonscript/pandemonium/_hazmat/conversion.pyx +++ b/pythonscript/pandemonium/_hazmat/conversion.pyx @@ -243,7 +243,7 @@ cdef inline Plane _pandemonium_variant_to_pyobj_plane(const pandemonium_variant cdef inline Quaternion _pandemonium_variant_to_pyobj_quat(const pandemonium_variant *p_gdvar): cdef Quaternion ret = Quaternion.__new__(Quaternion) - ret._gd_data = gdapi10.pandemonium_variant_as_quat(p_gdvar) + ret._gd_data = gdapi10.pandemonium_variant_as_quaternion(p_gdvar) return ret @@ -358,7 +358,7 @@ cdef bint pyobj_to_pandemonium_variant(object pyobj, pandemonium_variant *p_var) elif isinstance(pyobj, Plane): gdapi10.pandemonium_variant_new_plane(p_var, &(pyobj)._gd_data) elif isinstance(pyobj, Quaternion): - gdapi10.pandemonium_variant_new_quat(p_var, &(pyobj)._gd_data) + gdapi10.pandemonium_variant_new_quaternion(p_var, &(pyobj)._gd_data) elif isinstance(pyobj, AABB): gdapi10.pandemonium_variant_new_aabb(p_var, &(pyobj)._gd_data) elif isinstance(pyobj, Basis): diff --git a/pythonscript/pythonscript.c b/pythonscript/pythonscript.c index b9d5f43..88bc870 100644 --- a/pythonscript/pythonscript.c +++ b/pythonscript/pythonscript.c @@ -20,246 +20,245 @@ #include "_pandemonium_api.h" - static const char *PYTHONSCRIPT_RECOGNIZED_EXTENSIONS[] = { "py", "pyc", "pyo", "pyd", 0 }; static const char *PYTHONSCRIPT_RESERVED_WORDS[] = { - "False", - "None", - "True", - "and", - "as", - "assert", - "break", - "class", - "continue", - "def", - "del", - "elif", - "else", - "except", - "finally", - "for", - "from", - "global", - "if", - "import", - "in", - "is", - "lambda", - "nonlocal", - "not", - "or", - "pass", - "raise", - "return", - "try", - "while", - "with", - "yield", - 0 + "False", + "None", + "True", + "and", + "as", + "assert", + "break", + "class", + "continue", + "def", + "del", + "elif", + "else", + "except", + "finally", + "for", + "from", + "global", + "if", + "import", + "in", + "is", + "lambda", + "nonlocal", + "not", + "or", + "pass", + "raise", + "return", + "try", + "while", + "with", + "yield", + 0 }; static const char *PYTHONSCRIPT_COMMENT_DELIMITERS[] = { "#", "\"\"\"\"\"\"", 0 }; static const char *PYTHONSCRIPT_STRING_DELIMITERS[] = { "\" \"", "' '", 0 }; static pandemonium_pluginscript_language_desc desc; static PyThreadState *gilstate = NULL; - /* * Global variables exposing Pandemonium API to the pandemonium.hazmat cython module. * Hence we must initialized them before loading `_pandemonium`/`pandemonium` modules * (which both depend on `pandemonium.hazmat`). */ #ifdef _WIN32 -# define PYTHONSCRIPT_EXPORT __declspec(dllexport) +#define PYTHONSCRIPT_EXPORT __declspec(dllexport) #else -# define PYTHONSCRIPT_EXPORT +#define PYTHONSCRIPT_EXPORT #endif PYTHONSCRIPT_EXPORT const pandemonium_gdnative_core_api_struct *pythonscript_gdapi10 = NULL; PYTHONSCRIPT_EXPORT const pandemonium_gdnative_ext_nativescript_api_struct *pythonscript_gdapi_ext_nativescript = NULL; PYTHONSCRIPT_EXPORT const pandemonium_gdnative_ext_pluginscript_api_struct *pythonscript_gdapi_ext_pluginscript = NULL; PYTHONSCRIPT_EXPORT const pandemonium_gdnative_ext_android_api_struct *pythonscript_gdapi_ext_android = NULL; - static void _register_gdapi(const pandemonium_gdnative_init_options *options) { - pythonscript_gdapi10 = (const pandemonium_gdnative_core_api_struct *)options->api_struct; - /* - if (pythonscript_gdapi10->next) { - pythonscript_gdapi11 = (const pandemonium_gdnative_core_1_1_api_struct *)pythonscript_gdapi10->next; - if (pythonscript_gdapi11->next) { - pythonscript_gdapi12 = (const pandemonium_gdnative_core_1_2_api_struct *)pythonscript_gdapi11->next; - } - } - */ + pythonscript_gdapi10 = (const pandemonium_gdnative_core_api_struct *)options->api_struct; + /* + if (pythonscript_gdapi10->next) { + pythonscript_gdapi11 = (const pandemonium_gdnative_core_1_1_api_struct *)pythonscript_gdapi10->next; + if (pythonscript_gdapi11->next) { + pythonscript_gdapi12 = (const pandemonium_gdnative_core_1_2_api_struct *)pythonscript_gdapi11->next; + } + } + */ - for (unsigned int i = 0; i < pythonscript_gdapi10->num_extensions; i++) { - const pandemonium_gdnative_api_struct *ext = pythonscript_gdapi10->extensions[i]; - switch (ext->type) { - case GDNATIVE_EXT_NATIVESCRIPT: - pythonscript_gdapi_ext_nativescript = (const pandemonium_gdnative_ext_nativescript_api_struct *)ext; - break; - case GDNATIVE_EXT_PLUGINSCRIPT: - pythonscript_gdapi_ext_pluginscript = (const pandemonium_gdnative_ext_pluginscript_api_struct *)ext; - break; - case GDNATIVE_EXT_ANDROID: - pythonscript_gdapi_ext_android = (const pandemonium_gdnative_ext_android_api_struct *)ext; - break; - default: - break; - } - } + for (unsigned int i = 0; i < pythonscript_gdapi10->num_extensions; i++) { + const pandemonium_gdnative_api_struct *ext = pythonscript_gdapi10->extensions[i]; + switch (ext->type) { + case GDNATIVE_EXT_NATIVESCRIPT: + pythonscript_gdapi_ext_nativescript = (const pandemonium_gdnative_ext_nativescript_api_struct *)ext; + break; + case GDNATIVE_EXT_PLUGINSCRIPT: + pythonscript_gdapi_ext_pluginscript = (const pandemonium_gdnative_ext_pluginscript_api_struct *)ext; + break; + case GDNATIVE_EXT_ANDROID: + pythonscript_gdapi_ext_android = (const pandemonium_gdnative_ext_android_api_struct *)ext; + break; + default: + break; + } + } } - GDN_EXPORT void pandemonium_gdnative_init(pandemonium_gdnative_init_options *options) { - // Registering the api should be the very first thing to do ! - _register_gdapi(options); + // Registering the api should be the very first thing to do ! + _register_gdapi(options); - // Now those macros are usable + // Now those macros are usable - #define GD_PRINT(c_msg) { \ - pandemonium_string gd_msg; \ - pythonscript_gdapi10->pandemonium_string_new_with_wide_string( \ - &gd_msg, c_msg, -1); \ - pythonscript_gdapi10->pandemonium_print(&gd_msg); \ - pythonscript_gdapi10->pandemonium_string_destroy(&gd_msg); \ - } +#define GD_PRINT(c_msg) \ + { \ + pandemonium_string gd_msg; \ + pythonscript_gdapi10->pandemonium_string_new_with_wide_string( \ + &gd_msg, c_msg, -1); \ + pythonscript_gdapi10->pandemonium_print(&gd_msg); \ + pythonscript_gdapi10->pandemonium_string_destroy(&gd_msg); \ + } - #define GD_ERROR_PRINT(msg) { \ - pythonscript_gdapi10->pandemonium_print_error(msg, __func__, __FILE__, __LINE__); \ - } +#define GD_ERROR_PRINT(msg) \ + { \ + pythonscript_gdapi10->pandemonium_print_error(msg, __func__, __FILE__, __LINE__); \ + } - // Check for mandatory plugins - /* - if (!pythonscript_gdapi10 || !pythonscript_gdapi11 || !pythonscript_gdapi12) { - GD_ERROR_PRINT("Pandemonium-Python requires GDNative API >= v1.2"); - return; - } - */ - if (!pythonscript_gdapi_ext_pluginscript) { - GD_ERROR_PRINT("Pluginscript extension not available"); - return; - } + // Check for mandatory plugins + /* + if (!pythonscript_gdapi10 || !pythonscript_gdapi11 || !pythonscript_gdapi12) { + GD_ERROR_PRINT("Pandemonium-Python requires GDNative API >= v1.2"); + return; + } + */ + if (!pythonscript_gdapi_ext_pluginscript) { + GD_ERROR_PRINT("Pluginscript extension not available"); + return; + } #ifndef _WIN32 - // Make sure the shared library has all it symbols loaded - // (strange bug with libpython3.x.so otherwise...) - { - const wchar_t *wpath = pythonscript_gdapi10->pandemonium_string_wide_str( - options->active_library_path - ); - char path[300]; - wcstombs(path, wpath, 300); - dlopen(path, RTLD_NOW | RTLD_GLOBAL); - } + // Make sure the shared library has all it symbols loaded + // (strange bug with libpython3.x.so otherwise...) + { + pandemonium_char_16_string wpstr = pythonscript_gdapi10->pandemonium_string_utf16(options->active_library_path); + const wchar_t *wpath = (wchar_t *)pythonscript_gdapi10->pandemonium_char_16_string_get_data(&wpstr); - const char *err = dlerror(); - if (err) { - GD_ERROR_PRINT(err); - return; - } + //const wchar_t *wpath = pythonscript_gdapi10->pandemonium_string_wide_str( + // options->active_library_path + //); + char path[300]; + wcstombs(path, wpath, 300); + dlopen(path, RTLD_NOW | RTLD_GLOBAL); + } + + const char *err = dlerror(); + if (err) { + GD_ERROR_PRINT(err); + return; + } #endif - // Initialize CPython interpreter + // Initialize CPython interpreter - // Retrieve path and set pythonhome - { - static wchar_t pythonhome[300]; - pandemonium_string _pythonhome = pythonscript_gdapi10->pandemonium_string_get_base_dir( - options->active_library_path - ); - wcsncpy(pythonhome, pythonscript_gdapi10->pandemonium_string_wide_str(&_pythonhome), 300); - pythonscript_gdapi10->pandemonium_string_destroy(&_pythonhome); - Py_SetPythonHome(pythonhome); - } - // TODO: site.USER_SITE seems to point to an invalid location in ~/.local - // // Add current dir to PYTHONPATH - // wchar_t *path = Py_GetPath(); - // int new_path_len = wcslen(path) + 3; - // wchar_t new_path[new_path_len * sizeof(wchar_t)]; - // wcsncpy(new_path, L".:", new_path_len); - // wcsncpy(new_path + 2, path, new_path_len - 2); - // Py_SetPath(new_path); - // PyRun_SimpleString("import sys\nprint('PYTHON_PATH:', sys.path)\n"); + // Retrieve path and set pythonhome + { + static wchar_t pythonhome[300]; + pandemonium_string _pythonhome = pythonscript_gdapi10->pandemonium_string_get_base_dir( + options->active_library_path); - Py_SetProgramName(L"pandemonium"); - // Initialize interpreter but skip initialization registration of signal handlers - Py_InitializeEx(0); - // PyEval_InitThreads acquires the GIL, so we must release it later. - // Since python3.7 PyEval_InitThreads is automatically called by Py_InitializeEx, but it's better to leave it here - // to be explicit. Calling it again does nothing. - PyEval_InitThreads(); - int ret = import__pandemonium(); - if (ret != 0){ - GD_ERROR_PRINT("Cannot load pandemonium python module"); - return; - } + pandemonium_char_16_string wpstr = pythonscript_gdapi10->pandemonium_string_utf16(&_pythonhome); + wcsncpy(pythonhome, (wchar_t *)pythonscript_gdapi10->pandemonium_char_16_string_get_data(&wpstr), 300); + pythonscript_gdapi10->pandemonium_string_destroy(&_pythonhome); + Py_SetPythonHome(pythonhome); + } + // TODO: site.USER_SITE seems to point to an invalid location in ~/.local + // // Add current dir to PYTHONPATH + // wchar_t *path = Py_GetPath(); + // int new_path_len = wcslen(path) + 3; + // wchar_t new_path[new_path_len * sizeof(wchar_t)]; + // wcsncpy(new_path, L".:", new_path_len); + // wcsncpy(new_path + 2, path, new_path_len - 2); + // Py_SetPath(new_path); + // PyRun_SimpleString("import sys\nprint('PYTHON_PATH:', sys.path)\n"); - desc.name = "Python"; - desc.type = "Python"; - desc.extension = "py"; - desc.recognized_extensions = PYTHONSCRIPT_RECOGNIZED_EXTENSIONS; - desc.init = pythonscript_init; - desc.finish = pythonscript_finish; - desc.reserved_words = PYTHONSCRIPT_RESERVED_WORDS; - desc.comment_delimiters = PYTHONSCRIPT_COMMENT_DELIMITERS; - desc.string_delimiters = PYTHONSCRIPT_STRING_DELIMITERS; - desc.has_named_classes = false; - desc.add_global_constant = pythonscript_add_global_constant; + Py_SetProgramName(L"pandemonium"); + // Initialize interpreter but skip initialization registration of signal handlers + Py_InitializeEx(0); + // PyEval_InitThreads acquires the GIL, so we must release it later. + // Since python3.7 PyEval_InitThreads is automatically called by Py_InitializeEx, but it's better to leave it here + // to be explicit. Calling it again does nothing. + PyEval_InitThreads(); + int ret = import__pandemonium(); + if (ret != 0) { + GD_ERROR_PRINT("Cannot load pandemonium python module"); + return; + } - desc.script_desc.init = pythonscript_script_init; - desc.script_desc.finish = pythonscript_script_finish; + desc.name = "Python"; + desc.type = "Python"; + desc.extension = "py"; + desc.recognized_extensions = PYTHONSCRIPT_RECOGNIZED_EXTENSIONS; + desc.init = pythonscript_init; + desc.finish = pythonscript_finish; + desc.reserved_words = PYTHONSCRIPT_RESERVED_WORDS; + desc.comment_delimiters = PYTHONSCRIPT_COMMENT_DELIMITERS; + desc.string_delimiters = PYTHONSCRIPT_STRING_DELIMITERS; + desc.has_named_classes = false; + desc.add_global_constant = pythonscript_add_global_constant; - desc.script_desc.instance_desc.init = pythonscript_instance_init; - desc.script_desc.instance_desc.finish = pythonscript_instance_finish; - desc.script_desc.instance_desc.set_prop = pythonscript_instance_set_prop; - desc.script_desc.instance_desc.get_prop = pythonscript_instance_get_prop; - desc.script_desc.instance_desc.call_method = pythonscript_instance_call_method; - desc.script_desc.instance_desc.notification = pythonscript_instance_notification; - desc.script_desc.instance_desc.refcount_incremented = NULL; - desc.script_desc.instance_desc.refcount_decremented = NULL; + desc.script_desc.init = pythonscript_script_init; + desc.script_desc.finish = pythonscript_script_finish; - if (options->in_editor) { + desc.script_desc.instance_desc.init = pythonscript_instance_init; + desc.script_desc.instance_desc.finish = pythonscript_instance_finish; + desc.script_desc.instance_desc.set_prop = pythonscript_instance_set_prop; + desc.script_desc.instance_desc.get_prop = pythonscript_instance_get_prop; + desc.script_desc.instance_desc.call_method = pythonscript_instance_call_method; + desc.script_desc.instance_desc.notification = pythonscript_instance_notification; + desc.script_desc.instance_desc.refcount_incremented = NULL; + desc.script_desc.instance_desc.refcount_decremented = NULL; - desc.get_template_source_code = pythonscript_get_template_source_code; - desc.validate = pythonscript_validate; - desc.find_function = pythonscript_find_function; - desc.make_function = pythonscript_make_function; - desc.complete_code = pythonscript_complete_code; - desc.auto_indent_code = pythonscript_auto_indent_code; + if (options->in_editor) { + desc.get_template_source_code = pythonscript_get_template_source_code; + desc.validate = pythonscript_validate; + desc.find_function = pythonscript_find_function; + desc.make_function = pythonscript_make_function; + desc.complete_code = pythonscript_complete_code; + desc.auto_indent_code = pythonscript_auto_indent_code; - desc.debug_get_error = pythonscript_debug_get_error; - desc.debug_get_stack_level_count = pythonscript_debug_get_stack_level_count; - desc.debug_get_stack_level_line = pythonscript_debug_get_stack_level_line; - desc.debug_get_stack_level_function = pythonscript_debug_get_stack_level_function; - desc.debug_get_stack_level_source = pythonscript_debug_get_stack_level_source; - desc.debug_get_stack_level_locals = pythonscript_debug_get_stack_level_locals; - desc.debug_get_stack_level_members = pythonscript_debug_get_stack_level_members; - desc.debug_get_globals = pythonscript_debug_get_globals; - desc.debug_parse_stack_level_expression = pythonscript_debug_parse_stack_level_expression; + desc.debug_get_error = pythonscript_debug_get_error; + desc.debug_get_stack_level_count = pythonscript_debug_get_stack_level_count; + desc.debug_get_stack_level_line = pythonscript_debug_get_stack_level_line; + desc.debug_get_stack_level_function = pythonscript_debug_get_stack_level_function; + desc.debug_get_stack_level_source = pythonscript_debug_get_stack_level_source; + desc.debug_get_stack_level_locals = pythonscript_debug_get_stack_level_locals; + desc.debug_get_stack_level_members = pythonscript_debug_get_stack_level_members; + desc.debug_get_globals = pythonscript_debug_get_globals; + desc.debug_parse_stack_level_expression = pythonscript_debug_parse_stack_level_expression; - desc.profiling_start = pythonscript_profiling_start; - desc.profiling_stop = pythonscript_profiling_stop; - desc.profiling_get_accumulated_data = pythonscript_profiling_get_accumulated_data; - desc.profiling_get_frame_data = pythonscript_profiling_get_frame_data; - desc.profiling_frame = pythonscript_profiling_frame; - } - pythonscript_gdapi_ext_pluginscript->pandemonium_pluginscript_register_language(&desc); + desc.profiling_start = pythonscript_profiling_start; + desc.profiling_stop = pythonscript_profiling_stop; + desc.profiling_get_accumulated_data = pythonscript_profiling_get_accumulated_data; + desc.profiling_get_frame_data = pythonscript_profiling_get_frame_data; + desc.profiling_frame = pythonscript_profiling_frame; + } + pythonscript_gdapi_ext_pluginscript->pandemonium_pluginscript_register_language(&desc); - // Release the Kraken... er I mean the GIL ! - gilstate = PyEval_SaveThread(); + // Release the Kraken... er I mean the GIL ! + gilstate = PyEval_SaveThread(); } - GDN_EXPORT void pandemonium_gdnative_singleton() { } - GDN_EXPORT void pandemonium_gdnative_terminate() { - // Re-acquire the gil in order to finalize properly - PyEval_RestoreThread(gilstate); + // Re-acquire the gil in order to finalize properly + PyEval_RestoreThread(gilstate); - int ret = Py_FinalizeEx(); - if (ret != 0) { - GD_ERROR_PRINT("Cannot finalize python interpreter"); - } + int ret = Py_FinalizeEx(); + if (ret != 0) { + GD_ERROR_PRINT("Cannot finalize python interpreter"); + } }