Skip to content

Commit

Permalink
Spin up Tokio to make async in main() work
Browse files Browse the repository at this point in the history
  • Loading branch information
RubberDuckShobe committed Apr 27, 2024
1 parent ae006aa commit 6159d7f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
1 change: 0 additions & 1 deletion wavebreaker_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ bass-sys = "2.3.0"
rfd = "0.14.1"
octocrab = "0.38.0"
semver = "1.0.22"
pollster = "0.3.0"
tokio = { version = "1.37.0", features = ["full"] }

[build-dependencies]
Expand Down
51 changes: 27 additions & 24 deletions wavebreaker_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use figment::{
providers::{Env, Format, Toml},
Figment,
};
use pollster::FutureExt;
use tracing::{debug, error, info};
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};
Expand All @@ -36,7 +35,7 @@ use windows::{

use crate::hooking::{deinit_hooks, init_hooks};

unsafe fn main() -> anyhow::Result<()> {
async unsafe fn main() -> anyhow::Result<()> {
let file_appender = RollingFileAppender::builder()
.rotation(Rotation::DAILY)
.filename_prefix("wavebreaker_client")
Expand Down Expand Up @@ -64,7 +63,7 @@ unsafe fn main() -> anyhow::Result<()> {
let release = repo
.releases()
.get_latest()
.block_on()
.await
.context("Failed to get releases from repo")?;
let tag_version =
semver::Version::parse(&release.tag_name).context("Failed to parse tag")?;
Expand All @@ -75,7 +74,7 @@ unsafe fn main() -> anyhow::Result<()> {
);
if tag_version > current_version {
info!("New version {} available!", tag_version);
std::process::exit(0); //just brutally kill the game
std::process::exit(0); //just kill the game
}
}

Expand Down Expand Up @@ -118,27 +117,31 @@ unsafe extern "system" fn DllMain(hinst: HMODULE, reason: u32, _reserved: *mut c
&mut handle as *mut HMODULE,
);

// TODO: Use WinAPI thread functions directly and properly clean up on detach!
// TODO: Properly clean up on detach!
thread::spawn(|| {
match main() {
Ok(_) => (),
Err(e) => {
error!("{:?}", e);
let error_cstr = CString::new(format!(
"{:?}\r\nThe client will be unloaded.\r\nPlease report this issue!",
e
))
.unwrap();
let error_pcstr = PCSTR::from_raw(error_cstr.as_bytes_with_nul().as_ptr());

MessageBoxA(
HWND(0),
error_pcstr,
s!("Wavebreaker client fatal error"),
MB_OK | MB_ICONERROR,
);
}
};
let rt = tokio::runtime::Runtime::new().expect("Failed to create runtime");

rt.block_on(async {
match main().await {
Ok(_) => (),
Err(e) => {
error!("{:?}", e);
let error_cstr = CString::new(format!(
"{:?}\r\nThe client will be unloaded.\r\nPlease report this issue!",
e
))
.unwrap();
let error_pcstr = PCSTR::from_raw(error_cstr.as_bytes_with_nul().as_ptr());

MessageBoxA(
HWND(0),
error_pcstr,
s!("Wavebreaker client fatal error"),
MB_OK | MB_ICONERROR,
);
}
};
});
});
}

Expand Down

0 comments on commit 6159d7f

Please sign in to comment.