Skip to content

Commit a709225

Browse files
bors[bot]toasteater
andauthored
Merge #636
636: Add wrapper for try_from_instance_id r=toasteater a=toasteater Close #464 Co-authored-by: toasteater <[email protected]>
2 parents 6695a29 + 50ea49b commit a709225

40 files changed

+862
-71
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- **The minimum compatible engine version is now 3.2-stable.**
13+
1014
## [0.9.1] - 2020-10-19
1115

1216
### Added

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The bindings cover most of the exposed API of Godot 3.2, and are being used on a
1717

1818
We are serious about engine compatibility. We are committed to keeping compatibility with the latest stable patch releases of all minor versions of the engine, starting from Godot 3.2.
1919

20-
The current minimum compatible version, with `api.json` replacement, is Godot 3.1.1-stable. Changes to this will be considered a breaking change, and will be called out in the release notes.
20+
The current minimum compatible version, with `api.json` replacement, is Godot 3.2-stable. Changes to this will be considered a breaking change, and will be called out in the release notes.
2121

2222
The bindings do *not* support Godot 4.0 (`master` branch) currently.
2323

gdnative-core/src/object.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,36 @@ pub unsafe trait GodotObject: Sized + crate::private::godot_object::Sealed {
151151
{
152152
Ref::from_sys(self.as_raw().sys())
153153
}
154+
155+
/// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
156+
/// still alive. See also `TRef::try_from_instance_id`.
157+
///
158+
/// # Safety
159+
///
160+
/// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
161+
/// have exclusive access to the underlying object, if it is still alive.
162+
#[inline]
163+
unsafe fn try_from_instance_id<'a>(id: i64) -> Option<TRef<'a, Self, Shared>> {
164+
TRef::try_from_instance_id(id)
165+
}
166+
167+
/// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
168+
/// still alive, and panics otherwise. This does **NOT** guarantee that the resulting
169+
/// reference is safe to use.
170+
///
171+
/// # Panics
172+
///
173+
/// Panics if the given id refers to a destroyed object. For a non-panicking version, see
174+
/// `try_from_instance_id`.
175+
///
176+
/// # Safety
177+
///
178+
/// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
179+
/// have exclusive access to the underlying object, if it is still alive.
180+
#[inline]
181+
unsafe fn from_instance_id<'a>(id: i64) -> TRef<'a, Self, Shared> {
182+
TRef::from_instance_id(id)
183+
}
154184
}
155185

156186
/// Marker trait for API types that are subclasses of another type. This trait is implemented
@@ -892,6 +922,41 @@ where
892922
}
893923
}
894924

925+
impl<'a, T: GodotObject> TRef<'a, T, Shared> {
926+
/// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
927+
/// still alive.
928+
///
929+
/// # Safety
930+
///
931+
/// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
932+
/// have exclusive access to the underlying object, if it is still alive.
933+
#[inline]
934+
pub unsafe fn try_from_instance_id(id: i64) -> Option<Self> {
935+
let api = get_api();
936+
let ptr = NonNull::new((api.godot_instance_from_id)(id as sys::godot_int))?;
937+
let raw = RawObject::try_from_sys_ref(ptr)?;
938+
Some(TRef::new(T::cast_ref(raw)))
939+
}
940+
941+
/// Recovers a instance ID previously returned by `Object::get_instance_id` if the object is
942+
/// still alive, and panics otherwise. This does **NOT** guarantee that the resulting
943+
/// reference is safe to use.
944+
///
945+
/// # Panics
946+
///
947+
/// Panics if the given id refers to a destroyed object. For a non-panicking version, see
948+
/// `try_from_instance_id`.
949+
///
950+
/// # Safety
951+
///
952+
/// During the entirety of `'a`, the thread from which `try_from_instance_id` is called must
953+
/// have exclusive access to the underlying object, if it is still alive.
954+
#[inline]
955+
pub unsafe fn from_instance_id(id: i64) -> Self {
956+
Self::try_from_instance_id(id).expect("instance should be alive")
957+
}
958+
}
959+
895960
/// Trait for safe conversion from Godot object references into API method arguments. This is
896961
/// a sealed trait with no public interface.
897962
///

gdnative-sys/godot_headers/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2017-2020 GodotNativeTools
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

