Skip to content

fix: remove reflect_functions and file_watcher flags from bevy dependency #316

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

Merged
merged 5 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions .github/workflows/bevy_mod_scripting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,23 @@ jobs:
matrix:
run_args: ${{fromJson(needs.generate-job-matrix.outputs.matrix)}}
steps:
- name: Free Disk Space (Ubuntu)
if: runner.os == 'Linux'
uses: jlumbroso/free-disk-space@main
with:
tool-cache: false
android: true
dotnet: true
haskell: true
large-packages: true
docker-images: true
swap-storage: true
# - if: runner.os == 'linux'
# run: |
# sudo rm -rf /usr/share/dotnet; sudo rm -rf /opt/ghc; sudo rm -rf "/usr/local/share/boost"; sudo rm -rf "$AGENT_TOOLSDIRECTORY"
- name: Checkout
if: ${{ needs.check-needs-run.outputs.any-changes == 'true' }}
uses: actions/checkout@v4
- if: runner.os == 'linux'
run: |
sudo rm -rf /usr/share/dotnet; sudo rm -rf /opt/ghc; sudo rm -rf "/usr/local/share/boost"; sudo rm -rf "$AGENT_TOOLSDIRECTORY"
- uses: actions-rs/toolchain@v1
if: ${{ needs.check-needs-run.outputs.any-changes == 'true' }}
with:
Expand Down
5 changes: 1 addition & 4 deletions crates/bevy_mod_scripting_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ rhai = { version = "1.21", default-features = false, features = [
"sync",
], optional = true }

bevy = { workspace = true, default-features = false, features = [
"bevy_asset",
"reflect_functions",
] }
bevy = { workspace = true, default-features = false, features = ["bevy_asset"] }

thiserror = "1.0.31"
parking_lot = "0.12.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ use crate::{
error::InteropError,
ScriptValue,
};
use bevy::{
prelude::{Reflect, Resource},
reflect::func::FunctionError,
};
use bevy::prelude::{Reflect, Resource};
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::borrow::Cow;
use std::collections::{HashMap, VecDeque};
Expand Down Expand Up @@ -604,10 +601,7 @@ macro_rules! impl_script_function {
if let Some(default) = <$param>::default_value() {
default
} else {
return Err(InteropError::function_call_error(FunctionError::ArgCountMismatch{
expected: expected_arg_count,
received: received_args_len
}));
return Err(InteropError::argument_count_mismatch(expected_arg_count,received_args_len));
}
}
};
Expand Down Expand Up @@ -695,10 +689,7 @@ mod test {
InteropError::function_interop_error(
"my_fn",
Namespace::Global,
InteropError::function_call_error(FunctionError::ArgCountMismatch {
expected: 2,
received: 1
})
InteropError::argument_count_mismatch(2, 1)
)
);
});
Expand Down
13 changes: 4 additions & 9 deletions crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,10 @@ impl ReflectReferencePrinter {
ReflectRef::Opaque(o) => {
self.pretty_print_value_opaque(o, output);
}
ReflectRef::Function(f) => {
output.push_str("Function(");
output.push_str(
f.info()
.name()
.unwrap_or(&Cow::Borrowed("<unnamed function>"))
.as_ref(),
);
output.push(')');
// for function_reflection from bevy or other feature gated things
#[allow(unreachable_patterns)]
_ => {
output.push_str(&format!("{:?}", v));
}
}
}
Expand Down
62 changes: 33 additions & 29 deletions crates/bevy_mod_scripting_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::bindings::{
use bevy::{
ecs::component::ComponentId,
prelude::Entity,
reflect::{func::FunctionError, PartialReflect, Reflect},
reflect::{PartialReflect, Reflect},
};
use std::{
any::TypeId,
Expand Down Expand Up @@ -481,13 +481,6 @@ impl InteropError {
}))
}

/// Thrown when the error happens after a function call, and an error is thrown by bevy.
///
/// I.e. mismatch in args, or invalid number of arguments
pub fn function_call_error(inner: FunctionError) -> Self {
Self(Arc::new(InteropErrorInner::FunctionCallError { inner }))
}

/// Thrown when an error happens during argument conversion in a function call
pub fn function_arg_conversion_error(argument: String, error: InteropError) -> Self {
Self(Arc::new(InteropErrorInner::FunctionArgConversionError {
Expand Down Expand Up @@ -555,6 +548,14 @@ impl InteropError {
variant_name: variant_name.to_string(),
}))
}

/// Thrown when the number of arguments in a function call does not match.
pub fn argument_count_mismatch(expected: usize, got: usize) -> Self {
Self(Arc::new(InteropErrorInner::ArgumentCountMismatch {
expected,
got,
}))
}
}

