prepared engine swap

This commit is contained in:
2023-12-18 21:01:27 +01:00
parent 83e363f7c7
commit 1b3b8666d3
3098 changed files with 446024 additions and 0 deletions

View File

@ -0,0 +1,44 @@
# Start with a sanity check to ensure the loading is done from Godot-Python
# (and not from a regular Python interpreter which would lead to a segfault).
# The idea is we should have the following loading order:
# godot binary -> pythonscript.so -> _godot.so -> godot/__init__.py
import sys
if "_godot" not in sys.modules:
raise ImportError(
"Cannot initialize godot module given Godot GDNative API not available.\n"
"This is most likely because you are running code from a regular Python interpreter"
" (i.e. doing something like `python my_script.py`) while godot module is only available"
" to Python code loaded from Godot through Godot-Python plugin."
)
del sys
from godot._version import __version__
from godot.tags import (
MethodRPCMode,
PropertyHint,
PropertyUsageFlag,
rpcdisabled,
rpcremote,
rpcmaster,
rpcpuppet,
rpcslave,
rpcremotesync,
rpcsync,
rpcmastersync,
rpcpuppetsync,
signal,
export,
exposed,
)
from godot.pool_arrays import (
PoolIntArray,
PoolRealArray,
PoolByteArray,
PoolVector2Array,
PoolVector3Array,
PoolColorArray,
PoolStringArray,
)
from godot.builtins import *
from godot.bindings import *

View File

@ -0,0 +1,3 @@
# `_hazmat` package containing all stuff not to be exposed to the user.
# Note we don't reexport anything in the `__init__.pxd` (i.e. this file)
# to avoid subtle recursive dependency errors within cython.

View File

@ -0,0 +1,136 @@
from libc.stddef cimport wchar_t
from libc.stdio cimport printf
from godot._hazmat.gdapi cimport pythonscript_gdapi10 as gdapi10
from godot._hazmat.gdnative_api_struct cimport (
godot_string,
godot_string_name,
godot_int,
godot_vector2,
godot_variant,
godot_variant_type,
)
from godot.builtins cimport GDString, NodePath
# Godot string are basically a vector of wchar_t, each wchar_t representing
# a single unicode character (i.e. there is no surrogates support).
# The sad part is wchar_t is not portable: it is 16bits long on Windows and
# 32bits long on Linux and MacOS...
# So we end up with a UCS2 encoding on Windows and UCS4 everywhere else :'(
IF UNAME_SYSNAME == "Windows":
# Specify endianess otherwise `encode` appends a BOM at the start of the converted string
DEF _STRING_ENCODING = "UTF-16-LE"
DEF _STRING_CODEPOINT_LENGTH = 2
ELSE:
DEF _STRING_ENCODING = "UTF-32-LE"
DEF _STRING_CODEPOINT_LENGTH = 4
cdef inline str godot_string_to_pyobj(const godot_string *p_gdstr):
# TODO: unicode&windows support is most likely broken...
cdef char *raw = <char*>gdapi10.godot_string_wide_str(p_gdstr)
cdef godot_int length = gdapi10.godot_string_length(p_gdstr)
return raw[:length * _STRING_CODEPOINT_LENGTH].decode(_STRING_ENCODING)
# cdef char *raw = <char*>gdapi10.godot_string_wide_str(p_gdstr)
# cdef godot_int length = gdapi10.godot_string_length(p_gdstr)
# printf("==========> godot_string_to_pyobj ")
# cdef int i
# for i in range(length):
# printf("%c ", raw[i * 4]);
# printf("\n")
# cdef object ret = raw[:length * _STRING_CODEPOINT_LENGTH].decode(_STRING_ENCODING)
# print('==>ret: %r' % ret)
# return ret
cdef inline void pyobj_to_godot_string(str pystr, godot_string *p_gdstr):
# TODO: unicode&windows support is most likely broken...
cdef bytes raw = pystr.encode(_STRING_ENCODING)
gdapi10.godot_string_new_with_wide_string(
p_gdstr, (<wchar_t*><char*>raw), len(pystr)
)
cdef inline str godot_string_name_to_pyobj(const godot_string_name *p_gdname):
cdef godot_string strname = gdapi10.godot_string_name_get_name(p_gdname)
cdef ret = godot_string_to_pyobj(&strname)
gdapi10.godot_string_destroy(&strname)
return ret
cdef inline void pyobj_to_godot_string_name(str pystr, godot_string_name *p_gdname):
cdef godot_string strname
pyobj_to_godot_string(pystr, &strname)
gdapi10.godot_string_name_new(p_gdname, &strname)
gdapi10.godot_string_destroy(&strname)
cdef object godot_variant_to_pyobj(const godot_variant *p_gdvar)
cdef bint pyobj_to_godot_variant(object pyobj, godot_variant *p_var)
cdef bint is_pytype_compatible_with_godot_variant(object pytype)
cdef object godot_type_to_pytype(godot_variant_type gdtype)
cdef godot_variant_type pytype_to_godot_type(object pytype)
cdef GDString ensure_is_gdstring(object gdstring_or_pystr)
cdef NodePath ensure_is_nodepath(object nodepath_or_pystr)
# TODO: finish this...
# cdef inline object cook_slice(slice slice_, godot_int size, godot_int *r_start, godot_int *r_stop, godot_int *r_step, godot_int *r_items):
# cdef godot_int start
# cdef godot_int stop
# cdef godot_int step
# step = slice_.step if slice_.step is not None else 1
# if step == 0:
# raise ValueError("range() arg 3 must not be zero")
# elif step > 0:
# start = slice_.start if slice_.start is not None else 0
# stop = slice_.stop if slice_.stop is not None else size
# else:
# start = slice_.start if slice_.start is not None else size
# stop = slice_.stop if slice_.stop is not None else -size - 1
# r_start[0] = cook_slice_start(size, start)
# r_stop[0] = cook_slice_stop(size, stop)
# r_step[0] = step
# r_items[0] = cook_slice_get_items(size, start, stop, step)
# return None
# cdef inline godot_int cook_slice_start(godot_int size, godot_int start):
# if start > size - 1:
# return size - 1
# elif start < 0:
# start += size
# if start < 0:
# return 0
# return start
# cdef inline godot_int cook_slice_stop(godot_int size, godot_int stop):
# if stop > size:
# return size
# elif stop < -size:
# return -1
# elif stop < 0:
# stop += size
# return stop
# cdef inline godot_int cook_slice_get_items(godot_int size, godot_int start, godot_int stop, godot_int step):
# cdef godot_int items
# if step > 0:
# if start >= stop:
# return 0
# items = 1 + (stop - start - 1) // step
# else:
# if start <= stop:
# return 0
# items = 1 + (stop - start + 1) // step
# return items if items > 0 else 0