gdnative-sys/godot_headers/README.md

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
# godot_headers
2+
#### `GDNative / NativeScript`
3+
4+
> `GDNative` enables the use of dynamically linked libraries inside of [**Godot**](https://github.com/godotengine/godot).
5+
6+
> `NativeScript` uses GDNative to implement scripts backed by native code.
7+
8+
- [**Branches**](#branches)
9+
- [**Getting Started**](#getting-started)
10+
- [**FAQ**](#faq)
11+
12+
## Branches
13+
14+
We maintain branches on this repo that relate directly to the main builds of Godot.
15+
Make sure you use the correct branch for the version of Godot you are using!
16+
17+
| Branch | Version of Godot |
18+
| --- | --- |
19+
| [master](https://github.com/GodotNativeTools/godot_headers) | Godot master |
20+
| [3.2](https://github.com/GodotNativeTools/godot_headers/tree/3.2) | Godot 3.2.x |
21+
| [3.1](https://github.com/GodotNativeTools/godot_headers/tree/3.1) | Godot 3.1.x |
22+
| [3.0](https://github.com/GodotNativeTools/godot_headers/tree/3.0) | Godot 3.0.x |
23+
24+
## Getting Started
25+
26+
| **Build latest version of Godot** | [**GitHub**](https://github.com/godotengine/godot) | [**Docs**](https://godot.readthedocs.io/en/latest/development/compiling/index.html) |
27+
| --- | --- | --- |
28+
29+
### Clone godot_headers into Library
30+
31+
Clone `godot_headers` under `SimpleLibrary/`
32+
33+
```bash
34+
cd SimpleLibrary
35+
git clone https://github.com/GodotNativeTools/godot_headers
36+
```
37+
38+
> Note that the master branch of this repository contains the header for the latest Godot master. If you want to build GDNative modules for older versions of Godot add `-b <version>` to the git clone command above. i.e. `git clone https://github.com/GodotNativeTools/godot_headers -b 3.0` will retrieve headers compatible with Godot 3.0.
39+
40+
> With the exception of a breaking change in the ARVR module between 3.0 and 3.1, GDNative plugins written for an older version of Godot will work in newer versions.
41+
42+
```bash
43+
[SimpleLibrary]
44+
├── lib/
45+
└── src/
46+
```
47+
48+
### Create Script
49+
50+
Create `test.c` under `SimpleLibrary/src/`
51+
52+
<details>
53+
54+
```c
55+
#include <gdnative/gdnative.h>
56+
#include <nativescript/godot_nativescript.h>
57+
58+
#include <stdio.h>
59+
60+
void *test_constructor(godot_object *obj, void *method_data) {
61+
printf("test.constructor()\n");
62+
return 0;
63+
}
64+
65+
void test_destructor(godot_object *obj, void *method_data, void *user_data) {
66+
printf("test.destructor()\n");
67+
}
68+
69+
/** func _ready() **/
70+
godot_variant test_ready(godot_object *obj, void *method_data, void *user_data, int num_args, godot_variant **args) {
71+
godot_variant ret;
72+
godot_variant_new_nil(&ret);
73+
74+
printf("_ready()\n");
75+
76+
return ret;
77+
}
78+
79+
/** Library entry point **/
80+
void GDN_EXPORT godot_gdnative_init(godot_gdnative_init_options *o) {
81+
}
82+
83+
/** Library de-initialization **/
84+
void GDN_EXPORT godot_gdnative_terminate(godot_gdnative_terminate_options *o) {
85+
}
86+
87+
/** Script entry (Registering all the classes and stuff) **/
88+
void GDN_EXPORT godot_nativescript_init(void *desc) {
89+
printf("nativescript init\n");
90+
91+
godot_instance_create_func create_func = {
92+
.create_func = &test_constructor,
93+
.method_data = 0,
94+
.free_func = 0
95+
};
96+
97+
godot_instance_destroy_func destroy_func = {
98+
.destroy_func = &test_destructor,
99+
.method_data = 0,
100+
.free_func = 0
101+
};
102+
103+
godot_nativescript_register_class(desc, "SimpleClass", "Node", create_func, destroy_func);
104+
105+
{
106+
godot_instance_method method = {
107+
.method = &test_ready,
108+
.method_data = 0,
109+
.free_func = 0
110+
};
111+
112+
godot_method_attributes attr = {
113+
.rpc_type = GODOT_METHOD_RPC_MODE_DISABLED
114+
};
115+
116+
godot_nativescript_register_method(desc, "SimpleClass", "_ready", attr, method);
117+
}
118+
}
119+
120+
godot_variant GDN_EXPORT some_test_procedure(void *data, godot_array *args) {
121+
godot_variant ret;
122+
godot_variant_new_int(&ret, 42);
123+
124+
godot_string s;
125+
godot_string_new_unicode_data(&s, L"Hello World", 11);
126+
godot_print(&s);
127+
128+
godot_string_destroy(&s);
129+
130+
return ret;
131+
}
132+
```
133+
134+
</details>
135+
136+
`Expand details for example code.`
137+
138+
### Compile Library
139+
140+
On Linux:
141+
142+
```bash
143+
clang -g -fPIC -std=c99 -c src/test.c -I/path/to/godot/headers/ -o src/test.os
144+
clang -g -shared src/test.os -o lib/test.so
145+
```
146+
147+
On MacOS:
148+
149+
```bash
150+
clang -g -fPIC -std=c99 -c src/test.c -I/path/to/godot/headers/ -o src/test.os
151+
clang -g -shared -framework Cocoa -Wl,-undefined,dynamic_lookup src/test.os -o lib/test.dylib
152+
```
153+
154+
- `-g` is for debugging information.
155+
- Use `godot_nativescript_*` methods only in the `nativescript_init()` function.
156+
157+
### Create GDNativeLibrary Resource
158+
The GDNativeLibrary resource contains links to the libraries for each platform.
159+
160+
1. Create a new resource in memory and edit it.
161+
1. Select `Resource > GDNativeLibrary`.
162+
1. Set the library file for your platform inside the inspector.
163+
1. Save the edited resource as a `.tres`
164+
165+
<details>
166+
167+
![](images/faq/dllibrary_create_new_resource.png?raw=true)
168+
169+
![](images/faq/dllibrary_create_new_dllibrary.png?raw=true)
170+
171+
![](images/faq/dllibrary_save_as_resource.png?raw=true)
172+
173+
*Note*: Remember to save `GDNativeLibrary` as `.gdnlib`
174+
175+
</details>
176+
177+
`Expand details for screenshots.`
178+
179+
### Using GDNativeLibrary in GDScript
180+
181+
```gdscript
182+
extends Node
183+
184+
func _ready():
185+
var gdn = GDNative.new()
186+
gdn.library = load("res://lib/libtest.tres")
187+
188+
gdn.initialize()
189+
190+
var res = gdn.call_native("standard_varcall", "some_test_procedure", [])
191+
192+
print("result: ", res)
193+
194+
gdn.terminate()
195+
```
196+
197+
### Attaching GDNativeLibrary to a Node
198+
199+
1. Attach a new script to a node.
200+
1. In the pop-up dialog, choose NativeScript in the `Language` menu.
201+
1. Enable built-in script, or create a `.gdn` file, which only contains a name.
202+
1. Specify the `Class Name`.
203+
1. Press `Create`.
204+
205+
The GDNativeLibrary field in a NativeScript is empty by default.
206+
207+
208+
<details>
209+
210+
![](images/faq/create_dlscript.png?raw=true)
211+
212+
![](images/faq/set_script_dllibrary.png?raw=true)
213+
214+
</details>
215+
216+
`Expand details for screenshots.`
217+
218+
## FAQ
219+
220+
**What is the difference between `GDNative` and `NativeScript`?**
221+
222+
`GDNative` is a new class that can call native functions in libraries.
223+
GDScript / VisualScript / C#, etc, are able to use this class.
224+
225+
Godot treats `NativeScript` as a scripting language, enabling the
226+
use of GDNative to implement scripts backed by native code.
227+
228+
**Which languages are binding as a NativeScript?**
229+
230+
[**C++**](https://github.com/GodotNativeTools/cpp_bindings),
231+
[**D**](https://github.com/GodotNativeTools/d_bindings),
232+
[**Nim**](https://github.com/pragmagic/godot-nim)
233+
234+
**Can you debug NativeScripts?**
235+
236+
You must compile the library with debug
237+
symbols, and then you can use your debugger as usual.
238+
239+
**Can you use one GDNativeLibrary for all NativeScripts?**
240+
241+
You can! ✨
242+
243+
**What is the reason behind the name "GDNative"?**
244+
245+
GDNative was originally named "cscript" because it exposes a C API, but people
246+
mistook a relation to C#, which is sometimes abbreviated as "cs". Then named "DLScript", but that brought up some confusion, so we settled with
247+
GDNative. 📖

gdnative-sys/godot_headers/android/godot_android.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/* GODOT ENGINE */
66
/* https://godotengine.org */
77
/*************************************************************************/
8-
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9-
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
8+
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
9+
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
1010
/* */
1111
/* Permission is hereby granted, free of charge, to any person obtaining */
1212
/* a copy of this software and associated documentation files (the */
@@ -46,6 +46,8 @@ extern "C" {
4646

4747
JNIEnv *GDAPI godot_android_get_env();
4848
jobject GDAPI godot_android_get_activity();
49+
jobject GDAPI godot_android_get_surface();
50+
bool GDAPI godot_android_is_activity_resumed();
4951

5052
#ifdef __cplusplus
5153
}

0 commit comments

Comments
 (0)