/// For errors to do with reflection, type conversions or other interop issues
Expand Down Expand Up @@ -684,11 +685,6 @@ pub(crate) enum InteropErrorInner {
/// The component that was invalid
component_id: ComponentId,
},
/// Thrown when an error happens in a function call
FunctionCallError {
/// The inner error that occurred
inner: FunctionError,
},
/// Thrown when an error happens during argument conversion in a function call
MissingFunctionError {
/// The type that the function was attempted to be called on
Expand Down Expand Up @@ -739,6 +735,8 @@ pub(crate) enum InteropErrorInner {
type_id: TypeId,
variant_name: String,
},
/// Thrown when the number of arguments in a function call does not match.
ArgumentCountMismatch { expected: usize, got: usize },
}

/// For test purposes
Expand Down Expand Up @@ -879,10 +877,6 @@ impl PartialEq for InteropErrorInner {
InteropErrorInner::InvalidComponent { component_id: a },
InteropErrorInner::InvalidComponent { component_id: b },
) => a == b,
(
InteropErrorInner::FunctionCallError { inner: a },
InteropErrorInner::FunctionCallError { inner: b },
) => a == b,
(
InteropErrorInner::MissingFunctionError {
on: a,
Expand Down Expand Up @@ -947,6 +941,16 @@ impl PartialEq for InteropErrorInner {
variant_name: d,
},
) => a == c && b == d,
(
InteropErrorInner::ArgumentCountMismatch {
expected: a,
got: b,
},
InteropErrorInner::ArgumentCountMismatch {
expected: c,
got: d,
},
) => a == c && b == d,
_ => false,
}
}
Expand Down Expand Up @@ -1084,12 +1088,6 @@ macro_rules! function_arg_conversion_error {
};
}

macro_rules! function_call_error {
($inner:expr) => {
format!("Error in function call: {}", $inner)
};
}

macro_rules! better_conversion_exists {
($context:expr) => {
format!("Unfinished conversion in context of: {}. A better conversion exists but caller didn't handle the case.", $context)
Expand Down Expand Up @@ -1248,9 +1246,6 @@ impl DisplayWithWorld for InteropErrorInner {
InteropErrorInner::FunctionArgConversionError { argument, error } => {
function_arg_conversion_error!(argument, error.display_with_world(world))
},
InteropErrorInner::FunctionCallError { inner } => {
function_call_error!(inner)
},
InteropErrorInner::BetterConversionExists{ context } => {
better_conversion_exists!(context)
},
Expand All @@ -1277,6 +1272,12 @@ impl DisplayWithWorld for InteropErrorInner {
type_id.display_with_world(world)
)
},
InteropErrorInner::ArgumentCountMismatch { expected, got } => {
format!(
"Argument count mismatch, expected: {}, got: {}",
expected, got
)
},
}
}

Expand Down Expand Up @@ -1387,9 +1388,6 @@ impl DisplayWithWorld for InteropErrorInner {
InteropErrorInner::FunctionArgConversionError { argument, error } => {
function_arg_conversion_error!(argument, error.display_without_world())
},
InteropErrorInner::FunctionCallError { inner } => {
function_call_error!(inner)
},
InteropErrorInner::BetterConversionExists{ context } => {
better_conversion_exists!(context)
},
Expand All @@ -1416,6 +1414,12 @@ impl DisplayWithWorld for InteropErrorInner {
type_id.display_without_world()
)
},
InteropErrorInner::ArgumentCountMismatch { expected, got } => {
format!(
"Argument count mismatch, expected: {}, got: {}",
expected, got
)
},
}
}
}
Expand Down
35 changes: 1 addition & 34 deletions crates/bevy_mod_scripting_core/src/reflection_extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use crate::{
bindings::{ReflectReference, WorldGuard},
error::InteropError,
};
use bevy::reflect::{
func::Return, FromReflect, PartialReflect, Reflect, ReflectFromReflect, ReflectMut, TypeInfo,
};
use bevy::reflect::{PartialReflect, Reflect, ReflectFromReflect, ReflectMut, TypeInfo};
use std::{
any::{Any, TypeId},
cmp::max,
Expand Down Expand Up @@ -460,37 +458,6 @@ impl TypeInfoExtensions for TypeInfo {
}
}