View File

@ -0,0 +1,38 @@
from godot._hazmat.gdnative_api_struct cimport (
godot_gdnative_core_api_struct,
godot_gdnative_core_1_1_api_struct,
godot_gdnative_core_1_2_api_struct,
godot_gdnative_ext_nativescript_api_struct,
godot_gdnative_ext_pluginscript_api_struct,
godot_gdnative_ext_android_api_struct,
godot_gdnative_ext_arvr_api_struct,
)
cdef extern from * nogil:
# Global variables defined in pythonscript.c
# Just easier to inline the definitions instead of use a header file
# and having to tweak compile flags.
"""
#include <gdnative_api_struct.gen.h>
#ifdef _WIN32
# define PYTHONSCRIPT_IMPORT __declspec(dllimport)
#else
# define PYTHONSCRIPT_IMPORT
#endif
PYTHONSCRIPT_IMPORT extern const godot_gdnative_core_api_struct *pythonscript_gdapi10;
PYTHONSCRIPT_IMPORT extern const godot_gdnative_core_1_1_api_struct *pythonscript_gdapi11;
PYTHONSCRIPT_IMPORT extern const godot_gdnative_core_1_2_api_struct *pythonscript_gdapi12;
PYTHONSCRIPT_IMPORT extern const godot_gdnative_ext_nativescript_api_struct *pythonscript_gdapi_ext_nativescript;
PYTHONSCRIPT_IMPORT extern const godot_gdnative_ext_pluginscript_api_struct *pythonscript_gdapi_ext_pluginscript;
PYTHONSCRIPT_IMPORT extern const godot_gdnative_ext_android_api_struct *pythonscript_gdapi_ext_android;
PYTHONSCRIPT_IMPORT extern const godot_gdnative_ext_arvr_api_struct *pythonscript_gdapi_ext_arvr;
"""
cdef const godot_gdnative_core_api_struct *pythonscript_gdapi10
cdef const godot_gdnative_core_1_1_api_struct *pythonscript_gdapi11
cdef const godot_gdnative_core_1_2_api_struct *pythonscript_gdapi12
cdef const godot_gdnative_ext_nativescript_api_struct *pythonscript_gdapi_ext_nativescript
cdef const godot_gdnative_ext_pluginscript_api_struct *pythonscript_gdapi_ext_pluginscript
cdef const godot_gdnative_ext_android_api_struct *pythonscript_gdapi_ext_android
cdef const godot_gdnative_ext_arvr_api_struct *pythonscript_gdapi_ext_arvr

View File

@ -0,0 +1,18 @@
from godot.bindings cimport Object
cdef bint __pythonscript_verbose
cdef inline bint get_pythonscript_verbose():
return __pythonscript_verbose
cdef inline void set_pythonscript_verbose(bint status):
global __pythonscript_verbose
__pythonscript_verbose = status
cdef object get_exposed_class(str module_name)
cdef void set_exposed_class(object cls)
cdef void destroy_exposed_class(object cls)

