|
| 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 | +mod signals; |
| 8 | + |
| 9 | +use std::marker::PhantomData; |
| 10 | +use std::ops::{Deref, DerefMut}; |
| 11 | +use std::{collections::HashMap, fmt::Debug}; |
| 12 | + |
| 13 | +use godot::obj::Inherits; |
| 14 | +use godot::prelude::{Gd, Object, StringName, Variant}; |
| 15 | + |
| 16 | +pub use crate::runtime::Context; |
| 17 | + |
| 18 | +pub use signals::{ScriptSignal, Signal}; |
| 19 | + |
| 20 | +pub trait GodotScript: Debug + GodotScriptImpl<ImplBase = Self::Base> { |
| 21 | + type Base: Inherits<Object>; |
| 22 | + |
| 23 | + const CLASS_NAME: &'static str; |
| 24 | + |
| 25 | + fn set(&mut self, name: StringName, value: Variant) -> bool; |
| 26 | + fn get(&self, name: StringName) -> Option<Variant>; |
| 27 | + fn call( |
| 28 | + &mut self, |
| 29 | + method: StringName, |
| 30 | + args: &[&Variant], |
| 31 | + context: Context<'_, Self>, |
| 32 | + ) -> Result<Variant, godot::sys::GDExtensionCallErrorType>; |
| 33 | + |
| 34 | + fn to_string(&self) -> String; |
| 35 | + fn property_state(&self) -> HashMap<StringName, Variant>; |
| 36 | + |
| 37 | + fn default_with_base(base: godot::prelude::Gd<godot::prelude::Object>) -> Self; |
| 38 | +} |
| 39 | + |
| 40 | +pub trait GodotScriptImpl { |
| 41 | + type ImplBase: Inherits<Object>; |
| 42 | + |
| 43 | + fn call_fn( |
| 44 | + &mut self, |
| 45 | + name: StringName, |
| 46 | + args: &[&Variant], |
| 47 | + context: Context<Self>, |
| 48 | + ) -> Result<Variant, godot::sys::GDExtensionCallErrorType>; |
| 49 | +} |
| 50 | + |
| 51 | +#[derive(Debug)] |
| 52 | +pub struct RsRef<T: GodotScript> { |
| 53 | + owner: Gd<T::Base>, |
| 54 | + script_ty: PhantomData<T>, |
| 55 | +} |
| 56 | + |
| 57 | +impl<T: GodotScript> RsRef<T> { |
| 58 | + pub(crate) fn new<B: Inherits<T::Base> + Inherits<Object>>(owner: Gd<B>) -> Self { |
| 59 | + Self { |
| 60 | + owner: owner.upcast(), |
| 61 | + script_ty: PhantomData, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fn validate_script<O: Inherits<Object>>(owner: &Gd<O>) -> Option<GodotScriptCastError> { |
| 66 | + let script = owner |
| 67 | + .upcast_ref::<Object>() |
| 68 | + .get_script() |
| 69 | + .try_to::<Option<Gd<crate::runtime::RustScript>>>(); |
| 70 | + |
| 71 | + let Ok(script) = script else { |
| 72 | + return Some(GodotScriptCastError::NotRustScript); |
| 73 | + }; |
| 74 | + |
| 75 | + let Some(script) = script else { |
| 76 | + return Some(GodotScriptCastError::NoScriptAttached); |
| 77 | + }; |
| 78 | + |
| 79 | + let class_name = script.bind().str_class_name(); |
| 80 | + |
| 81 | + (class_name != T::CLASS_NAME).then(|| { |
| 82 | + GodotScriptCastError::ClassMismatch(T::CLASS_NAME, script.get_class().to_string()) |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl<T: GodotScript> Deref for RsRef<T> { |
| 88 | + type Target = Gd<T::Base>; |
| 89 | + |
| 90 | + fn deref(&self) -> &Self::Target { |
| 91 | + &self.owner |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl<T: GodotScript> DerefMut for RsRef<T> { |
| 96 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 97 | + &mut self.owner |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<T: GodotScript> Clone for RsRef<T> { |
| 102 | + fn clone(&self) -> Self { |
| 103 | + Self { |
| 104 | + owner: self.owner.clone(), |
| 105 | + script_ty: PhantomData, |
| 106 | + } |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[derive(thiserror::Error, Debug)] |
| 111 | +pub enum GodotScriptCastError { |
| 112 | + #[error("Object has no script attached!")] |
| 113 | + NoScriptAttached, |
| 114 | + |
| 115 | + #[error("Script attached to object is not a RustScript!")] |
| 116 | + NotRustScript, |
| 117 | + |
| 118 | + #[error( |
| 119 | + "Script attached to object does not match expected script class `{0}` but found `{1}`!" |
| 120 | + )] |
| 121 | + ClassMismatch(&'static str, String), |
| 122 | +} |
| 123 | + |
| 124 | +pub trait CastToScript<T: GodotScript> { |
| 125 | + fn try_to_script(&self) -> Result<RsRef<T>, GodotScriptCastError>; |
| 126 | + fn try_into_script(self) -> Result<RsRef<T>, GodotScriptCastError>; |
| 127 | + fn to_script(&self) -> RsRef<T>; |
| 128 | + fn into_script(self) -> RsRef<T>; |
| 129 | +} |
| 130 | + |
| 131 | +impl<T: GodotScript, B: Inherits<T::Base> + Inherits<Object>> CastToScript<T> for Gd<B> { |
| 132 | + fn try_to_script(&self) -> Result<RsRef<T>, GodotScriptCastError> { |
| 133 | + if let Some(err) = RsRef::<T>::validate_script(self) { |
| 134 | + return Err(err); |
| 135 | + } |
| 136 | + |
| 137 | + Ok(RsRef::new(self.clone())) |
| 138 | + } |
| 139 | + |
| 140 | + fn try_into_script(self) -> Result<RsRef<T>, GodotScriptCastError> { |
| 141 | + if let Some(err) = RsRef::<T>::validate_script(&self) { |
| 142 | + return Err(err); |
| 143 | + } |
| 144 | + |
| 145 | + Ok(RsRef::new(self)) |
| 146 | + } |
| 147 | + |
| 148 | + fn to_script(&self) -> RsRef<T> { |
| 149 | + self.try_to_script().unwrap_or_else(|err| { |
| 150 | + panic!( |
| 151 | + "`{}` was assumed to have rust script `{}`, but this was not the case at runtime!\nError: {}", |
| 152 | + B::class_name(), |
| 153 | + T::CLASS_NAME, |
| 154 | + err, |
| 155 | + ); |
| 156 | + }) |
| 157 | + } |
| 158 | + |
| 159 | + fn into_script(self) -> RsRef<T> { |
| 160 | + self.try_into_script().unwrap_or_else(|err| { |
| 161 | + panic!( |
| 162 | + "`{}` was assumed to have rust script `{}`, but this was not the case at runtime!\nError: {}", |
| 163 | + B::class_name(), |
| 164 | + T::CLASS_NAME, |
| 165 | + err |
| 166 | + ); |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +#[macro_export] |
| 172 | +macro_rules! setup_library { |
| 173 | + () => { |
| 174 | + #[no_mangle] |
| 175 | + pub fn __godot_rust_script_init( |
| 176 | + ) -> ::std::vec::Vec<$crate::private_export::RustScriptMetaData> { |
| 177 | + use $crate::godot::obj::EngineEnum; |
| 178 | + use $crate::private_export::*; |
| 179 | + |
| 180 | + let lock = $crate::private_export::__godot_rust_plugin_SCRIPT_REGISTRY |
| 181 | + .lock() |
| 182 | + .expect("unable to aquire mutex lock"); |
| 183 | + |
| 184 | + $crate::private_export::assemble_metadata(lock.iter()) |
| 185 | + } |
| 186 | + |
| 187 | + pub const __GODOT_RUST_SCRIPT_SRC_ROOT: &str = $crate::private_export::concat!( |
| 188 | + env!("CARGO_MANIFEST_DIR"), |
| 189 | + "/src", |
| 190 | + $crate::private_export::replace!( |
| 191 | + $crate::private_export::unwrap!($crate::private_export::strip_prefix!( |
| 192 | + module_path!(), |
| 193 | + $crate::private_export::replace!(env!("CARGO_PKG_NAME"), "-", "_") |
| 194 | + )), |
| 195 | + "::", |
| 196 | + "/" |
| 197 | + ), |
| 198 | + ); |
| 199 | + }; |
| 200 | +} |
| 201 | + |
| 202 | +#[macro_export] |
| 203 | +macro_rules! init { |
| 204 | + ($scripts_module:tt) => { |
| 205 | + $crate::RustScriptExtensionLayer::initialize( |
| 206 | + $scripts_module::__godot_rust_script_init, |
| 207 | + $scripts_module::__GODOT_RUST_SCRIPT_SRC_ROOT, |
| 208 | + ) |
| 209 | + }; |
| 210 | +} |
| 211 | + |
| 212 | +#[macro_export] |
| 213 | +macro_rules! deinit { |
| 214 | + () => { |
| 215 | + $crate::RustScriptExtensionLayer::deinitialize() |
| 216 | + }; |
| 217 | +} |
0 commit comments