Skip to content

Commit 5a7c5bb

Browse files
committed
Add 1-cython-access-gdextension test case
1 parent 31d7320 commit 5a7c5bb

File tree

12 files changed

+162
-1
lines changed

12 files changed

+162
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
res://pythonscript.gdextension
2+
res://my.gdextension
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import platform
2+
from pathlib import Path
3+
import subprocess
4+
import shutil
5+
6+
7+
PROJECT_DIR = Path(__file__).resolve().parent
8+
9+
if platform.system() == "Windows":
10+
python_path = PROJECT_DIR / "addons/pythonscript/windows-x86_64/python.exe"
11+
lib_pattern = "my.*.pyd"
12+
elif platform.system() == "Darwin":
13+
python_path = PROJECT_DIR / "addons/pythonscript/macos-x86_64/bin/python"
14+
lib_pattern = "my.*.dylib"
15+
else:
16+
assert platform.system() == "Linux"
17+
python_path = PROJECT_DIR / "addons/pythonscript/linux-x86_64/bin/python"
18+
lib_pattern = "my.*.so"
19+
20+
cmd = [str(python_path), "setup.py", "build_ext", "--build-lib", str(PROJECT_DIR)]
21+
print(" ".join(cmd))
22+
subprocess.check_call(cmd, cwd=PROJECT_DIR)
23+
24+
# Finally remove the platform info from the shared library, this is to avoid
25+
# annoying update everytime we change CPython embedded version
26+
27+
lib_candidates = list(PROJECT_DIR.glob(lib_pattern))
28+
assert len(lib_candidates) == 1, lib_candidates
29+
lib = lib_candidates[0]
30+
lib_new_name = f"my{lib.suffix}"
31+
print(f"rename {lib.name} -> {lib_new_name}")
32+
shutil.move(lib, lib.parent / lib_new_name)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const PROJECT_SETTING_NAME = "my/result"
2+
3+
func _ready():
4+
# The Cython gdextension module should have created this project setting
5+
var project_setting_value = ProjectSettings.get_setting(PROJECT_SETTING_NAME)
6+
if project_setting_value != "all_good":
7+
printerr("Missing/invalid project setting `{}`".format(project_setting_value))
8+
self.get_tree().quit(2)
9+
else:
10+
self.get_tree().quit(0)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://2mikyvxkygni"]
2+
3+
[ext_resource type="Script" path="res://main.gd" id="1_2fqr4"]
4+
5+
[node name="Node" type="Node"]
6+
script = ExtResource("1_2fqr4")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[configuration]
2+
3+
entry_symbol = "my_init"
4+
5+
[libraries]
6+
7+
linux.64="res://my.so"
8+
linux.32="res://my.so"
9+
windows.64="res://my.pyd"
10+
windows.32="res://my.pyd"
11+
macos.64="res://my.dylib"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# cython: language_level=3
2+
3+
from godot.hazmat.gdapi cimport *
4+
5+
6+
cdef public void _my_initialize(void *userdata, GDExtensionInitializationLevel p_level) with gil:
7+
print("==> _initialize")
8+
9+
10+
cdef extern void _my_deinitialize(void *userdata, GDExtensionInitializationLevel p_level) with gil:
11+
print("==> _deinitialize")
12+
13+
14+
cdef extern GDExtensionBool _my_init(
15+
const GDExtensionInterface *p_interface,
16+
const GDExtensionClassLibraryPtr p_library,
17+
GDExtensionInitialization *r_initialization
18+
) nogil:
19+
# print("==> _my_init")
20+
r_initialization.minimum_initialization_level = GDEXTENSION_INITIALIZATION_SERVERS
21+
r_initialization.userdata = NULL
22+
r_initialization.initialize = _my_initialize
23+
r_initialization.deinitialize = _my_deinitialize
24+
return True
25+
26+
27+
cdef extern from * nogil:
28+
"""
29+
#include <godot/gdextension_interface.h>
30+
#ifdef _WIN32
31+
# define DLL_EXPORT __declspec(dllexport)
32+
#else
33+
# define DLL_EXPORT
34+
#endif
35+
36+
GDExtensionBool _my_init(const GDExtensionInterface *, const GDExtensionClassLibraryPtr, GDExtensionInitialization *);
37+
DLL_EXPORT GDExtensionBool my_init(
38+
const GDExtensionInterface *p_interface,
39+
const GDExtensionClassLibraryPtr p_library,
40+
GDExtensionInitialization *r_initialization
41+
) {
42+
return _my_init(p_interface, p_library, r_initialization);
43+
}
44+
"""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=5
10+
11+
[application]
12+
13+
run/main_scene="res://main.tscn"
14+
config/features=PackedStringArray("4.0")
15+
name="1-gdextension"
16+
main_scene="res://main.tscn"
17+
18+
[python_script]
19+
20+
io_streams_capture=false
21+
verbose=true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel", "Cython"]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[configuration]
2+
3+
entry_symbol = "pythonscript_init"
4+
5+
[libraries]
6+
7+
linux.64="res://addons/pythonscript/linux-x86_64/libpythonscript.so"
8+
linux.32="res://addons/pythonscript/linux-x86/libpythonscript.so"
9+
windows.64="res://addons/pythonscript/windows-x86_64/pythonscript.dll"
10+
windows.32="res://addons/pythonscript/windows-x86/pythonscript.dll"
11+
macos.64="res://addons/pythonscript/macos-x86_64/libpythonscript.dylib"

0 commit comments

Comments
Β (0)