Skip to content

Commit

Permalink
Add a logging util
Browse files Browse the repository at this point in the history
  • Loading branch information
jac3km4 committed Mar 14, 2022
1 parent e211f34 commit 92ff805
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
1 change: 1 addition & 0 deletions doc/FUNCTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
All game functions are in a `game` namespace, they can be called like this:
```rhai
game::GiveXP(1)
log(game::GetRamAttackRange())
game::OnQuestSuccess_Player_TraitorToHumanity()
game::Kill()
```
Expand Down
43 changes: 26 additions & 17 deletions src/host.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use std::borrow::Cow;
use std::fmt::Debug;
use std::sync::mpsc;

use rhai::{Engine, Module, Scope};
use rhai::{Dynamic, Engine, Module, Scope};

use crate::elex::FunctionPtr;

#[derive(Debug)]
pub struct ScriptHost {
pub(crate) cmd: String,
pub(crate) history: String,
log_receiver: mpsc::Receiver<String>,
is_active: bool,
engine: Engine,
scope: Scope<'static>,
Expand All @@ -17,10 +18,17 @@ pub struct ScriptHost {
impl Default for ScriptHost {
fn default() -> Self {
let mut engine = Engine::new();
let (tx, tr) = mpsc::channel();

engine.register_static_module("game", ScriptHost::create_game_module().into());
engine.register_fn("log", move |val: Dynamic| {
tx.send(val.to_string()).ok();
});

Self {
cmd: String::new(),
history: String::new(),
log_receiver: tr,
is_active: false,
engine,
scope: Scope::new(),
Expand All @@ -33,19 +41,28 @@ impl ScriptHost {
self.history.push_str(&self.cmd);
self.history.push('\n');

let out = self.handle_command();
self.history.push_str(&out);
if let Err(err) = self.engine.run_with_scope(&mut self.scope, &self.cmd) {
self.history.push_str(&err.to_string());
self.history.push('\n');
}
self.cmd.clear();
}

fn handle_command(&mut self) -> Cow<'static, str> {
let res = self.engine.run_with_scope(&mut self.scope, &self.cmd);
match res {
Ok(()) => Cow::Borrowed(""),
Err(err) => Cow::Owned(err.to_string() + "\n"),
pub fn process_events(&mut self) {
while let Ok(str) = self.log_receiver.try_recv() {
self.history.push_str(&str);
self.history.push('\n');
}
}

pub fn toggle(&mut self) {
self.is_active = !self.is_active;
}

pub fn is_active(&self) -> bool {
self.is_active
}

fn create_game_module() -> Module {
let mut module = Module::new();

Expand All @@ -58,14 +75,6 @@ impl ScriptHost {
}
module
}

pub fn toggle(&mut self) {
self.is_active = !self.is_active;
}

pub fn is_active(&self) -> bool {
self.is_active
}
}

pub type CustomHandler = fn(&str, &mut Module, FunctionPtr) -> u64;
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ egui_hook!(ScriptHost, ui);
fn ui(ctx: &Context, app: &mut ScriptHost) {
const DEFAULT_SIZE: Vec2 = Vec2::new(600., 320.);

let was_active = app.is_active();
if ctx.input().key_pressed(Key::Home) {
app.toggle();
}
Expand Down Expand Up @@ -38,9 +39,14 @@ fn ui(ctx: &Context, app: &mut ScriptHost) {
.desired_width(600.)
.show(ui);

if app.is_active() != was_active {
input.response.request_focus();
}

if ui.input().key_pressed(Key::Enter) {
input.response.request_focus();
app.process_command();
};
app.process_events();
});
}

0 comments on commit 92ff805

Please # to comment.