Skip to content

Commit

Permalink
More documentation and custom functions example.
Browse files Browse the repository at this point in the history
  • Loading branch information
doonv committed Dec 13, 2023
1 parent 16800da commit d785a25
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ rustflags = ["-Clink-arg=-fuse-ld=lld"]
rustflags = [
"-C",
"link-arg=-fuse-ld=/usr/local/opt/llvm/bin/ld64.lld",
"-Zshare-generics=y",
# "-Zshare-generics=y",
]

[target.aarch64-apple-darwin]
rustflags = [
"-C",
"link-arg=-fuse-ld=/opt/homebrew/opt/llvm/bin/ld64.lld",
"-Zshare-generics=y",
# "-Zshare-generics=y",
]

[target.x86_64-pc-windows-msvc]
linker = "rust-lld.exe"
rustflags = ["-Zshare-generics=n"]
# rustflags = ["-Zshare-generics=n"]

# Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only'
# In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains.
Expand Down
3 changes: 2 additions & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! A simple exmaple
//! A simple example

use bevy::{
log::{Level, LogPlugin},
Expand All @@ -16,6 +16,7 @@ fn main() {
// Not removing the LogPlugin will cause a panic!
DefaultPlugins.build().disable::<LogPlugin>(),
// Add the dev console plugin itself.
DevConsolePlugin,
))
.add_systems(Startup, test)
.run();
Expand Down
62 changes: 62 additions & 0 deletions examples/custom_functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! A simple exmaple

use bevy::{
log::{Level, LogPlugin},
prelude::*,
};
use bevy_dev_console::{
builtin_parser::{Environment, Spanned, Value},
prelude::*,
register,
};

// Declare the functions we want to create:

// Basic function
fn time_since_epoch() {
let time = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap();
info!("The unix epoch was {} seconds ago", time.as_secs());
}

// Function with parameters and return value
fn add(num1: f64, num2: f64) -> f64 {
num1 + num2
}

// Function with any value + span
fn print_debug_info(value: Spanned<Value>) {
info!(
"Location in command: {:?}, Value: {:?}",
value.span, value.value
)
}

// For more example take a look at the standard library.

// Register our functions by creating and inserting our own environment
fn custom_environment() -> Environment {
let mut environment = Environment::default();

// The register macro allows us to easily add functions to the environment.
register!(&mut environment => {
fn time_since_epoch;
fn add;
fn print_debug_info;
});

environment
}

fn main() {
App::new()
// Insert our new environment
.insert_non_send_resource(custom_environment())
.add_plugins((
ConsoleLogPlugin::default().append_filter(module_path!(), Level::TRACE),
DefaultPlugins.build().disable::<LogPlugin>(),
DevConsolePlugin,
))
.run();
}
2 changes: 1 addition & 1 deletion examples/resource.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Example of modifying resources via the console via reflection.
//!
//!
//! **Warning:** This is very experimental, might not work.

use bevy::{log::LogPlugin, prelude::*};
Expand Down
9 changes: 5 additions & 4 deletions src/builtin_parser.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! [`bevy_dev_console`](crate)'s built-in command parser.
//!
//!
//! Currently the built-in command parser is in very early development.
//! It's purpose is to provide a simple, yet powerful method of modifying
//! It's purpose is to provide a simple, yet powerful method of modifying
//! the game world via commands.

use bevy::prelude::*;
Expand All @@ -16,11 +16,12 @@ pub(crate) mod parser;
pub(crate) mod runner;

pub use runner::environment::Environment;
pub use runner::Value;

