Skip to content

Commit 54fbe15

Browse files
authored
fully seperating scripts lib and runtime (#5)
1 parent 669e103 commit 54fbe15

File tree

12 files changed

+141
-113
lines changed

12 files changed

+141
-113
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ edition = "2021"
3232
crate-type = ["cdylib"]
3333

3434
[dependencies]
35-
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master" }
35+
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master", features = ["runtime"] }
3636
scripts = { path = "./scripts" }
3737
```
3838

@@ -88,7 +88,7 @@ edition = "2021"
8888
crate-type = ["dylib", "rlib"]
8989

9090
[dependencies]
91-
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master" }
91+
godot-rust-script = { git = "https://github.com/TitanNano/godot-rust-script.git", branch = "master", features = ["scripts"] }
9292
```
9393

9494
scripts-lib/src/lib.rs

rust-script/Cargo.toml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ edition.workspace = true
77

88
[dependencies]
99
godot.workspace = true
10-
itertools.workspace = true
1110
abi_stable.workspace = true
12-
rand.workspace = true
1311
cfg-if.workspace = true
1412

1513
hot-lib-reloader = { workspace = true, optional = true }
14+
itertools = { workspace = true, optional = true }
15+
rand = { workspace = true, optional = true }
1616
process_path = { workspace = true, optional = true }
1717

18-
godot-rust-script-derive.workspace = true
18+
godot-rust-script-derive = { workspace = true, optional = true }
1919

2020
[dev-dependencies]
2121
tests-scripts-lib = { path = "../tests-scripts-lib", features = ["hot-reload"] }
22+
godot-rust-script = { path = "./", features = ["runtime"] }
2223

2324
[features]
2425
default = ["hot-reload"]
25-
hot-reload = ["dep:process_path", "dep:hot-lib-reloader"]
26+
hot-reload = ["dep:process_path", "dep:hot-lib-reloader"]
27+
runtime = ["dep:itertools", "dep:rand"]
28+
scripts = ["dep:godot-rust-script-derive"]

rust-script/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
mod apply;
2+
mod script_registry;
3+
mod shared;
4+
5+
#[cfg(feature = "scripts")]
26
mod library;
7+
#[cfg(feature = "runtime")]
38
mod runtime;
4-
mod script_registry;
59

10+
#[cfg(feature = "scripts")]
611
pub use library::*;
12+
#[cfg(feature = "runtime")]
713
pub use runtime::*;
814

15+
#[cfg(feature = "scripts")]
916
pub use godot_rust_script_derive::{godot_script_impl, GodotScript};
1017

1118
pub mod private_export {
12-
pub use super::script_registry::RemoteVariantType;
19+
pub use super::{script_registry::RemoteVariantType, shared::BindingInit};
1320
pub use abi_stable::std_types::{RStr, RString, RVec};
1421
pub use godot::sys::{plugin_add, plugin_registry};
1522

rust-script/src/library.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ macro_rules! setup_library {
5757
$crate::private_export::plugin_registry!(pub __SCRIPT_REGISTRY: $crate::RegistryItem);
5858

5959
#[no_mangle]
60-
pub fn __godot_rust_script_init(binding: Option<$crate::BindingInit>) -> $crate::private_export::RVec<$crate::RemoteScriptMetaData> {
60+
pub fn __godot_rust_script_init(binding: Option<$crate::private_export::BindingInit>) -> $crate::private_export::RVec<$crate::RemoteScriptMetaData> {
6161
use $crate::private_export::*;
6262
use $crate::godot::obj::EngineEnum;
6363

rust-script/src/runtime/hot_reloader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use hot_lib_reloader::LibReloadObserver;
1313
use crate::{
1414
apply::Apply,
1515
script_registry::{RemoteGodotScript_TO, RemoteValueRef},
16-
RustScriptLibInit,
16+
shared::RustScriptLibInit,
1717
};
1818

1919
use super::{rust_script::RustScript, HOT_RELOAD_BRIDGE};

rust-script/src/runtime/metadata.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use std::rc::Rc;
2+
3+
use abi_stable::std_types::RBox;
4+
use godot::{
5+
obj::EngineEnum,
6+
prelude::{
7+
meta::{ClassName, MethodInfo, PropertyInfo},
8+
Array, Dictionary, Gd, Object, StringName, ToGodot,
9+
},
10+
};
11+
12+
use crate::{
13+
apply::Apply,
14+
script_registry::{CreateScriptInstanceData_TO, RemoteGodotScript_TO},
15+
RemoteScriptMetaData,
16+
};
17+
18+
#[derive(Debug)]
19+
pub struct ScriptMetaData {
20+
class_name: ClassName,
21+
base_type_name: StringName,
22+
properties: Rc<Vec<PropertyInfo>>,
23+
methods: Rc<Vec<MethodInfo>>,
24+
create_data: CreateScriptInstanceData_TO<'static, RBox<()>>,
25+
}
26+
27+
impl ScriptMetaData {
28+
pub fn class_name(&self) -> ClassName {
29+
self.class_name
30+
}
31+
32+
pub fn base_type_name(&self) -> StringName {
33+
self.base_type_name.clone()
34+
}
35+
36+
pub fn create_data(&self, base: Gd<Object>) -> RemoteGodotScript_TO<'static, RBox<()>> {
37+
self.create_data.create(base.to_variant().into())
38+
}
39+
40+
pub fn properties(&self) -> Rc<Vec<PropertyInfo>> {
41+
self.properties.clone()
42+
}
43+
44+
pub fn methods(&self) -> Rc<Vec<MethodInfo>> {
45+
self.methods.clone()
46+
}
47+
}
48+
49+
impl From<RemoteScriptMetaData> for ScriptMetaData {
50+
fn from(value: RemoteScriptMetaData) -> Self {
51+
Self {
52+
class_name: ClassName::from_ascii_cstr(value.class_name.as_str().as_bytes()),
53+
base_type_name: StringName::from(value.base_type_name.as_str()),
54+
properties: Rc::new(value.properties.into_iter().map(Into::into).collect()),
55+
methods: Rc::new(value.methods.into_iter().map(Into::into).collect()),
56+
create_data: value.create_data,
57+
}
58+
}
59+
}
60+
61+
pub(super) trait ToDictionary {
62+
fn to_dict(&self) -> Dictionary;
63+
}
64+
65+
impl ToDictionary for PropertyInfo {
66+
fn to_dict(&self) -> Dictionary {
67+
let mut dict = Dictionary::new();
68+
69+
dict.set("name", self.property_name.clone());
70+
dict.set("class_name", self.class_name.to_string_name());
71+
dict.set("type", self.variant_type as i32);
72+
dict.set("hint", self.hint.ord());
73+
dict.set("hint_string", self.hint_string.clone());
74+
dict.set("usage", self.usage.ord());
75+
76+
dict
77+
}
78+
}
79+
80+
impl ToDictionary for MethodInfo {
81+
fn to_dict(&self) -> Dictionary {
82+
Dictionary::new().apply(|dict| {
83+
dict.set("name", self.method_name.clone());
84+
dict.set("flags", self.flags.ord());
85+
86+
let args: Array<_> = self.arguments.iter().map(|arg| arg.to_dict()).collect();
87+
88+
dict.set("args", args);
89+
90+
dict.set("return", self.return_type.to_dict());
91+
})
92+
}
93+
}

rust-script/src/runtime/mod.rs

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[cfg(all(feature = "hot-reload", debug_assertions))]
22
mod hot_reloader;
3+
mod metadata;
34
mod resource_loader;
45
mod resource_saver;
56
mod rust_script;
@@ -8,22 +9,18 @@ mod rust_script_language;
89

910
use std::{collections::HashMap, rc::Rc, sync::RwLock};
1011

11-
use abi_stable::std_types::RVec;
1212
use cfg_if::cfg_if;
1313
use godot::{
1414
engine::{Engine, ResourceLoader, ResourceSaver},
15-
obj::EngineEnum,
16-
prelude::{
17-
godot_print,
18-
meta::{MethodInfo, PropertyInfo},
19-
Array, Dictionary, Gd,
20-
},
15+
prelude::{godot_print, Gd},
2116
};
2217

2318
use crate::{
24-
apply::Apply,
25-
runtime::{resource_loader::RustScriptResourceLoader, resource_saver::RustScriptResourceSaver},
26-
script_registry::{RemoteScriptMetaData, ScriptMetaData},
19+
runtime::{
20+
metadata::ScriptMetaData, resource_loader::RustScriptResourceLoader,
21+
resource_saver::RustScriptResourceSaver,
22+
},
23+
shared::RustScriptLibInit,
2724
};
2825

2926
use self::rust_script_language::RustScriptLanguage;
@@ -43,7 +40,7 @@ macro_rules! setup {
4340
#[hot_functions]
4441
extern "Rust" {
4542
pub fn __godot_rust_script_init(
46-
binding: Option<$crate::BindingInit>,
43+
binding: Option<$crate::private_export::BindingInit>,
4744
) -> RVec<$crate::RemoteScriptMetaData>;
4845
}
4946

@@ -79,12 +76,6 @@ thread_local! {
7976
static HOT_RELOAD_BRIDGE: std::cell::RefCell<HashMap<rust_script_instance::RustScriptInstanceId, std::cell::RefCell<HotReloadEntry>>> = std::cell::RefCell::default();
8077
}
8178

82-
pub type BindingInit = godot::sys::GodotBinding;
83-
84-
pub trait RustScriptLibInit: Fn(Option<BindingInit>) -> RVec<RemoteScriptMetaData> {}
85-
86-
impl<F> RustScriptLibInit for F where F: Fn(Option<BindingInit>) -> RVec<RemoteScriptMetaData> {}
87-
8879
cfg_if! {
8980
if #[cfg(all(feature = "hot-reload", debug_assertions))] {
9081
type HotReloadSubscribe = fn() -> hot_lib_reloader::LibReloadObserver;
@@ -206,37 +197,3 @@ fn load_rust_scripts(lib_init_fn: Rc<dyn RustScriptLibInit>) {
206197
*reg = registry;
207198
});
208199
}
209-
210-
trait ToDictionary {
211-
fn to_dict(&self) -> Dictionary;
212-
}
213-
214-
impl ToDictionary for PropertyInfo {
215-
fn to_dict(&self) -> Dictionary {
216-
let mut dict = Dictionary::new();
217-
218-
dict.set("name", self.property_name.clone());
219-
dict.set("class_name", self.class_name.to_string_name());
220-
dict.set("type", self.variant_type as i32);
221-
dict.set("hint", self.hint.ord());
222-
dict.set("hint_string", self.hint_string.clone());
223-
dict.set("usage", self.usage.ord());
224-
225-
dict
226-
}
227-
}
228-
229-
impl ToDictionary for MethodInfo {
230-
fn to_dict(&self) -> Dictionary {
231-
Dictionary::new().apply(|dict| {
232-
dict.set("name", self.method_name.clone());
233-
dict.set("flags", self.flags.ord());
234-
235-
let args: Array<_> = self.arguments.iter().map(|arg| arg.to_dict()).collect();
236-
237-
dict.set("args", args);
238-
239-
dict.set("return", self.return_type.to_dict());
240-
})
241-
}
242-
}

rust-script/src/runtime/rust_script.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ use RemoteGodotScript_trait::RemoteGodotScript_TO;
1313
use crate::script_registry::RemoteGodotScript_trait;
1414

1515
use super::{
16+
metadata::ToDictionary,
1617
rust_script_instance::{RustScriptInstance, RustScriptPlaceholder},
1718
rust_script_language::RustScriptLanguage,
18-
ToDictionary, SCRIPT_REGISTRY,
19+
SCRIPT_REGISTRY,
1920
};
2021

2122
#[derive(GodotClass)]

rust-script/src/script_registry.rs

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, fmt::Debug, marker::PhantomData, rc::Rc};
1+
use std::{collections::HashMap, fmt::Debug, marker::PhantomData};
22

33
use abi_stable::{
44
sabi_trait::TD_CanDowncast,
@@ -11,7 +11,7 @@ use godot::{
1111
prelude::{
1212
godot_print,
1313
meta::{ClassName, MethodInfo, PropertyInfo},
14-
Gd, Object, StringName, ToGodot, Variant,
14+
Gd, Object, StringName, Variant,
1515
},
1616
sys::VariantType,
1717
};
@@ -147,11 +147,11 @@ impl From<RemoteScriptMethodInfo> for MethodInfo {
147147
#[derive(Debug, StableAbi)]
148148
#[repr(C)]
149149
pub struct RemoteScriptMetaData {
150-
class_name: RStr<'static>,
151-
base_type_name: RStr<'static>,
152-
properties: RVec<RemoteScriptPropertyInfo>,
153-
methods: RVec<RemoteScriptMethodInfo>,
154-
create_data: CreateScriptInstanceData_TO<'static, RBox<()>>,
150+
pub(crate) class_name: RStr<'static>,
151+
pub(crate) base_type_name: RStr<'static>,
152+
pub(crate) properties: RVec<RemoteScriptPropertyInfo>,
153+
pub(crate) methods: RVec<RemoteScriptMethodInfo>,
154+
pub(crate) create_data: CreateScriptInstanceData_TO<'static, RBox<()>>,
155155
}
156156

157157
impl RemoteScriptMetaData {
@@ -175,49 +175,6 @@ impl RemoteScriptMetaData {
175175
}
176176
}
177177

178-
#[derive(Debug)]
179-
pub struct ScriptMetaData {
180-
class_name: ClassName,
181-
base_type_name: StringName,
182-
properties: Rc<Vec<PropertyInfo>>,
183-
methods: Rc<Vec<MethodInfo>>,
184-
create_data: CreateScriptInstanceData_TO<'static, RBox<()>>,
185-
}
186-
187-
impl ScriptMetaData {
188-
pub fn class_name(&self) -> ClassName {
189-
self.class_name
190-
}
191-
192-
pub fn base_type_name(&self) -> StringName {
193-
self.base_type_name.clone()
194-
}
195-
196-
pub fn create_data(&self, base: Gd<Object>) -> RemoteGodotScript_TO<'static, RBox<()>> {
197-
self.create_data.create(base.to_variant().into())
198-
}
199-
200-
pub fn properties(&self) -> Rc<Vec<PropertyInfo>> {
201-
self.properties.clone()
202-
}
203-
204-
pub fn methods(&self) -> Rc<Vec<MethodInfo>> {
205-
self.methods.clone()
206-
}
207-
}
208-
209-
impl From<RemoteScriptMetaData> for ScriptMetaData {
210-
fn from(value: RemoteScriptMetaData) -> Self {
211-
Self {
212-
class_name: ClassName::from_ascii_cstr(value.class_name.as_str().as_bytes()),
213-
base_type_name: StringName::from(value.base_type_name.as_str()),
214-
properties: Rc::new(value.properties.into_iter().map(Into::into).collect()),
215-
methods: Rc::new(value.methods.into_iter().map(Into::into).collect()),
216-
create_data: value.create_data,
217-
}
218-
}
219-
}
220-
221178
#[abi_stable::sabi_trait]
222179
pub trait CreateScriptInstanceData: Debug {
223180
fn create(&self, base: RemoteValue) -> RemoteGodotScript_TO<'static, RBox<()>>;

0 commit comments

Comments
 (0)