use std::collections::{HashSet, VecDeque};
use std::future::Future;
use std::panic::{AssertUnwindSafe, catch_unwind};
use std::pin::Pin;
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::task::{Context, Poll, Wake, Waker};
use ghast_client::{Behavior, GameMode, GameVersion, Health, Interests, Pose, TickContext, Vec3};
use crate::Error;
use crate::events::{Event, SubState};
pub(crate) type Request = Box<dyn FnOnce(&mut TickContext<'_>) + Send>;
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;
type Factory = Box<dyn FnMut(Bot) -> BoxFuture<Result<(), Error>> + Send>;
type PanicHook = Box<dyn FnOnce(String) + Send>;
const MAX_ROUNDS: usize = 256;
#[derive(Debug, Clone)]
pub(crate) struct Snapshot {
pub(crate) version: GameVersion,
pub(crate) ctx_tick: u64,
pub(crate) pose: Pose,
pub(crate) sent: Pose,
pub(crate) eyes: Vec3,
pub(crate) health: Health,
pub(crate) world_time: Option<ghast_client::WorldTime>,
pub(crate) dead: bool,
pub(crate) game_mode: GameMode,
pub(crate) held_slot: u8,
pub(crate) open_window: Option<i32>,
pub(crate) entity_id: i32,
pub(crate) dimension: String,
pub(crate) held_cooldown: Option<ghast_client::cooldown::ItemCooldown>,
}
impl Snapshot {
fn empty() -> Self {
Snapshot {
version: GameVersion::V1_8_9,
ctx_tick: 0,
pose: Pose::default(),
sent: Pose::default(),
eyes: Vec3::default(),
health: Health::default(),
world_time: None,
dead: false,
game_mode: GameMode::Survival,
held_slot: 0,
open_window: None,
entity_id: 0,
dimension: String::new(),
held_cooldown: None,
}
}
fn refresh(&mut self, ctx: &TickContext<'_>) {
self.version = ctx.version();
self.ctx_tick = ctx.tick();
self.pose = *ctx.pose();
self.sent = *ctx.sent();
self.eyes = ctx.eyes(ctx.pose());
self.health = ctx.health();
self.world_time = ctx.world_time();
self.dead = ctx.is_dead();
self.game_mode = ctx.game_mode();
self.held_slot = ctx.held_slot();
self.open_window = ctx.open_window();
self.entity_id = ctx.entity_id();
self.held_cooldown = ctx.held_cooldown();
if self.dimension != ctx.dimension() {
ctx.dimension().clone_into(&mut self.dimension);
}
}
}
pub(crate) struct Shared {
pub(crate) session: u64,
pub(crate) closed: Option<String>,
pub(crate) tick: u64,
pub(crate) snap: Snapshot,
pub(crate) requests: VecDeque<Request>,
pub(crate) tick_wakers: Vec<Waker>,
pub(crate) spawned: Vec<(u64, BoxFuture<()>, PanicHook)>,
pub(crate) aborted: Vec<u64>,
pub(crate) next_task: u64,
pub(crate) subs: Vec<Weak<Mutex<SubState>>>,
pub(crate) teleports: u64,
pub(crate) deaths: u64,
pub(crate) extra_interests: Interests,
entity_ids: Option<HashSet<i32>>,
pub(crate) interrupt_on_teleport: bool,
}
impl Shared {
fn new() -> Self {
Shared {
session: 0,
closed: Some("not started".into()),
tick: 0,
snap: Snapshot::empty(),
requests: VecDeque::new(),
tick_wakers: Vec::new(),
spawned: Vec::new(),
aborted: Vec::new(),
next_task: 1,
subs: Vec::new(),
teleports: 0,
deaths: 0,
extra_interests: Interests::NONE,
entity_ids: None,
interrupt_on_teleport: true,
}
}
fn wake_ticks(&mut self) {
for w in self.tick_wakers.drain(..) {
w.wake();
}
}
fn wake_subs(&mut self) {
self.subs.retain(|s| s.strong_count() > 0);
for s in &self.subs {
if let Some(s) = s.upgrade() {
let mut s = lock(&s);
if let Some(w) = s.waker.take() {
w.wake();
}
}
}
}
fn publish(&mut self, ctx: &TickContext<'_>) {
self.subs.retain(|s| s.strong_count() > 0);
if self.subs.is_empty() {
self.entity_ids = None;
return;
}
let ev = ctx.events();
let mut out = Vec::new();
if ev.joined {
out.push(Event::Joined);
}
if ev.respawned {
out.push(Event::Respawned);
}
for t in &ev.teleports {
out.push(Event::Teleport(*t));
}
if ev.died {
out.push(Event::Died);
}
if ev.health_changed {
out.push(Event::Health(ctx.health()));
}
if ev.game_mode_changed {
out.push(Event::GameMode(ctx.game_mode()));
}
for m in &ev.chat {
out.push(Event::Chat(m.clone()));
}
if let Some(id) = ev.inventory.opened {
out.push(Event::WindowOpened(id));
}
if ev.inventory.closed {
out.push(Event::WindowClosed);
}
if ev.inventory.player_changed {
out.push(Event::InventoryChanged);
}
for c in &ev.inventory.clicks {
out.push(Event::Click(*c));
}
for (pos, raw) in &ev.block_updates {
out.push(Event::BlockUpdate {
pos: *pos,
raw: *raw,
});
}
for h in ev.hud() {
out.push(Event::Hud(h.clone()));
}
for t in ev.tab_completions() {
out.push(Event::TabComplete(t.clone()));
}
for s in ev.signs() {
out.push(Event::Sign(*s));
}
for s in ev.sleep() {
out.push(Event::Sleep(*s));
}
for f in ev.fishing() {
out.push(Event::Fishing(*f));
}
if let Some(es) = ctx.entities() {
let me = es.self_id();
let now: HashSet<i32> = es
.iter()
.map(|e| e.id())
.filter(|id| Some(*id) != me)
.collect();
if let Some(before) = &self.entity_ids {
for id in now.difference(before) {
if let Some(e) = crate::find::entity_info(ctx, *id) {
out.push(Event::EntitySpawned(e));
}
}
for id in before.difference(&now) {
out.push(Event::EntityGone(*id));
}
}
self.entity_ids = Some(now);
} else {
self.entity_ids = None;
}
if out.is_empty() {
return;
}
for s in &self.subs {
if let Some(s) = s.upgrade() {
let mut s = lock(&s);
s.queue.extend(out.iter().cloned());
if let Some(w) = s.waker.take() {
w.wake();
}
}
}
}
}
pub(crate) fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
m.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
#[derive(Clone)]
pub struct Bot {
pub(crate) shared: Arc<Mutex<Shared>>,
}
impl std::fmt::Debug for Bot {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = lock(&self.shared);
f.debug_struct("Bot")
.field("session", &s.session)
.field("tick", &s.tick)
.field("pos", &s.snap.pose.pos)
.finish_non_exhaustive()
}
}
impl Bot {
fn new() -> Self {
Bot {
shared: Arc::new(Mutex::new(Shared::new())),
}
}
pub(crate) fn lock(&self) -> MutexGuard<'_, Shared> {
lock(&self.shared)
}
#[must_use]
pub fn tick(&self) -> u64 {
self.lock().tick
}
#[must_use]
pub fn version(&self) -> GameVersion {
self.lock().snap.version
}
#[must_use]
pub fn pose(&self) -> Pose {
self.lock().snap.pose
}
#[must_use]
pub fn sent(&self) -> Pose {
self.lock().snap.sent
}
#[must_use]
pub fn eyes(&self) -> Vec3 {
self.lock().snap.eyes
}
#[must_use]
pub fn health(&self) -> Health {
self.lock().snap.health
}
#[must_use]
pub fn world_time(&self) -> Option<ghast_client::WorldTime> {
self.lock().snap.world_time
}
#[must_use]
pub fn is_dead(&self) -> bool {
self.lock().snap.dead
}
#[must_use]
pub fn game_mode(&self) -> GameMode {
self.lock().snap.game_mode
}
#[must_use]
pub fn held_slot(&self) -> u8 {
self.lock().snap.held_slot
}
#[must_use]
pub fn held_cooldown(&self) -> Option<ghast_client::cooldown::ItemCooldown> {
self.lock().snap.held_cooldown.clone()
}
pub async fn wait_cooldown(&self) -> Result<(), Error> {
self.wait_until(|ctx| ctx.held_cooldown().is_none()).await
}
#[must_use]
pub fn open_window(&self) -> Option<i32> {
self.lock().snap.open_window
}
#[must_use]
pub fn entity_id(&self) -> i32 {
self.lock().snap.entity_id
}
#[must_use]
pub fn dimension(&self) -> String {
self.lock().snap.dimension.clone()
}
pub fn with<R, F>(&self, f: F) -> With<R>
where
R: Send + 'static,
F: FnOnce(&mut TickContext<'_>) -> R + Send + 'static,
{
let slot = Arc::new(Mutex::new(WithSlot {
result: None,
waker: None,
dropped: false,
}));
let mut guard = DropWake(Some(slot.clone()));
let req: Request = Box::new(move |ctx| {
let r = catch_unwind(AssertUnwindSafe(|| f(ctx)));
if let Some(slot) = guard.0.take() {
let mut s = lock(&slot);
s.result = Some(r);
if let Some(w) = s.waker.take() {
w.wake();
}
}
});
With {
bot: self.clone(),
session: self.lock().session,
req: Some(req),
slot,
}
}
pub fn act<F>(&self, f: F)
where
F: FnOnce(&mut TickContext<'_>) + Send + 'static,
{
let mut s = self.lock();
if s.closed.is_none() {
s.requests.push_back(Box::new(f));
}
}
pub fn wait_ticks(&self, n: u64) -> WaitTicks {
let s = self.lock();
WaitTicks {
bot: self.clone(),
session: s.session,
until: s.tick + n,
}
}
pub fn next_tick(&self) -> WaitTicks {
self.wait_ticks(1)
}
pub fn sleep(&self, d: std::time::Duration) -> WaitTicks {
let ms = d.as_millis();
self.wait_ticks(u64::try_from(ms.div_ceil(50)).unwrap_or(u64::MAX))
}
pub fn timeout<F: Future>(&self, ticks: u64, fut: F) -> Timeout<F> {
Timeout {
fut: Box::pin(fut),
wait: self.wait_ticks(ticks),
}
}
pub async fn wait_until<F>(&self, cond: F) -> Result<(), Error>
where
F: Fn(&TickContext<'_>) -> bool + Send + Sync + 'static,
{
let cond = Arc::new(cond);
loop {
let c = cond.clone();
if self.with(move |ctx| c(ctx)).await? {
return Ok(());
}
self.next_tick().await;
}
}
pub fn spawn<T, F>(&self, fut: F) -> JoinHandle<T>
where
T: Send + 'static,
F: Future<Output = T> + Send + 'static,
{
let slot = Arc::new(Mutex::new(JoinSlot {
result: None,
waker: None,
}));
let done = Completer(slot.clone());
let on_panic = slot.clone();
let hook: PanicHook = Box::new(move |msg| {
let mut s = lock(&on_panic);
if s.result.is_none()
|| matches!(&s.result, Some(Err(Error::Panicked(m))) if m.is_empty())
{
s.result = Some(Err(Error::Panicked(msg)));
if let Some(w) = s.waker.take() {
w.wake();
}
}
});
let wrapped = async move {
let v = fut.await;
done.complete(Ok(v));
};
let mut s = self.lock();
let id = s.next_task;
s.next_task += 1;
if s.closed.is_some() {
drop(s);
lock(&slot).result = Some(Err(Error::Disconnected));
} else {
s.spawned.push((id, Box::pin(wrapped), hook));
}
JoinHandle {
bot: self.clone(),
id,
slot,
}
}
pub fn require(&self, interests: Interests) {
let mut s = self.lock();
s.extra_interests |= interests;
}
pub(crate) fn mark(&self) -> Mark {
let s = self.lock();
Mark {
teleports: s.teleports,
deaths: s.deaths,
}
}
pub(crate) fn check(&self, m: Mark) -> Result<(), Error> {
let s = self.lock();
if s.closed.is_some() {
return Err(Error::Disconnected);
}
if s.deaths != m.deaths {
return Err(Error::Died);
}
if s.interrupt_on_teleport && s.teleports != m.teleports {
return Err(Error::Teleported);
}
Ok(())
}
pub(crate) fn check_alive(&self, m: Mark) -> Result<(), Error> {
let s = self.lock();
if s.closed.is_some() {
return Err(Error::Disconnected);
}
if s.deaths != m.deaths {
return Err(Error::Died);
}
Ok(())
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) struct Mark {
teleports: u64,
deaths: u64,
}
struct WithSlot<R> {
result: Option<std::thread::Result<R>>,
waker: Option<Waker>,
dropped: bool,
}
struct DropWake<R>(Option<Arc<Mutex<WithSlot<R>>>>);
impl<R> Drop for DropWake<R> {
fn drop(&mut self) {
if let Some(slot) = self.0.take() {
let mut s = lock(&slot);
s.dropped = true;
if let Some(w) = s.waker.take() {
w.wake();
}
}
}
}
#[must_use = "futures do nothing unless awaited"]
pub struct With<R> {
bot: Bot,
session: u64,
req: Option<Request>,
slot: Arc<Mutex<WithSlot<R>>>,
}
impl<R> std::fmt::Debug for With<R> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("With")
.field("sent", &self.req.is_none())
.finish()
}
}
impl<R> Future for With<R> {
type Output = Result<R, Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
if let Some(req) = this.req.take() {
let mut s = this.bot.lock();
if s.closed.is_some() || s.session != this.session {
return Poll::Ready(Err(Error::Disconnected));
}
lock(&this.slot).waker = Some(cx.waker().clone());
s.requests.push_back(req);
return Poll::Pending;
}
let mut slot = lock(&this.slot);
match slot.result.take() {
Some(Ok(v)) => Poll::Ready(Ok(v)),
Some(Err(panic)) => {
drop(slot);
std::panic::resume_unwind(panic)
}
None if slot.dropped => Poll::Ready(Err(Error::Disconnected)),
None => {
slot.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
}
#[must_use = "futures do nothing unless awaited"]
#[derive(Debug)]
pub struct WaitTicks {
bot: Bot,
session: u64,
until: u64,
}
impl Future for WaitTicks {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
let mut s = self.bot.lock();
if s.closed.is_some() || s.session != self.session || s.tick >= self.until {
return Poll::Ready(());
}
if !s.tick_wakers.iter().any(|w| w.will_wake(cx.waker())) {
s.tick_wakers.push(cx.waker().clone());
}
Poll::Pending
}
}
#[must_use = "futures do nothing unless awaited"]
pub struct Timeout<F> {
fut: Pin<Box<F>>,
wait: WaitTicks,
}
impl<F> std::fmt::Debug for Timeout<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Timeout")
.field("until", &self.wait.until)
.finish()
}
}
impl<F: Future> Future for Timeout<F> {
type Output = Result<F::Output, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if let Poll::Ready(v) = this.fut.as_mut().poll(cx) {
return Poll::Ready(Ok(v));
}
let closed = {
let s = this.wait.bot.lock();
s.closed.is_some() || s.session != this.wait.session
};
match Pin::new(&mut this.wait).poll(cx) {
Poll::Ready(()) if closed => Poll::Ready(Err(Error::Disconnected)),
Poll::Ready(()) => Poll::Ready(Err(Error::Timeout)),
Poll::Pending => Poll::Pending,
}
}
}
struct JoinSlot<T> {
result: Option<Result<T, Error>>,
waker: Option<Waker>,
}
struct Completer<T>(Arc<Mutex<JoinSlot<T>>>);
impl<T> Completer<T> {
fn complete(self, r: Result<T, Error>) {
let mut s = lock(&self.0);
s.result = Some(r);
if let Some(w) = s.waker.take() {
w.wake();
}
}
}
impl<T> Drop for Completer<T> {
fn drop(&mut self) {
let mut s = lock(&self.0);
if s.result.is_none() {
s.result = Some(Err(if std::thread::panicking() {
Error::Panicked(String::new())
} else {
Error::Cancelled
}));
if let Some(w) = s.waker.take() {
w.wake();
}
}
}
}
#[must_use = "dropping the handle detaches the task"]
pub struct JoinHandle<T> {
bot: Bot,
id: u64,
slot: Arc<Mutex<JoinSlot<T>>>,
}
impl<T> std::fmt::Debug for JoinHandle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JoinHandle").field("id", &self.id).finish()
}
}
impl<T> JoinHandle<T> {
pub fn abort(&self) {
self.bot.lock().aborted.push(self.id);
}
#[must_use]
pub fn is_finished(&self) -> bool {
lock(&self.slot).result.is_some()
}
}
impl<T> Future for JoinHandle<T> {
type Output = Result<T, Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut s = lock(&self.slot);
match s.result.take() {
Some(r) => Poll::Ready(r),
None => {
s.waker = Some(cx.waker().clone());
Poll::Pending
}
}
}
}
struct TaskWaker {
id: u64,
ready: Arc<Mutex<Vec<u64>>>,
}
impl Wake for TaskWaker {
fn wake(self: Arc<Self>) {
lock(&self.ready).push(self.id);
}
fn wake_by_ref(self: &Arc<Self>) {
lock(&self.ready).push(self.id);
}
}
struct Task {
id: u64,
fut: BoxFuture<()>,
waker: Waker,
on_panic: Option<PanicHook>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum WhenDone {
#[default]
Disconnect,
Stay,
}
pub struct Ext {
name: String,
factory: Factory,
interests: Interests,
when_done: WhenDone,
auto_respawn: bool,
interrupt_on_teleport: bool,
bot: Bot,
tasks: Vec<Task>,
ready: Arc<Mutex<Vec<u64>>>,
main: Option<JoinHandle<Result<(), Error>>>,
result: Option<Result<(), Error>>,
finished: bool,
started: bool,
}
impl std::fmt::Debug for Ext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Ext")
.field("name", &self.name)
.field("tasks", &self.tasks.len())
.field("result", &self.result)
.finish_non_exhaustive()
}
}
impl Ext {
pub fn new<F, Fut>(mut main: F) -> Self
where
F: FnMut(Bot) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), Error>> + Send + 'static,
{
Ext {
name: "ext".into(),
factory: Box::new(move |bot| Box::pin(main(bot))),
interests: Interests::CHAT,
when_done: WhenDone::Disconnect,
auto_respawn: false,
interrupt_on_teleport: true,
bot: Bot::new(),
tasks: Vec::new(),
ready: Arc::new(Mutex::new(Vec::new())),
main: None,
result: None,
finished: false,
started: false,
}
}
#[must_use]
pub fn name(mut self, name: impl Into<String>) -> Self {
self.name = name.into();
self
}
#[must_use]
pub fn interests(mut self, interests: Interests) -> Self {
self.interests = interests;
self
}
#[must_use]
pub fn when_done(mut self, when: WhenDone) -> Self {
self.when_done = when;
self
}
#[must_use]
pub fn auto_respawn(mut self, on: bool) -> Self {
self.auto_respawn = on;
self
}
#[must_use]
pub fn interrupt_on_teleport(mut self, on: bool) -> Self {
self.interrupt_on_teleport = on;
self
}
#[must_use]
pub fn bot(&self) -> Bot {
self.bot.clone()
}
#[must_use]
pub fn result(&self) -> Option<&Result<(), Error>> {
self.result.as_ref()
}
fn begin_session(&mut self, ctx: &TickContext<'_>) {
self.tasks.clear();
lock(&self.ready).clear();
self.result = None;
self.finished = false;
{
let mut s = self.bot.lock();
s.session += 1;
s.closed = None;
s.tick = 0;
s.requests.clear();
s.spawned.clear();
s.aborted.clear();
s.teleports = 0;
s.deaths = 0;
s.interrupt_on_teleport = self.interrupt_on_teleport;
s.snap.refresh(ctx);
}
let fut = (self.factory)(self.bot.clone());
self.main = Some(self.bot.spawn(fut));
}
fn end_session(&mut self, reason: &str) {
let mut s = self.bot.lock();
s.closed = Some(reason.to_owned());
let reqs: Vec<Request> = s.requests.drain(..).collect();
let spawned = std::mem::take(&mut s.spawned);
s.wake_ticks();
s.wake_subs();
drop(s);
drop(reqs);
drop(spawned);
self.tasks.clear();
}
fn run(&mut self, ctx: &mut TickContext<'_>) {
for _ in 0..MAX_ROUNDS {
let (spawned, aborted) = {
let mut s = self.bot.lock();
(
std::mem::take(&mut s.spawned),
std::mem::take(&mut s.aborted),
)
};
if !aborted.is_empty() {
self.tasks.retain(|t| !aborted.contains(&t.id));
}
for (id, fut, on_panic) in spawned {
let waker = Waker::from(Arc::new(TaskWaker {
id,
ready: self.ready.clone(),
}));
self.tasks.push(Task {
id,
fut,
waker,
on_panic: Some(on_panic),
});
lock(&self.ready).push(id);
}
let mut ready = std::mem::take(&mut *lock(&self.ready));
ready.sort_unstable();
ready.dedup();
for id in ready {
let Some(i) = self.tasks.iter().position(|t| t.id == id) else {
continue;
};
let t = &mut self.tasks[i];
let mut cx = Context::from_waker(&t.waker);
match catch_unwind(AssertUnwindSafe(|| t.fut.as_mut().poll(&mut cx))) {
Ok(Poll::Pending) => {}
Ok(Poll::Ready(())) => {
self.tasks.swap_remove(i);
}
Err(p) => {
let msg = panic_message(&*p);
log::error!("[{}] task {id} panicked: {msg}", self.name);
let mut t = self.tasks.swap_remove(i);
if let Some(h) = t.on_panic.take() {
h(msg);
}
}
}
}
let mut ran = false;
loop {
let r = self.bot.lock().requests.pop_front();
let Some(r) = r else { break };
ran = true;
r(ctx);
}
if ran {
self.bot.lock().snap.refresh(ctx);
}
let idle = lock(&self.ready).is_empty() && {
let s = self.bot.lock();
s.spawned.is_empty() && s.aborted.is_empty() && s.requests.is_empty()
};
if idle {
return;
}
}
log::debug!(
"[{}] t{} tasks still busy after {MAX_ROUNDS} rounds: continuing next tick",
self.name,
ctx.tick()
);
}
}
pub(crate) fn panic_message(p: &(dyn std::any::Any + Send)) -> String {
if let Some(s) = p.downcast_ref::<&str>() {
(*s).to_owned()
} else if let Some(s) = p.downcast_ref::<String>() {
s.clone()
} else {
"unknown panic".to_owned()
}
}
impl Behavior for Ext {
fn name(&self) -> &str {
&self.name
}
fn interests(&self) -> Interests {
self.interests | self.bot.lock().extra_interests
}
fn start(&mut self, ctx: &mut TickContext<'_>) {
if self.started {
self.end_session("new session");
}
self.started = true;
self.begin_session(ctx);
}
fn tick(&mut self, ctx: &mut TickContext<'_>) {
{
let mut s = self.bot.lock();
s.tick += 1;
s.snap.refresh(ctx);
let ev = ctx.events();
s.teleports += ev.teleports.len() as u64;
if ev.died {
s.deaths += 1;
}
s.publish(ctx);
s.wake_ticks();
}
if self.auto_respawn && ctx.is_dead() {
ctx.respawn();
}
self.run(ctx);
if self.result.is_none()
&& let Some(h) = &self.main
&& h.is_finished()
{
let r = lock(&h.slot).result.take();
let r = match r {
Some(Ok(r)) => r,
Some(Err(e)) => Err(e),
None => Err(Error::Cancelled),
};
self.main = None;
match (&r, self.when_done) {
(Ok(()), WhenDone::Disconnect) => ctx.disconnect("done"),
(Err(e), WhenDone::Disconnect) => ctx.disconnect(format!("failed: {e}")),
(Ok(()), WhenDone::Stay) => {
log::info!("[{}] main task done", self.name);
}
(Err(e), WhenDone::Stay) => log::warn!("[{}] main task failed: {e}", self.name),
}
self.result = Some(r);
}
if self.result.is_some() && self.when_done == WhenDone::Stay && self.tasks.is_empty() {
self.finished = true;
}
}
fn running(&self) -> bool {
!self.finished
}
fn stop(&mut self, reason: &str) {
self.end_session(reason);
if self.result.is_none() && self.main.take().is_some() {
self.result = Some(Err(Error::Disconnected));
}
self.started = false;
}
}