-
-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add "validate_property" virtual func #1030
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
use crate::builder::ClassBuilder; | ||
use crate::builtin::{StringName, Variant}; | ||
use crate::classes::Object; | ||
use crate::meta::PropertyInfo; | ||
use crate::obj::{bounds, cap, AsDyn, Base, Bounds, Gd, GodotClass, Inherits, UserClass}; | ||
use crate::registry::plugin::ErasedDynGd; | ||
use crate::storage::{as_storage, InstanceStorage, Storage, StorageRefCounted}; | ||
|
@@ -357,6 +358,35 @@ pub unsafe extern "C" fn property_get_revert<T: cap::GodotPropertyGetRevert>( | |
|
||
sys::conv::SYS_TRUE | ||
} | ||
|
||
/// Callback for `validate_property`. | ||
/// | ||
/// Exposes `PropertyInfo` created out of `*mut GDExtensionPropertyInfo` ptr to user and moves edited values back to the pointer. | ||
/// | ||
/// # Safety | ||
/// | ||
/// - Must only be called by Godot as a callback for `validate_property` for a rust-defined class of type `T`. | ||
/// - `property_info_ptr` must be valid for the whole duration of this function call (i.e. - can't be freed nor consumed). | ||
/// | ||
#[deny(unsafe_op_in_unsafe_fn)] | ||
#[cfg(since_api = "4.2")] | ||
pub unsafe extern "C" fn validate_property<T: cap::GodotValidateProperty>( | ||
instance: sys::GDExtensionClassInstancePtr, | ||
property_info_ptr: *mut sys::GDExtensionPropertyInfo, | ||
) -> sys::GDExtensionBool { | ||
// SAFETY: `instance` is a valid `T` instance pointer for the duration of this function call. | ||
let storage = unsafe { as_storage::<T>(instance) }; | ||
let instance = storage.get(); | ||
|
||
// SAFETY: property_info_ptr must be valid. | ||
let mut property_info = unsafe { PropertyInfo::new_from_sys(property_info_ptr) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Safety statement? 🙂 |
||
T::__godot_validate_property(&*instance, &mut property_info); | ||
|
||
// SAFETY: property_info_ptr remains valid & unchanged. | ||
unsafe { property_info.move_into_property_info_ptr(property_info_ptr) }; | ||
|
||
sys::conv::SYS_TRUE | ||
} | ||
// ---------------------------------------------------------------------------------------------------------------------------------------------- | ||
// Safe, higher-level methods | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright (c) godot-rust; Bromeon and contributors. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use godot::builtin::{Array, Dictionary, GString, StringName}; | ||
use godot::classes::IObject; | ||
use godot::global::{PropertyHint, PropertyUsageFlags}; | ||
use godot::meta::PropertyInfo; | ||
use godot::obj::NewAlloc; | ||
use godot::register::{godot_api, GodotClass}; | ||
use godot::test::itest; | ||
|
||
#[derive(GodotClass)] | ||
#[class(base = Object, init)] | ||
pub struct ValidatePropertyTest { | ||
#[var(hint = NONE, hint_string = "initial")] | ||
#[export] | ||
my_var: i64, | ||
} | ||
|
||
#[godot_api] | ||
impl IObject for ValidatePropertyTest { | ||
fn validate_property(&self, property: &mut PropertyInfo) { | ||
if property.property_name.to_string() == "my_var" { | ||
property.usage = PropertyUsageFlags::NO_EDITOR; | ||
property.property_name = StringName::from("SuperNewTestPropertyName"); | ||
property.hint_info.hint_string = GString::from("SomePropertyHint"); | ||
property.hint_info.hint = PropertyHint::TYPE_STRING; | ||
|
||
// Makes no sense, but allows to check if given ClassName can be properly moved to GDExtensionPropertyInfo. | ||
property.class_name = <ValidatePropertyTest as godot::obj::GodotClass>::class_name(); | ||
} | ||
} | ||
} | ||
|
||
#[itest] | ||
fn validate_property_test() { | ||
let obj = ValidatePropertyTest::new_alloc(); | ||
let properties: Array<Dictionary> = obj.get_property_list(); | ||
|
||
let property = properties | ||
.iter_shared() | ||
.find(|dict| { | ||
dict.get("name") | ||
.is_some_and(|v| v.to_string() == "SuperNewTestPropertyName") | ||
}) | ||
.expect("Test failed – unable to find validated property."); | ||
|
||
let hint_string = property | ||
.get("hint_string") | ||
.expect("validated property dict should contain a `hint_string` entry.") | ||
.to::<GString>(); | ||
assert_eq!(hint_string, GString::from("SomePropertyHint")); | ||
|
||
let class = property | ||
.get("class_name") | ||
.expect("Validated property dict should contain a class_name entry.") | ||
.to::<StringName>(); | ||
assert_eq!(class, StringName::from("ValidatePropertyTest")); | ||
|
||
let usage = property | ||
.get("usage") | ||
.expect("Validated property dict should contain an usage entry.") | ||
.to::<PropertyUsageFlags>(); | ||
assert_eq!(usage, PropertyUsageFlags::NO_EDITOR); | ||
|
||
let hint = property | ||
.get("hint") | ||
.expect("Validated property dict should contain a hint entry.") | ||
.to::<PropertyHint>(); | ||
assert_eq!(hint, PropertyHint::TYPE_STRING); | ||
|
||
obj.free(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to add docs, see other methods 🙂