View File

@ -0,0 +1,205 @@
# /!\ Autogenerated code, modifications will be lost /!\
# see `generation/generate_builtins.py`
cimport cython
from godot._hazmat.gdnative_api_struct cimport *
from godot.pool_arrays cimport (
PoolIntArray,
PoolRealArray,
PoolByteArray,
PoolVector2Array,
PoolVector3Array,
PoolColorArray,
PoolStringArray,
)
@cython.final
cdef class RID:
cdef godot_rid _gd_data
@cython.final
cdef class Vector3:
cdef godot_vector3 _gd_data
@cython.final
cdef class Vector2:
cdef godot_vector2 _gd_data
@cython.final
cdef class AABB:
cdef godot_aabb _gd_data
@cython.final
cdef class Basis:
cdef godot_basis _gd_data
@cython.final
cdef class Color:
cdef godot_color _gd_data
@cython.final
cdef class GDString:
cdef godot_string _gd_data
@staticmethod
cdef inline GDString new()
@staticmethod
cdef inline GDString new_with_wide_string(wchar_t *content, int size)
@staticmethod
cdef inline GDString from_ptr(const godot_string *_ptr)
@cython.final
cdef class Rect2:
cdef godot_rect2 _gd_data
@cython.final
cdef class Transform2D:
cdef godot_transform2d _gd_data
@cython.final
cdef class Plane:
cdef godot_plane _gd_data
@cython.final
cdef class Quat:
cdef godot_quat _gd_data
@cython.final
cdef class Transform:
cdef godot_transform _gd_data
@cython.final
cdef class NodePath:
cdef godot_node_path _gd_data
@cython.final
cdef class Dictionary:
cdef godot_dictionary _gd_data
@staticmethod
cdef inline Dictionary new()
@staticmethod
cdef inline Dictionary from_ptr(const godot_dictionary *_ptr)
cdef inline operator_update(self, Dictionary items)
cdef inline bint operator_equal(self, Dictionary other)
@cython.final
cdef class Array:
cdef godot_array _gd_data
@staticmethod
cdef inline Array new()
@staticmethod
cdef inline Array from_ptr(const godot_array *_ptr)
cdef inline Array operator_getslice(self, godot_int start, godot_int stop, godot_int step)
cdef inline bint operator_equal(self, Array other)
cdef inline Array operator_add(self, Array items)
cdef inline operator_iadd(self, Array items)

View File

@ -0,0 +1,12 @@
from _godot import __global_constants
def __getattr__(name):
try:
return __global_constants[name]
except KeyError:
raise AttributeError
def __dir__():
return list(__global_constants.keys())

View File

@ -0,0 +1,19 @@
# Public low-level APIs are exposed here
from godot._hazmat cimport gdnative_api_struct
# Re-expose Godot API with better names
from godot._hazmat.gdapi cimport (
pythonscript_gdapi10 as gdapi10,
pythonscript_gdapi11 as gdapi11,
pythonscript_gdapi12 as gdapi12,
pythonscript_gdapi_ext_nativescript as gdapi_ext_nativescript,
pythonscript_gdapi_ext_pluginscript as gdapi_ext_pluginscript,
pythonscript_gdapi_ext_android as gdapi_ext_android,
pythonscript_gdapi_ext_arvr as gdapi_ext_arvr,
)
from godot._hazmat.conversion cimport (
godot_string_to_pyobj,
pyobj_to_godot_string,
godot_variant_to_pyobj,
pyobj_to_godot_variant,
)

View File

