Skip to content

Commit 00a7a81

Browse files
committed
feat: expand lua scripting bridge and tooling
1 parent 0eada42 commit 00a7a81

10 files changed

Lines changed: 670 additions & 4 deletions

File tree

‎crates/teamtalk/examples/lua_script_commands.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use teamtalk::extensions::scripts::ScriptManager;
44
#[cfg(feature = "scripts")]
55
fn main() -> Result<(), Box<dyn std::error::Error>> {
66
let mut manager = ScriptManager::new();
7-
manager.load_script("commands", "scripts/commands.lua")?;
7+
manager.load_script("commands", "crates/teamtalk/examples/scripts/commands.lua")?;
88
let handled = manager.call_command("start", &["channel".to_string()])?;
99
println!("handled: {}", handled);
1010
Ok(())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[cfg(feature = "scripts")]
2+
use teamtalk::Client;
3+
#[cfg(feature = "scripts")]
4+
fn main() -> Result<(), Box<dyn std::error::Error>> {
5+
let client = Client::new()?;
6+
client.enable_scripts();
7+
client.scripts_mut(|scripts| {
8+
let _ = scripts.load_script("events", "crates/teamtalk/examples/scripts/events.lua");
9+
});
10+
Ok(())
11+
}
12+
13+
#[cfg(not(feature = "scripts"))]
14+
fn main() {
15+
eprintln!("Enable scripts feature: cargo run --example lua_script_events --features scripts");
16+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
commands = {
2+
start = function(args)
3+
return true
4+
end
5+
}
6+
7+
state = {
8+
total_text = 0
9+
}
10+
11+
function on_event(ev)
12+
if ev.type == "TextMessage" and ev.text ~= nil then
13+
state.total_text = state.total_text + 1
14+
end
15+
return false
16+
end
17+
18+
events = {
19+
UserJoined = function(ev)
20+
return false
21+
end,
22+
UserLeft = function(ev)
23+
return false
24+
end
25+
}

‎crates/teamtalk/src/client/core.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ impl Client {
3636
self.update_cache_for_event(event, &message);
3737
self.invoke_hooks(event, &message);
3838
self.dispatch_bus(event, &message);
39+
#[cfg(feature = "scripts")]
40+
self.dispatch_scripts(event, &message);
3941
self.handle_auto_reconnect();
4042
Some((event, message))
4143
} else {

‎crates/teamtalk/src/client/mod.rs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Core client type and message wrapper.
22
use crate::events::{ConnectionState, Error, Event, Result};
3+
#[cfg(feature = "scripts")]
4+
use crate::extensions::scripts::ScriptManager;
35
use crate::types::ClientId;
46
use std::cell::{Cell, RefCell};
57
use std::sync::atomic::{AtomicU64, Ordering};
@@ -43,6 +45,8 @@ pub struct Client {
4345
state: Cell<ConnectionState>,
4446
hooks: RefCell<ClientHooks>,
4547
bus: RefCell<bus::EventBus>,
48+
#[cfg(feature = "scripts")]
49+
scripts: RefCell<Option<ScriptManager>>,
4650
auto_reconnect: RefCell<AutoReconnectState>,
4751
cache: RefCell<cache::CacheState>,
4852
}
@@ -63,6 +67,8 @@ impl Client {
6367
state: Cell::new(ConnectionState::Idle),
6468
hooks: RefCell::new(ClientHooks::default()),
6569
bus: RefCell::new(bus::EventBus::default()),
70+
#[cfg(feature = "scripts")]
71+
scripts: RefCell::new(None),
6672
auto_reconnect: RefCell::new(AutoReconnectState::default()),
6773
cache: RefCell::new(cache::CacheState::default()),
6874
})
@@ -89,6 +95,8 @@ impl Client {
8995
state: Cell::new(ConnectionState::Idle),
9096
hooks: RefCell::new(ClientHooks::default()),
9197
bus: RefCell::new(bus::EventBus::default()),
98+
#[cfg(feature = "scripts")]
99+
scripts: RefCell::new(None),
92100
auto_reconnect: RefCell::new(AutoReconnectState::default()),
93101
cache: RefCell::new(cache::CacheState::default()),
94102
})
@@ -178,6 +186,32 @@ impl Client {
178186
*self.hooks.borrow_mut() = ClientHooks::default();
179187
}
180188

189+
#[cfg(feature = "scripts")]
190+
pub fn enable_scripts(&self) {
191+
let mut scripts = self.scripts.borrow_mut();
192+
if scripts.is_none() {
193+
*scripts = Some(ScriptManager::new());
194+
}
195+
}
196+
197+
#[cfg(feature = "scripts")]
198+
pub fn set_script_manager(&self, manager: ScriptManager) {
199+
*self.scripts.borrow_mut() = Some(manager);
200+
}
201+
202+
#[cfg(feature = "scripts")]
203+
pub fn clear_scripts(&self) {
204+
*self.scripts.borrow_mut() = None;
205+
}
206+
207+
#[cfg(feature = "scripts")]
208+
pub fn scripts_mut<F, R>(&self, f: F) -> Option<R>
209+
where
210+
F: FnOnce(&mut ScriptManager) -> R,
211+
{
212+
self.scripts.borrow_mut().as_mut().map(f)
213+
}
214+
181215
pub(crate) fn set_connection_state(&self, state: ConnectionState) {
182216
self.state.set(state);
183217
}
@@ -190,6 +224,13 @@ impl Client {
190224
self.bus.borrow_mut().dispatch(self, event, msg);
191225
}
192226

227+
#[cfg(feature = "scripts")]
228+
pub(crate) fn dispatch_scripts(&self, event: crate::events::Event, msg: &Message) {
229+
if let Some(manager) = self.scripts.borrow().as_ref() {
230+
let _ = manager.handle_event(event, msg);
231+
}
232+
}
233+
193234
pub(crate) fn invoke_joined_hook(&self, channel_id: crate::types::ChannelId) {
194235
self.hooks.borrow_mut().fire_joined(self, channel_id);
195236
}

0 commit comments

Comments
 (0)