Skip to content

Commit b06f02f

Browse files
authored
Upgrade gdext (#24)
1 parent a2a5c01 commit b06f02f

File tree

7 files changed

+82
-26
lines changed

7 files changed

+82
-26
lines changed

Cargo.lock

Lines changed: 13 additions & 7 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/titannano/gdext", rev = "4ce4714" }
13+
godot = { git = "https://github.com/titannano/gdext", rev = "e3644a0348b4d6fe952007cebd94d1d3f5ddfd86" }
1414
itertools = "0.10.3"
1515
abi_stable = { version = "0.11.2", default-features = false }
1616
rand = "0.8.5"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
5+
*/
6+
7+
use std::ops::Deref;
8+
9+
use godot::{
10+
builtin::meta::ToGodot,
11+
obj::{Gd, GodotClass, Inherits, WithBaseField},
12+
};
13+
14+
pub trait DowngradeSelf: GodotClass {
15+
fn downgrade<F: FnOnce(&Self) -> R, R>(&mut self, closure: F) -> R;
16+
fn downgrade_gd<F: FnOnce(Gd<Self>) -> R, R>(&mut self, closure: F) -> R;
17+
}
18+
19+
impl<T> DowngradeSelf for T
20+
where
21+
T: WithBaseField + GodotClass,
22+
T: Inherits<<T as GodotClass>::Base>,
23+
{
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+
32+
fn downgrade_gd<F: FnOnce(Gd<Self>) -> R, R>(&mut self, closure: F) -> R {
33+
let mut_base = self.base_mut();
34+
let self_gd = mut_base.to_godot().cast();
35+
36+
closure(self_gd)
37+
}
38+
}

rust-script/src/runtime/mod.rs

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

7+
mod downgrade_self;
78
mod metadata;
89
mod resource_loader;
910
mod resource_saver;

rust-script/src/runtime/rust_script.rs

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

7-
use std::{cell::RefCell, collections::HashSet, ffi::c_void, ops::Deref};
7+
use std::{cell::RefCell, collections::HashSet, ffi::c_void};
88

99
use abi_stable::std_types::RBox;
1010
use godot::{
@@ -14,7 +14,7 @@ use godot::{
1414
Script, ScriptExtension, ScriptInstance, ScriptLanguage, WeakRef,
1515
},
1616
log::{godot_error, godot_print, godot_warn},
17-
obj::{InstanceId, UserClass},
17+
obj::{InstanceId, WithBaseField},
1818
prelude::{
1919
godot_api, Array, Base, Dictionary, GString, Gd, GodotClass, Object, StringName, Variant,
2020
VariantArray,
@@ -25,6 +25,7 @@ use RemoteGodotScript_trait::RemoteGodotScript_TO;
2525
use crate::{apply::Apply, script_registry::RemoteGodotScript_trait};
2626

2727
use super::{
28+
downgrade_self::DowngradeSelf,
2829
metadata::{Documented, ToDictionary, ToMethodDoc, ToPropertyDoc},
2930
rust_script_instance::{RustScriptInstance, RustScriptPlaceholder},
3031
rust_script_language::RustScriptLanguage,
@@ -47,6 +48,7 @@ pub(super) struct RustScript {
4748
owner_ids: Array<i64>,
4849

4950
owners: RefCell<Vec<Gd<WeakRef>>>,
51+
#[base]
5052
base: Base<ScriptExtension>,
5153
}
5254

@@ -174,7 +176,7 @@ impl IScriptExtension for RustScript {
174176
}
175177

176178
fn get_language(&self) -> Option<Gd<ScriptLanguage>> {
177-
Some(RustScriptLanguage::alloc_gd().upcast())
179+
RustScriptLanguage::singleton().map(Gd::upcast)
178180
}
179181

180182
fn can_instantiate(&self) -> bool {
@@ -203,8 +205,7 @@ impl IScriptExtension for RustScript {
203205
.push(godot::engine::utilities::weakref(for_object.to_variant()).to());
204206

205207
let data = self.create_remote_instance(for_object.clone());
206-
let mut instance =
207-
RustScriptInstance::new(data, for_object, self.base.deref().clone().cast());
208+
let mut instance = RustScriptInstance::new(data, for_object, self.to_gd());
208209

209210
Self::init_script_instance(&mut instance);
210211
create_script_instance(instance)
@@ -215,7 +216,7 @@ impl IScriptExtension for RustScript {
215216
.borrow_mut()
216217
.push(godot::engine::utilities::weakref(for_object.to_variant()).to());
217218

218-
let placeholder = RustScriptPlaceholder::new(self.base.deref().clone().cast());
219+
let placeholder = RustScriptPlaceholder::new(self.to_gd());
219220

220221
create_script_instance(placeholder)
221222
}
@@ -338,7 +339,7 @@ impl IScriptExtension for RustScript {
338339
dict.set(GString::from("is_deprecated"), false);
339340
dict.set(GString::from("is_experimental"), false);
340341
dict.set(GString::from("is_script_doc"), true);
341-
dict.set(GString::from("script_path"), self.base.get_path());
342+
dict.set(GString::from("script_path"), self.base().get_path());
342343
});
343344

344345
Array::from(&[class_doc])
@@ -350,7 +351,9 @@ impl IScriptExtension for RustScript {
350351

351352
// godot script reload hook
352353
fn reload(&mut self, _keep_state: bool) -> godot::engine::global::Error {
353-
self.owners.borrow().iter().for_each(|owner| {
354+
let owners = self.owners.borrow().clone();
355+
356+
owners.iter().for_each(|owner| {
354357
let mut object: Gd<Object> = match owner.get_ref().try_to() {
355358
Ok(owner) => owner,
356359
Err(err) => {
@@ -359,14 +362,13 @@ impl IScriptExtension for RustScript {
359362
}
360363
};
361364

362-
let script = object.get_script();
363-
364365
// clear script to destroy script instance.
365366
object.set_script(Variant::nil());
366367

367-
// re-assign script to create new instance.
368-
// call is defered because this will call back into can_instantiate.
369-
object.call_deferred(StringName::from("set_script"), &[script]);
368+
self.downgrade_gd(|self_gd| {
369+
// re-assign script to create new instance.
370+
object.set_script(self_gd.to_variant());
371+
})
370372
});
371373

372374
godot::engine::global::Error::OK

rust-script/src/runtime/rust_script_instance.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use std::{collections::HashMap, rc::Rc};
1111
use abi_stable::std_types::{RBox, RString};
1212
use godot::{
1313
engine::{Script, ScriptInstance},
14-
obj::UserClass,
1514
prelude::{
1615
meta::{MethodInfo, PropertyInfo},
1716
GString, Gd, Object, StringName, Variant, VariantType,
@@ -192,7 +191,9 @@ impl ScriptInstance for RustScriptInstance {
192191
}
193192

194193
fn get_language(&self) -> Gd<godot::engine::ScriptLanguage> {
195-
RustScriptLanguage::alloc_gd().upcast()
194+
RustScriptLanguage::singleton()
195+
.map(Gd::upcast)
196+
.expect("RustScriptLanguage singleton is not initialized")
196197
}
197198

198199
fn on_refcount_decremented(&self) -> bool {
@@ -303,7 +304,9 @@ impl ScriptInstance for RustScriptPlaceholder {
303304
}
304305

305306
fn get_language(&self) -> Gd<godot::engine::ScriptLanguage> {
306-
RustScriptLanguage::alloc_gd().upcast()
307+
RustScriptLanguage::singleton()
308+
.map(Gd::upcast)
309+
.expect("RustScriptLanguage singleton is not initialized")
307310
}
308311

309312
fn on_refcount_decremented(&self) -> bool {

rust-script/src/runtime/rust_script_language.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use std::ffi::OsStr;
88

99
use godot::{
10-
engine::{FileAccess, IScriptLanguageExtension, ProjectSettings, Script},
10+
engine::{Engine, FileAccess, IScriptLanguageExtension, ProjectSettings, Script},
1111
obj::Base,
1212
prelude::{
1313
godot_api, Array, Dictionary, GString, Gd, GodotClass, Object, PackedStringArray,
@@ -51,6 +51,12 @@ impl RustScriptLanguage {
5151
})
5252
.join("")
5353
}
54+
55+
pub fn singleton() -> Option<Gd<Self>> {
56+
Engine::singleton()
57+
.get_singleton(RustScriptLanguage::class_name().to_string_name())
58+
.map(|gd| gd.cast())
59+
}
5460
}
5561

5662
#[godot_api]
@@ -90,7 +96,7 @@ impl IScriptLanguageExtension for RustScriptLanguage {
9096
}
9197

9298
/// frame hook will be called for each reandered frame
93-
fn frame(&mut self) {}
99+
/// fn frame(&mut self) {}
94100
95101
fn handles_global_class_type(&self, type_: GString) -> bool {
96102
type_ == self.get_type()

0 commit comments

Comments
 (0)