diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..35049cb --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[alias] +xtask = "run --package xtask --" diff --git a/Cargo.toml b/Cargo.toml index 1dff403..4c2f15c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = [ "gameserver", "proto", "sdkserver"] +members = ["gameserver", "proto", "sdkserver", "xtask"] resolver = "2" [workspace.package] diff --git a/README.md b/README.md index 3008b1c..6571709 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,10 @@ A Server emulator for the game [`Honkai: Star Rail`](https://hsr.hoyoverse.com/e - [Rust](https://www.rust-lang.org/tools/install) -**NOTE**: Nightly Rust is required to build the project. To install it, first install -Rust itself, then run the following command: - -```sh -rustup toolchain install nightly -rustup default nightly -``` - #### Building +##### Manually + ```sh git clone https://git.xeondev.com/reversedrooms/RobinSR.git cd RobinSR @@ -28,6 +22,18 @@ cargo install --path gameserver cargo install --path sdkserver ``` +##### Using xtasks (use this if stupid) + +```sh +cargo xtask run +``` + +##### To run it with automatic recompilation when any Rust files are changed + +```sh +cargo xtask watch +``` + ### From Pre-built Binaries Navigate to the [Releases](https://git.xeondev.com/reversedrooms/RobinSR/releases) @@ -55,8 +61,10 @@ run the following in a terminal: ``` ## Connecting + [Get 2.2 beta client](https://bhrpg-prod.oss-accelerate.aliyuncs.com/client/beta/20240322124944_scfGE0xJXlWtoJ1r/StarRail_2.1.51.zip), -replace [mhypbase.dll](https://git.xeondev.com/reversedrooms/RobinSR/raw/branch/master/mhypbase.dll) file in your game folder, it will redirect game traffic (and also disable in-game censorship) +replace [mhypbase.dll](https://git.xeondev.com/reversedrooms/RobinSR/raw/branch/master/mhypbase.dll) +file in your game folder, it will redirect game traffic (and disable in-game censorship) ## Contributing diff --git a/gameserver/src/net/handlers/lineup.rs b/gameserver/src/net/handlers/lineup.rs index d9c3255..3b08258 100644 --- a/gameserver/src/net/handlers/lineup.rs +++ b/gameserver/src/net/handlers/lineup.rs @@ -137,7 +137,8 @@ pub async fn on_quit_lineup_cs_req(session: &PlayerSession, body: &QuitLineupCsR .await } -fn lineup_avatar(id: u32, slot: u32) -> LineupAvatar { +#[must_use] +const fn lineup_avatar(id: u32, slot: u32) -> LineupAvatar { LineupAvatar { id, slot, diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 0000000..c158411 --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "xtask" +edition = "2021" +version.workspace = true + +[dependencies] +notify = "6.1.1" diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 0000000..3ea8253 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,95 @@ +use std::{io, process::Command, sync::mpsc, thread}; + +fn print_help() { + println!( + " +xtask must specify a task to run. + +Usage: `cargo xtask ` + +Tasks: + run + Run the gameserver and sdkserver. + watch + Watch for changes in the project and restart the servers if any file changes. +" + ); +} + +// run gameserver and sdkserver, wait till any of them exit +fn spawn_servers(release: bool) -> Result<(), Box> { + let (tx, rx) = mpsc::channel(); + + let tx1 = tx.clone(); + let handle1 = thread::spawn(move || { + let mut gameserver = Command::new("cargo") + .arg("run") + .arg("--bin") + .arg("gameserver") + .args(if release { vec!["--release"] } else { vec![] }) + .spawn() + .expect("failed to start gameserver"); + + gameserver.wait()?; + tx1.send(()).expect("failed to send completion signal"); + + Ok::<(), io::Error>(()) + }); + + let handle2 = thread::spawn(move || { + let mut sdkserver = Command::new("cargo") + .arg("run") + .arg("--bin") + .arg("sdkserver") + .args(if release { vec!["--release"] } else { vec![] }) + .spawn() + .expect("failed to start sdkserver"); + + let _ = sdkserver.wait()?; + tx.send(()).expect("failed to send completion signal"); + + Ok::<(), io::Error>(()) + }); + + rx.recv().expect("failed to receive from channel"); + + handle1.join().expect("failed to join gameserver thread")?; + handle2.join().expect("failed to join sdkserver thread")?; + + Ok(()) +} + +// watch for changes in the project and restart the servers if any file changes +fn watch(release: bool) -> Result<(), Box> { + let mut cmd = std::process::Command::new("cargo"); + cmd.arg("watch").arg("-x").arg(format!( + "xtask run {}", + if release { "--release" } else { "" } + )); + + let mut child = cmd.spawn()?; + + child.wait()?; + + Ok(()) +} + +fn main() -> Result<(), Box> { + let Some(task) = std::env::args().nth(1) else { + print_help(); + std::process::exit(0); + }; + + let release = std::env::args().any(|arg| arg == "--release"); + + match task.as_str() { + "run" => spawn_servers(release)?, + "watch" => watch(release)?, + _ => { + println!("invalid task: `{task}`, run `cargo xtask` for a list of tasks"); + std::process::exit(1); + } + } + + Ok(()) +}