Skip to content

Commit c1d0e96

Browse files
authored
Filter out invalid rust modules from being used as scripts (#25)
1 parent b06f02f commit c1d0e96

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

rust-script/src/runtime/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
},
2525
};
2626

27-
#[derive(Debug)]
27+
#[derive(Debug, Clone)]
2828
pub struct ScriptMetaData {
2929
class_name: ClassName,
3030
base_type_name: StringName,

rust-script/src/runtime/resource_loader.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ impl IResourceFormatLoader for RustScriptResourceLoader {
6262
return GString::new();
6363
}
6464

65+
if script_lang.validate_path(path).is_empty() {
66+
return GString::new();
67+
}
68+
6569
script_lang.get_type()
6670
}
6771

rust-script/src/runtime/rust_script_language.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use itertools::Itertools;
1818

1919
use crate::apply::Apply;
2020

21-
use super::rust_script::RustScript;
21+
use super::{metadata::ScriptMetaData, rust_script::RustScript, SCRIPT_REGISTRY};
2222

2323
#[derive(GodotClass)]
2424
#[class(base = ScriptLanguageExtension, tool)]
@@ -57,6 +57,14 @@ impl RustScriptLanguage {
5757
.get_singleton(RustScriptLanguage::class_name().to_string_name())
5858
.map(|gd| gd.cast())
5959
}
60+
61+
pub fn script_meta_data(class_name: &str) -> Option<ScriptMetaData> {
62+
let reg = SCRIPT_REGISTRY
63+
.read()
64+
.expect("unable to obtain read access");
65+
66+
reg.get(class_name).map(ToOwned::to_owned)
67+
}
6068
}
6169

6270
#[godot_api]
@@ -141,10 +149,14 @@ impl IScriptLanguageExtension for RustScriptLanguage {
141149
return GString::from("rust file is not part of the scripts crate!");
142150
}
143151

144-
if !FileAccess::file_exists(path) {
152+
if !FileAccess::file_exists(path.clone()) {
145153
return GString::from("RustScripts can not be created via the Godot editor!");
146154
}
147155

156+
if !self.get_global_class_name(path).contains_key("name") {
157+
return GString::from("Rust script has not been complied into shared library yet!");
158+
}
159+
148160
GString::new()
149161
}
150162

@@ -168,7 +180,14 @@ impl IScriptLanguageExtension for RustScriptLanguage {
168180
fn get_global_class_name(&self, path: GString) -> Dictionary {
169181
let class_name = Self::path_to_class_name(&path);
170182

171-
Dictionary::new().apply(|dict| dict.set("name", class_name))
183+
let Some(script) = Self::script_meta_data(&class_name) else {
184+
return Dictionary::new();
185+
};
186+
187+
Dictionary::new().apply(|dict| {
188+
dict.set("name", class_name);
189+
dict.set("base_type", script.base_type_name());
190+
})
172191
}
173192

174193
fn overrides_external_editor(&mut self) -> bool {

rust-script/src/script_registry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ impl RemoteScriptMetaData {
182182
}
183183

184184
#[abi_stable::sabi_trait]
185-
pub trait CreateScriptInstanceData: Debug + Sync + Send {
185+
pub trait CreateScriptInstanceData: Debug + Sync + Send + Clone {
186186
fn create(&self, base: RemoteValue) -> RemoteGodotScript_TO<'static, RBox<()>>;
187187
}
188188

189189
impl<F> CreateScriptInstanceData for F
190190
where
191-
F: Fn(Gd<Object>) -> RemoteGodotScript_TO<'static, RBox<()>> + Debug + Sync + Send,
191+
F: Fn(Gd<Object>) -> RemoteGodotScript_TO<'static, RBox<()>> + Debug + Sync + Send + Clone,
192192
{
193193
fn create(&self, base: RemoteValue) -> RemoteGodotScript_TO<'static, RBox<()>> {
194194
let variant: Variant = base.into();

0 commit comments

Comments
 (0)