Skip to content

Commit

Permalink
Move ssw V2 behind v2 feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dablenparty committed Dec 16, 2023
1 parent a1d0f66 commit 3f3cc0d
Show file tree
Hide file tree
Showing 22 changed files with 210 additions and 173 deletions.
20 changes: 4 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
# Generated by Cargo
# will have compiled files and executables
/target/
/.idea
/.vscode
/target
/mc-server

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.fleet/
*.iml
.idea/
.vscode/
mc-server*/
ssw-logs/
.env
34 changes: 6 additions & 28 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
[package]
name = "ssw"
version = "2.0.1"
version = "3.0.0"
edition = "2021"
default-run = "ssw"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "release_script"
test = false
bench = false

[dependencies]
async-trait = "0.1.68"
chrono = { version = "0.4.24", features = ["serde"] }
clap = { version = "4.4", features = ["derive"] }
dirs = "5.0.0"
dunce = "1.0.4"
duration-string = "0.3.0"
flate2 = "1.0.25"
getset = "0.1.2"
java-properties = "2.0.0"
log = "0.4.17"
mcping = { git = "https://github.com/Scetch/mcping", features = ["tokio-runtime"] }
reqwest = "0.11.17"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
simplelog = { version = "0.12.1", features = ["paris"] }
thiserror = "1.0.40"
ssw = { version = "2.0.1", path = "./v2", optional = true }
tokio = { version = "1", features = ["full"] }
tokio-util = "0.7.8"
toml = "0.8.0"
uuid = "1.3.3"
which = "4.4.0"
zip = "0.6.5"

[features]
default = []
v2 = ["ssw"]
136 changes: 7 additions & 129 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,134 +1,12 @@
#![warn(clippy::all, clippy::pedantic)]
// the same thing has different names on windows and unix for some reason
#![allow(clippy::module_name_repetitions)]

use clap::Parser;
use log::{debug, error, info, warn};
use tokio::{io::AsyncBufReadExt, select, sync::mpsc::Sender, task::JoinHandle};
use tokio_util::sync::CancellationToken;

use minecraft::{begin_server_task, ServerTaskRequest};

use crate::{
commandline::CommandLineArgs, logging::init_logging, minecraft::manifest::VersionManifestV2,
};

mod commandline;
mod config;
mod logging;
mod minecraft;

#[cfg(feature = "v2")]
#[tokio::main]
async fn main() {
let args = CommandLineArgs::parse();
if let Err(e) = init_logging(args.log_level) {
eprintln!("Failed to initialize logging: {e}");
eprintln!("Debug info: {e:?}");
std::process::exit(1);
}
debug!("Parsed args: {args:?}");
if args.refresh_manifest {
if let Err(e) = VersionManifestV2::refresh_launcher_manifest().await {
error!("Failed to refresh version manifest: {e}");
} else {
info!("Refreshed version manifest");
}
}
let jar_path = args.server_jar_path;
let server_token = CancellationToken::new();
let (running_tx, running_rx) = tokio::sync::mpsc::channel(1);
let (server_handle, server_sender) =
begin_server_task(jar_path, running_tx, server_token.clone());
let stdin_token = CancellationToken::new();
let stdin_handle = begin_stdin_task(server_sender, running_rx, stdin_token);
let ssw_version = env!("CARGO_PKG_VERSION");
println!("SSW Console v{ssw_version}");
stdin_handle.await.unwrap_or_else(|e| {
error!("Error waiting on stdin task: {e}");
});
let handles = vec![(server_handle, server_token)];
for (handle, token) in handles {
token.cancel();
if let Err(e) = handle.await {
error!("Error waiting on child task: {e}");
}
}
use ssw_v2::v2_main;
v2_main().await
}

fn begin_stdin_task(
server_sender: Sender<ServerTaskRequest>,
mut running_rx: tokio::sync::mpsc::Receiver<bool>,
token: CancellationToken,
) -> JoinHandle<()> {
tokio::spawn(async move {
let stdin = tokio::io::stdin();
let mut stdin_reader = tokio::io::BufReader::new(stdin).lines();
loop {
let line = select! {
r = stdin_reader.next_line() => {
if let Some(line) = r.unwrap_or_else(|e| {
error!("Error reading from stdin: {e}");
None
}) { line }
else {
warn!("Stdin closed or errored");
break;
}

}
() = token.cancelled() => {
debug!("Stdin task cancelled");
break;
}
};

match line.as_str() {
"start" => {
if let Err(e) = server_sender.send(ServerTaskRequest::Start).await {
error!("Error sending start request: {e}");
}
}
"stop" => {
if let Err(e) = server_sender.send(ServerTaskRequest::Stop).await {
error!("Error sending stop request: {e}");
}
}
"exit" => {
// kill server
if let Err(e) = server_sender.send(ServerTaskRequest::Kill).await {
error!("Error sending kill request: {e}");
}
info!("Exiting, expect some errors as the server is killed");
break;
}
"help" => print_help(),
_ => {
server_sender
.send(ServerTaskRequest::IsRunning)
.await
.unwrap_or_else(|e| {
error!("Error sending is_running request: {e}");
});
if !running_rx.recv().await.unwrap_or(false) {
warn!("Unknown command: {line}");
continue;
}
if let Err(e) = server_sender.send(ServerTaskRequest::Command(line)).await {
error!("Error sending command to server: {e}");
}
}
}
}
info!("Finished reading from stdin");
})
}

/// Print the help message
fn print_help() {
println!("SSW Help:");
println!(" start - start the server");
println!(" stop - stop the server");
println!(" exit - stop the server and exit");
println!(" help - print this help message");
println!(" anything else - send the command to the server");
#[cfg(not(feature = "v2"))]
#[tokio::main]
async fn main() {
println!("This binary was not compiled with the v2 feature enabled");
}
18 changes: 18 additions & 0 deletions v2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

.fleet/
*.iml
.idea/
.vscode/
mc-server*/
ssw-logs/
.env
42 changes: 42 additions & 0 deletions v2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "ssw"
version = "2.0.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "release_script"
test = false
bench = false

[lib]
name = "ssw_v2"
path = "src/lib.rs"
test = false
bench = false
doctest = false

[dependencies]
async-trait = "0.1.68"
chrono = { version = "0.4.24", features = ["serde"] }
clap = { version = "4.4", features = ["derive"] }
dirs = "5.0.0"
dunce = "1.0.4"
duration-string = "0.3.0"
flate2 = "1.0.25"
getset = "0.1.2"
java-properties = "2.0.0"
log = "0.4.17"
mcping = { git = "https://github.com/Scetch/mcping", features = ["tokio-runtime"] }
reqwest = "0.11.17"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0.96"
simplelog = { version = "0.12.1", features = ["paris"] }
thiserror = "1.0.40"
tokio = { version = "1", features = ["full"] }
tokio-util = "0.7.8"
toml = "0.8.0"
uuid = "1.3.3"
which = "4.4.0"
zip = "0.6.5"
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3f3cc0d

Please sign in to comment.