@ -0,0 +1,273 @@
# /!\ Autogenerated code, modifications will be lost /!\
# see `generation/generate_pool_arrays.py`
cimport cython
from godot._hazmat.gdapi cimport (
pythonscript_gdapi10 as gdapi10,
pythonscript_gdapi11 as gdapi11,
pythonscript_gdapi12 as gdapi12,
)
from godot._hazmat.gdnative_api_struct cimport (
godot_int,
godot_pool_int_array,
godot_pool_int_array_write_access,
godot_pool_int_array_read_access,
godot_real,
godot_pool_real_array,
godot_pool_real_array_write_access,
godot_pool_real_array_read_access,
uint8_t,
godot_pool_byte_array,
godot_pool_byte_array_write_access,
godot_pool_byte_array_read_access,
godot_vector2,
godot_pool_vector2_array,
godot_pool_vector2_array_write_access,
godot_pool_vector2_array_read_access,
godot_vector3,
godot_pool_vector3_array,
godot_pool_vector3_array_write_access,
godot_pool_vector3_array_read_access,
godot_color,
godot_pool_color_array,
godot_pool_color_array_write_access,
godot_pool_color_array_read_access,
godot_string,
godot_pool_string_array,
godot_pool_string_array_write_access,
godot_pool_string_array_read_access,
)
from godot.builtins cimport (
Array,
Vector2,
Vector3,
Color,
GDString,
)
@cython.final
cdef class PoolIntArray:
cdef godot_pool_int_array _gd_data
@staticmethod
cdef inline PoolIntArray new()
@staticmethod
cdef inline PoolIntArray new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolIntArray other)
cdef inline godot_int operator_getitem(self, godot_int index)
cdef inline PoolIntArray operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolIntArray copy(self)
cpdef inline void append(self, godot_int data)
cdef inline void append_array(self, PoolIntArray array)
cpdef inline void invert(self)
cpdef inline void push_back(self, godot_int data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolIntArrayWriteAccess:
cdef godot_int *_gd_ptr
@cython.final
cdef class PoolRealArray:
cdef godot_pool_real_array _gd_data
@staticmethod
cdef inline PoolRealArray new()
@staticmethod
cdef inline PoolRealArray new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolRealArray other)
cdef inline godot_real operator_getitem(self, godot_int index)
cdef inline PoolRealArray operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolRealArray copy(self)
cpdef inline void append(self, godot_real data)
cdef inline void append_array(self, PoolRealArray array)
cpdef inline void invert(self)
cpdef inline void push_back(self, godot_real data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolRealArrayWriteAccess:
cdef godot_real *_gd_ptr
@cython.final
cdef class PoolByteArray:
cdef godot_pool_byte_array _gd_data
@staticmethod
cdef inline PoolByteArray new()
@staticmethod
cdef inline PoolByteArray new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolByteArray other)
cdef inline uint8_t operator_getitem(self, godot_int index)
cdef inline PoolByteArray operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolByteArray copy(self)
cpdef inline void append(self, uint8_t data)
cdef inline void append_array(self, PoolByteArray array)
cpdef inline void invert(self)
cpdef inline void push_back(self, uint8_t data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolByteArrayWriteAccess:
cdef uint8_t *_gd_ptr
@cython.final
cdef class PoolVector2Array:
cdef godot_pool_vector2_array _gd_data
@staticmethod
cdef inline PoolVector2Array new()
@staticmethod
cdef inline PoolVector2Array new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolVector2Array other)
cdef inline Vector2 operator_getitem(self, godot_int index)
cdef inline PoolVector2Array operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolVector2Array copy(self)
cpdef inline void append(self, Vector2 data)
cdef inline void append_array(self, PoolVector2Array array)
cpdef inline void invert(self)
cpdef inline void push_back(self, Vector2 data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolVector2ArrayWriteAccess:
cdef godot_vector2 *_gd_ptr
@cython.final
cdef class PoolVector3Array:
cdef godot_pool_vector3_array _gd_data
@staticmethod
cdef inline PoolVector3Array new()
@staticmethod
cdef inline PoolVector3Array new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolVector3Array other)
cdef inline Vector3 operator_getitem(self, godot_int index)
cdef inline PoolVector3Array operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolVector3Array copy(self)
cpdef inline void append(self, Vector3 data)
cdef inline void append_array(self, PoolVector3Array array)
cpdef inline void invert(self)
cpdef inline void push_back(self, Vector3 data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolVector3ArrayWriteAccess:
cdef godot_vector3 *_gd_ptr
@cython.final
cdef class PoolColorArray:
cdef godot_pool_color_array _gd_data
@staticmethod
cdef inline PoolColorArray new()
@staticmethod
cdef inline PoolColorArray new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolColorArray other)
cdef inline Color operator_getitem(self, godot_int index)
cdef inline PoolColorArray operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolColorArray copy(self)
cpdef inline void append(self, Color data)
cdef inline void append_array(self, PoolColorArray array)
cpdef inline void invert(self)
cpdef inline void push_back(self, Color data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolColorArrayWriteAccess:
cdef godot_color *_gd_ptr
@cython.final
cdef class PoolStringArray:
cdef godot_pool_string_array _gd_data
@staticmethod
cdef inline PoolStringArray new()
@staticmethod
cdef inline PoolStringArray new_with_array(Array other)
# Operators
cdef inline bint operator_equal(self, PoolStringArray other)
cdef inline GDString operator_getitem(self, godot_int index)
cdef inline PoolStringArray operator_getslice(self, godot_int start, godot_int end, godot_int step)
# Methods
cpdef inline PoolStringArray copy(self)
cpdef inline void append(self, GDString data)
cdef inline void append_array(self, PoolStringArray array)
cpdef inline void invert(self)
cpdef inline void push_back(self, GDString data)
cpdef inline void resize(self, godot_int size)
cdef inline godot_int size(self)
@cython.final
cdef class PoolStringArrayWriteAccess:
cdef godot_string *_gd_ptr