Enable init.lua to interact with default MUD

This commit is contained in:
Condorra 2024-11-25 21:19:50 +11:00
parent a08e29f8d8
commit b36bcbf2ac
4 changed files with 32 additions and 13 deletions

View File

@ -104,7 +104,7 @@ impl PartialEq for FrameData {
pub type RegisteredTermFrames = HashMap<FrameId, FrameData>;
fn get_or_make_term_frame<'a>(
pub(super) fn get_or_make_term_frame<'a>(
frame: &FrameId,
frames: &'a mut RegisteredTermFrames,
// Only used for callbacks, expected frames already borrowed mut!

View File

@ -240,6 +240,12 @@ pub fn install_lua_globals(
ctx.set_global("commands", cmd_table);
let info_table = Table::new(&ctx);
ctx.set_global("info", info_table);
// Default everything to frame 1 for startup.
info_table.set(
ctx,
ctx.intern_static(b"current_frame"),
intern_id::<FrameId>(ctx, FrameId(1)),
)?;
let telopt_table = Table::new(&ctx);
let _ = info_table.set(ctx, "telopts", telopt_table);

View File

@ -5,6 +5,7 @@ use crate::{
timer_host::TimerHostAccessContext,
FrameId, FrameViewType, GlobalLayoutCell, GlobalLayoutState, GlobalMemoCell,
};
use anyhow::anyhow;
use gc_arena::{Gc, Rootable};
use itertools::Itertools;
use piccolo::{
@ -523,7 +524,10 @@ fn list_timers<'gc>(
cur_frame_id: FrameId,
) -> Result<(), Error<'gc>> {
let timer_val = match global_memo.frame_registry.borrow().get(&cur_frame_id) {
None => Err(anyhow::Error::msg("Frame no longer exists"))?,
None => Err(anyhow::anyhow!(
"List timers: Frame {} no longer exists",
cur_frame_id.0,
))?,
Some(frame_dat) => Table::from_value(ctx, frame_dat.timer_host.to_value(ctx)?)?,
};
if timer_val.iter().next().is_none() {
@ -631,7 +635,7 @@ pub(super) fn tick<'gc>(
.borrow_mut()
.get_mut(&cur_frame_id)
{
None => Err(anyhow::Error::msg("Frame no longer exists"))?,
None => Err(anyhow!("Tick: Frame {} no longer exists", cur_frame_id.0,))?,
Some(frame_dat) => frame_dat.timer_host.add_record(
GlobalCellTermFrameTimerHostAccess {
global_memo: global_memo.clone(),
@ -705,7 +709,7 @@ pub(super) fn delay<'gc>(
.borrow_mut()
.get_mut(&cur_frame_id)
{
None => Err(anyhow::Error::msg("Frame no longer exists"))?,
None => Err(anyhow!("Delay: Frame {} no longer exists", cur_frame_id.0))?,
Some(frame_dat) => frame_dat.timer_host.add_record(
GlobalCellTermFrameTimerHostAccess {
global_memo: global_memo.clone(),
@ -745,7 +749,7 @@ pub(super) fn untick<'gc>(
.borrow_mut()
.get_mut(&cur_frame_id)
{
None => Err(anyhow::Error::msg("Frame no longer exists"))?,
None => Err(anyhow!("Untick: Frame {} no longer exists", cur_frame_id.0,))?,
Some(frame_dat) => frame_dat.timer_host.remove_timer(name)?,
};
Ok(piccolo::CallbackReturn::Return)
@ -768,7 +772,7 @@ pub(super) fn editor<'gc>(
.borrow_mut()
.get_mut(&cur_frame_id)
{
None => Err(anyhow::Error::msg("Frame no longer exists"))?,
None => Err(anyhow!("Editor: Frame {} no longer exists", cur_frame_id.0))?,
Some(_frame_dat) => {
let mut new_gl: GlobalLayoutState = (*global_memo.layout.borrow().as_ref()).clone();
new_gl
@ -816,7 +820,10 @@ pub fn html<'gc>(
.borrow_mut()
.get_mut(&target_frame_id)
{
None => Err(anyhow::Error::msg("Frame no longer exists"))?,
None => Err(anyhow!(
"html: Frame {} no longer exists",
target_frame_id.0
))?,
Some(_frame_dat) => {
let mut new_gl: GlobalLayoutState = (*global_memo.layout.borrow().as_ref()).clone();
new_gl

View File

@ -82,21 +82,27 @@ fn app() -> Html {
layout: RefCell::new((*global_layout).clone()),
log_engine: RefCell::new(Default::default()),
});
let global_memo_cb = global_memo.clone();
use_memo((), |_| {
get_or_make_term_frame(
&FrameId(1),
&mut global_memo.frame_registry.borrow_mut(),
&global_memo_cb,
);
install_lua_globals(&global_memo, global_layout.setter())
.expect("Couldn't install Lua globals");
match try_run_script("init.lua", &global_memo) {
Ok(_) => {}
Err(e) => {
console::log_1(&format!("Error running init.lua: {}", e).into());
}
}
match connect_default_mud(&global_memo) {
Ok(_) => {}
Err(e) => {
console::log_1(&format!("Error connecting to default MUD: {}", e).into());
}
}
match try_run_script("init.lua", &global_memo) {
Ok(_) => {}
Err(e) => {
console::log_1(&format!("Error running init.lua: {}", e).into());
}
}
});
html! {