/// Extension trait for [`Return`] providing additional functionality for working with return values.
pub trait ReturnValExt<'a> {
/// Try to convert the return value into the concrete type, or return a boxed partial reflect if the conversion fails.
fn try_into_or_boxed<T: PartialReflect + FromReflect>(
self,
) -> Result<T, Box<dyn PartialReflect>>;

/// Get a reference to the partial reflect value.
fn as_ref(&'a self) -> &'a dyn PartialReflect;
}

impl<'a> ReturnValExt<'a> for Return<'a> {
fn as_ref(&'a self) -> &'a dyn PartialReflect {
match self {
Return::Owned(f) => f.as_partial_reflect(),
Return::Ref(r) => r.as_partial_reflect(),
Return::Mut(r) => r.as_partial_reflect(),
}
}

fn try_into_or_boxed<T: PartialReflect + FromReflect>(
self,
) -> Result<T, Box<dyn PartialReflect>> {
match self {
Return::Owned(partial_reflect) => partial_reflect.try_take::<T>(),
Return::Ref(r) => T::from_reflect(r).ok_or_else(|| r.clone_value()),
Return::Mut(r) => T::from_reflect(r).ok_or_else(|| r.clone_value()),
}
}
}

#[cfg(test)]
mod test {
use bevy::reflect::{DynamicMap, Map};
Expand Down
2 changes: 0 additions & 2 deletions crates/bevy_mod_scripting_functions/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ bevy_bindings = []

[dependencies]
bevy = { workspace = true, features = [
"reflect_functions",
"bevy_asset",
"bevy_animation",
"bevy_core_pipeline",
Expand All @@ -27,7 +26,6 @@ bevy = { workspace = true, features = [
"bevy_render",
"bevy_text",
"bevy_sprite",
"file_watcher",
"multi_threaded",
] }
profiling = { workspace = true }
Expand Down
20 changes: 11 additions & 9 deletions crates/ladfile_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ pub mod plugin;

use bevy_mod_scripting_core::{
bindings::{
function::{namespace::Namespace, script_function::FunctionCallContext},
function::{
namespace::Namespace,
script_function::{
DynamicScriptFunction, DynamicScriptFunctionMut, FunctionCallContext,
},
},
ReflectReference,
},
docgen::{
Expand All @@ -12,10 +17,7 @@ use bevy_mod_scripting_core::{
},
match_by_type,
};
use bevy_reflect::{
func::{DynamicFunction, DynamicFunctionMut},
NamedField, TypeInfo, TypeRegistry, Typed, UnnamedField,
};
use bevy_reflect::{NamedField, TypeInfo, TypeRegistry, Typed, UnnamedField};
use ladfile::*;
use std::{
any::TypeId,
Expand Down Expand Up @@ -53,8 +55,8 @@ fn primitive_from_type_id(type_id: TypeId) -> Option<LadBMSPrimitiveKind> {
i: OsString => return Some(LadBMSPrimitiveKind::OsString),
i: PathBuf => return Some(LadBMSPrimitiveKind::PathBuf),
i: FunctionCallContext => return Some(LadBMSPrimitiveKind::FunctionCallContext),
i: DynamicFunction => return Some(LadBMSPrimitiveKind::DynamicFunction),
i: DynamicFunctionMut => return Some(LadBMSPrimitiveKind::DynamicFunctionMut),
i: DynamicScriptFunction => return Some(LadBMSPrimitiveKind::DynamicFunction),
i: DynamicScriptFunctionMut => return Some(LadBMSPrimitiveKind::DynamicFunctionMut),
i: ReflectReference => return Some(LadBMSPrimitiveKind::ReflectReference)
});
None
Expand Down Expand Up @@ -101,8 +103,8 @@ impl<'t> LadFileBuilder<'t> {
.add_bms_primitive::<OsString>("A heap allocated OS string")
.add_bms_primitive::<PathBuf>("A heap allocated file path")
.add_bms_primitive::<FunctionCallContext>("Function call context, if accepted by a function, means the function can access the world in arbitrary ways.")
.add_bms_primitive::<DynamicFunction>("A callable dynamic function")
.add_bms_primitive::<DynamicFunctionMut>("A stateful and callable dynamic function")
.add_bms_primitive::<DynamicScriptFunction>("A callable dynamic function")
.add_bms_primitive::<DynamicScriptFunctionMut>("A stateful and callable dynamic function")
.add_bms_primitive::<ReflectReference>("A reference to a reflectable type");

builder
Expand Down
Loading