Skip to content

Commit e319200

Browse files
authored
Update gdext (#36)
1 parent e06373c commit e319200

File tree

11 files changed

+108
-102
lines changed

11 files changed

+108
-102
lines changed

Cargo.lock

Lines changed: 16 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ version = "0.1.0"
1010
edition = "2021"
1111

1212
[workspace.dependencies]
13-
godot = { git = "https://github.com/godot-rust/gdext", rev = "7fd1be904efccd71a9eb980c2e6fffa2d8705ec4" }
13+
godot = { git = "https://github.com/godot-rust/gdext", rev = "22fd33d4d5213a3fe5db9a58547888cebe35c647" }
1414
itertools = "0.10.3"
1515
rand = "0.8.5"
1616
darling = { version = "0.20.3" }

license_header.nu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ def lines []: string -> list<string> {
2020
}
2121

2222
def main []: nothing -> nothing {
23-
for file in (ls $"($source_root)/**/*.rs") {
24-
let current_header = read_file $file.name | first $license_length
23+
for file in (glob $"($source_root)/**/*.rs") {
24+
let current_header = read_file $file | first $license_length
2525

2626
if $current_header == $license_notice {
2727
continue
2828
}
2929

30-
read_file $file.name | prepend $license_notice | str join "\n" | save -f $file.name
30+
read_file $file | prepend $license_notice | str join "\n" | save -f $file.name
3131
}
3232
}

rust-script/src/runtime/downgrade_self.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
55
*/
66

7-
use std::ops::Deref;
8-
97
use godot::{
108
builtin::meta::ToGodot,
119
obj::{Gd, GodotClass, Inherits, WithBaseField},
1210
};
1311

1412
pub trait DowngradeSelf: GodotClass {
15-
fn downgrade<F: FnOnce(&Self) -> R, R>(&mut self, closure: F) -> R;
1613
fn downgrade_gd<F: FnOnce(Gd<Self>) -> R, R>(&mut self, closure: F) -> R;
1714
}
1815

@@ -21,14 +18,6 @@ where
2118
T: WithBaseField + GodotClass,
2219
T: Inherits<<T as GodotClass>::Base>,
2320
{
24-
fn downgrade<F: FnOnce(&Self) -> R, R>(&mut self, closure: F) -> R {
25-
let mut_base = self.base_mut();
26-
let self_gd: Gd<T> = mut_base.to_godot().cast();
27-
let slf = self_gd.bind();
28-
29-
closure(slf.deref())
30-
}
31-
3221
fn downgrade_gd<F: FnOnce(Gd<Self>) -> R, R>(&mut self, closure: F) -> R {
3322
let mut_base = self.base_mut();
3423
let self_gd = mut_base.to_godot().cast();

rust-script/src/runtime/metadata.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
use std::ops::Deref;
88

99
use godot::{
10+
builtin::meta::{MethodInfo, PropertyInfo},
1011
obj::{EngineBitfield, EngineEnum},
11-
prelude::{
12-
meta::{MethodInfo, PropertyInfo},
13-
Array, Dictionary,
14-
},
12+
prelude::{Array, Dictionary},
1513
sys::VariantType,
1614
};
1715

rust-script/src/runtime/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ mod rust_script_language;
1515
use std::{collections::HashMap, sync::RwLock};
1616

1717
use godot::{
18-
bind::GodotClass,
1918
engine::{Engine, RefCounted, ResourceLoader, ResourceSaver, ScriptLanguage},
2019
log::godot_warn,
2120
obj::GodotClass,
2221
prelude::{godot_print, Gd},
22+
register::GodotClass,
2323
};
2424
use once_cell::sync::Lazy;
2525

rust-script/src/runtime/rust_script.rs

Lines changed: 46 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
use std::{cell::RefCell, collections::HashSet, ffi::c_void};
88

99
use godot::{
10-
builtin::meta::{MethodInfo, PropertyInfo, ToGodot},
10+
builtin::{
11+
meta::{MethodInfo, PropertyInfo, ToGodot},
12+
Callable,
13+
},
1114
engine::{
12-
create_script_instance, notify::ObjectNotification, ClassDb, Engine, IScriptExtension,
13-
Script, ScriptExtension, ScriptInstance, ScriptLanguage, WeakRef,
15+
create_script_instance, notify::ObjectNotification, object::ConnectFlags, ClassDb, Engine,
16+
IScriptExtension, Script, ScriptExtension, ScriptLanguage, WeakRef,
1417
},
1518
log::{godot_error, godot_print, godot_warn},
16-
obj::{InstanceId, WithBaseField},
19+
obj::{EngineEnum, InstanceId, WithBaseField},
1720
prelude::{
1821
godot_api, Array, Base, Dictionary, GString, Gd, GodotClass, Object, StringName, Variant,
1922
VariantArray,
@@ -35,18 +38,17 @@ const NOTIFICATION_EXTENSION_RELOADED: i32 = 2;
3538
#[derive(GodotClass)]
3639
#[class(base = ScriptExtension, tool)]
3740
pub(super) struct RustScript {
38-
#[var(get = get_class_name, set = set_class_name, usage_flags = [PROPERTY_USAGE_STORAGE])]
41+
#[var(get = get_class_name, set = set_class_name, usage_flags = [STORAGE])]
3942
class_name: GString,
4043

41-
#[var(usage_flags = [PROPERTY_USAGE_STORAGE])]
44+
#[var(usage_flags = [STORAGE])]
4245
source_code: GString,
4346

44-
#[var( get = owner_ids, set = set_owner_ids, usage_flags = [PROPERTY_USAGE_STORAGE])]
47+
#[var( get = owner_ids, set = set_owner_ids, usage_flags = [STORAGE])]
4548
#[allow(dead_code)]
4649
owner_ids: Array<i64>,
4750

4851
owners: RefCell<Vec<Gd<WeakRef>>>,
49-
#[base]
5052
base: Base<ScriptExtension>,
5153
}
5254

@@ -121,29 +123,28 @@ impl RustScript {
121123
.collect();
122124
}
123125

124-
fn init_script_instance(instance: &mut RustScriptInstance) {
125-
match instance.call(StringName::from("_init"), &[]) {
126-
Ok(_) => (),
127-
Err(err) => {
128-
use godot::sys::*;
129-
130-
if !matches!(
131-
err,
132-
GDEXTENSION_CALL_OK | GDEXTENSION_CALL_ERROR_INVALID_METHOD
133-
) {
134-
let error_code = match err {
135-
GDEXTENSION_CALL_ERROR_INSTANCE_IS_NULL => "INSTANCE_IS_NULL",
136-
GDEXTENSION_CALL_ERROR_INVALID_ARGUMENT => "INVALID_ARGUMENT",
137-
GDEXTENSION_CALL_ERROR_METHOD_NOT_CONST => "METHOD_NOT_CONST",
138-
GDEXTENSION_CALL_ERROR_TOO_FEW_ARGUMENTS => "TOO_FEW_ARGUMENTS",
139-
GDEXTENSION_CALL_ERROR_TOO_MANY_ARGUMENTS => "TOO_MANY_ARGUMENTS",
140-
_ => "UNKNOWN",
141-
};
142-
143-
godot_error!("failed to call rust script _init fn: {}", error_code);
144-
}
145-
}
126+
#[func]
127+
fn init_script_instance(base: Variant) {
128+
let mut base: Gd<Object> = match base.try_to() {
129+
Ok(base) => base,
130+
Err(err) => panic!(
131+
"init_rust_script_instance was called without base object bind!\n{}",
132+
err
133+
),
146134
};
135+
136+
if let Err(err) = base.get_script().try_to::<Gd<RustScript>>() {
137+
godot_warn!("expected new script to be previously assigned RustScript, but it wasn't!");
138+
godot_warn!("{}", err);
139+
140+
return;
141+
}
142+
143+
if !base.has_method("_init".into()) {
144+
return;
145+
}
146+
147+
base.call(StringName::from("_init"), &[]);
147148
}
148149
}
149150

@@ -194,16 +195,26 @@ impl IScriptExtension for RustScript {
194195
false
195196
}
196197

197-
unsafe fn instance_create(&self, for_object: Gd<Object>) -> *mut c_void {
198+
unsafe fn instance_create(&self, mut for_object: Gd<Object>) -> *mut c_void {
198199
self.owners
199200
.borrow_mut()
200201
.push(godot::engine::utilities::weakref(for_object.to_variant()).to());
201202

202203
let data = self.create_remote_instance(for_object.clone());
203-
let mut instance = RustScriptInstance::new(data, for_object, self.to_gd());
204+
let instance = RustScriptInstance::new(data, for_object.clone(), self.to_gd());
205+
206+
let callbale_args = VariantArray::from(&[for_object.to_variant()]);
207+
208+
for_object
209+
.connect_ex(
210+
StringName::from("script_changed"),
211+
Callable::from_object_method(&self.to_gd(), "init_script_instance")
212+
.bindv(callbale_args),
213+
)
214+
.flags(ConnectFlags::ONE_SHOT.ord() as u32)
215+
.done();
204216

205-
Self::init_script_instance(&mut instance);
206-
create_script_instance(instance)
217+
create_script_instance(instance, for_object)
207218
}
208219

209220
unsafe fn placeholder_instance_create(&self, for_object: Gd<Object>) -> *mut c_void {
@@ -213,7 +224,7 @@ impl IScriptExtension for RustScript {
213224

214225
let placeholder = RustScriptPlaceholder::new(self.to_gd());
215226

216-
create_script_instance(placeholder)
227+
create_script_instance(placeholder, for_object)
217228
}
218229

219230
fn is_valid(&self) -> bool {

0 commit comments

Comments
 (0)