/// Wrapper around `T` that stores a [Span] (A location in the source code)
#[derive(Debug, Clone)]
pub struct Spanned<T> {
/// The location of `T` in the source.
/// The location of `T` in the source/command.
pub span: Span,
/// The value of `T`.
pub value: T,
Expand All @@ -33,7 +34,7 @@ impl Default for DefaultCommandParser {
}

/// [`bevy_dev_console`](crate)'s built-in command parser.
///
///
/// See the [module level documentation for more](self).
#[derive(Default)]
pub struct BuiltinCommandParser;
Expand Down
8 changes: 6 additions & 2 deletions src/builtin_parser/runner/environment.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{cell::RefCell, collections::HashMap, rc::Rc};

use bevy::{ecs::world::World, reflect::TypeRegistration};
use bevy::{ecs::world::World, log::warn, reflect::TypeRegistration};
use logos::Span;

use super::{
Expand Down Expand Up @@ -254,8 +254,12 @@ impl Environment {
name: impl Into<String>,
function: impl IntoFunction<T>,
) -> &mut Self {
let name = name.into();
if self.variables.contains_key(&name) {
warn!("Function {name} declared twice.")
}
self.variables
.insert(name.into(), Variable::Function(function.into_function()));
.insert(name, Variable::Function(function.into_function()));

self
}
Expand Down
7 changes: 6 additions & 1 deletion src/builtin_parser/runner/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{cell::Ref, ops::Range};

use bevy::log::info;

use super::{RunError, Value, Environment, Spanned};
use super::{Environment, RunError, Spanned, Value};

fn print(value: Spanned<Value>) -> Result<(), RunError> {
match value.value {
Expand Down Expand Up @@ -49,6 +49,9 @@ fn ref_depth(Spanned { span, value }: Spanned<Value>) -> Result<f64, RunError> {
})
}

/// Disposes of a [`Value`].
fn drop(_v: Value) {}

/// Macro for mass registering functions.
///
/// ```
Expand Down Expand Up @@ -85,10 +88,12 @@ macro_rules! register {
)*
};
}

pub fn register(environment: &mut Environment) {
register!(environment => {
fn print;
fn dbg;
fn ref_depth;
fn drop;
});
}
13 changes: 10 additions & 3 deletions src/builtin_parser/runner/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::rc::Weak;
use std::{cell::RefCell, rc::Rc};

use super::environment::{FunctionParameterData, ResultContainer};
use super::{RunError, super::Spanned};
use super::{super::Spanned, RunError};

use bevy::ecs::world::World;
use bevy::reflect::Reflect;
Expand All @@ -30,16 +30,23 @@ pub enum Value {
/// This isn't partically efficent, so:
/// TODO: Create a custom type this!
Reference(Weak<RefCell<Value>>),
/// A dynamic [`HashMap].
Object(HashMap<String, Rc<RefCell<Value>>>),
/// An [`Object`](Value::Object) with a name attached to it.
StructObject {
/// The name of the struct
name: String,
/// The [`Object`](Value::Object) [`HashMap`].
map: HashMap<String, Rc<RefCell<Value>>>,
},
/// A reference to a dynamic value. (aka a reference.)
/// A reference to a dynamic value. (aka a reference)
Dynamic(Box<dyn Reflect>),
Object(HashMap<String, Rc<RefCell<Value>>>),
}

impl Value {
/// Attempts to format this [`Value`].
///
/// Returns an error if the [`Value`] is a reference to moved data.
pub fn try_format(&self, span: Span) -> Result<String, RunError> {
match self {
Value::None => Ok(format!("()")),
Expand Down
8 changes: 4 additions & 4 deletions src/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Command execution functionality.

use bevy::{prelude::*, ecs::system::Command};
use bevy::{ecs::system::Command, prelude::*};

/// The command parser currrently being used by the dev console.
#[derive(Resource)]
Expand All @@ -15,12 +15,12 @@ impl DefaultCommandParser {

/// The trait that all [`CommandParser`]s implement.
/// You can take a look at the [builtin parser](crate::builtin_parser) for an example.
///
///
/// ```
/// # use bevy::ecs::world::World;
/// # use bevy_dev_console::command::CommandParser;
/// # use bevy::log::info;
///
///
/// pub struct MyCustomParser;
/// impl CommandParser for MyCustomParser {
/// fn parse(&self, command: &str, world: &mut World) {
Expand All @@ -45,4 +45,4 @@ impl Command for ExecuteCommand {
error!("Default command parser doesn't exist, cannot execute command.");
}
}
}
}
4 changes: 2 additions & 2 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct ConsoleUiState {
fn system_time_to_chorno_utc(t: SystemTime) -> chrono::DateTime<chrono::Utc> {
let dur = t.duration_since(instant::SystemTime::UNIX_EPOCH).unwrap();
let (sec, nsec) = (dur.as_secs() as i64, dur.subsec_nanos());

chrono::Utc.timestamp_opt(sec, nsec).unwrap()
}

Expand Down Expand Up @@ -108,7 +108,7 @@ fn add_log(

ui.push_id(id, |ui| {
let time_utc = system_time_to_chorno_utc(*time);
let time: chrono::DateTime::<chrono::Local> = time_utc.into();
let time: chrono::DateTime<chrono::Local> = time_utc.into();
let res = ui
.horizontal_wrapped(|ui| {
ui.label(egui::RichText::new(time.format("%H:%M").to_string()).font(FONT_ID));
Expand Down

0 comments on commit d785a25

Please sign in to comment.