-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
joiner start + virtual fs testing server
- Loading branch information
Showing
11 changed files
with
262 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use super::{SyncVarintRead, SyncVarintWrite}; | ||
use std::io::{Read, Write}; | ||
|
||
impl<T: Read + ?Sized> SyncVarintRead for T { | ||
type Error = tokio::io::Error; | ||
|
||
fn read_varint(&mut self) -> Result<i32, Self::Error> { | ||
Ok(self.read_varint_len()?.1) | ||
} | ||
|
||
fn read_varint_len(&mut self) -> Result<(u32, i32), Self::Error> { | ||
let mut buf = [0u8]; | ||
let mut res = 0; | ||
let mut count = 0u32; | ||
|
||
loop { | ||
self.read_exact(&mut buf)?; | ||
res |= (buf[0] as i32 & (0b0111_1111_i32)) | ||
.checked_shl(7 * count) | ||
.ok_or(tokio::io::ErrorKind::Other)?; | ||
|
||
count += 1; | ||
if count > 5 { | ||
break Err(tokio::io::ErrorKind::Other.into()); | ||
} else if (buf[0] & (0b1000_0000_u8)) == 0 { | ||
break Ok((count, res)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T: Write + ?Sized> SyncVarintWrite for T { | ||
type Error = tokio::io::Error; | ||
|
||
fn write_varint(&mut self, varint: i32) -> Result<(), Self::Error> { | ||
let mut buffer = [0]; | ||
let mut value = varint; | ||
|
||
if value == 0 { | ||
self.write_all(&buffer)?; | ||
} | ||
|
||
while value != 0 { | ||
buffer[0] = (value & 0b0111_1111) as u8; | ||
value = (value >> 7) & (i32::max_value() >> 6); | ||
if value != 0 { | ||
buffer[0] |= 0b1000_0000; | ||
} | ||
self.write_all(&buffer)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "ram_server" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
config = { workspace = true } | ||
semver = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{ | ||
fs, | ||
io::{BufRead, BufReader}, | ||
process::{Child, Command, Stdio}, | ||
}; | ||
|
||
pub fn run_server(version: impl ToString, port: u16, online_mode: bool) -> Option<Child> { | ||
let config = config::get(); | ||
let version = version.to_string(); | ||
let java_path = { | ||
let version = semver::Version::parse(&version).expect("Unable to parse version"); | ||
match version.minor { | ||
..=17 => &config.ram_server.java_8_path, | ||
18.. => &config.ram_server.java_17_path, | ||
#[allow(unreachable_patterns)] | ||
_ => &config.ram_server.java_11_path, | ||
} | ||
}; | ||
run_setup(); | ||
{ | ||
let mut from_path = config.ram_server.server_jar_path.clone().into_os_string(); | ||
from_path.push(format!("{version}.jar")); | ||
let mut to_path = config.ram_server.temp_fs_path.clone().into_os_string(); | ||
to_path.push(format!("{version}.jar")); | ||
fs::copy(from_path, to_path).expect("Unable to copy server jar to temp dir"); | ||
} | ||
{ | ||
let mut eula_path = config.ram_server.temp_fs_path.clone().into_os_string(); | ||
eula_path.push("eula.txt"); | ||
fs::write(eula_path, b"eula=true").expect("Unable to write to eula file"); | ||
} | ||
{ | ||
let mut server_properties_path = config.ram_server.temp_fs_path.clone().into_os_string(); | ||
server_properties_path.push("server.properties"); | ||
fs::write( | ||
server_properties_path, | ||
format!("server-port={port}\nonline-mode={online_mode}"), | ||
) | ||
.expect("Unable to write to server properties file"); | ||
} | ||
|
||
let mut res = Command::new(java_path) | ||
.current_dir(&config.ram_server.temp_fs_path) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::null()) | ||
.stdin(Stdio::null()) | ||
.arg("-jar") | ||
.arg(format!("{version}.jar")) | ||
.arg("--nogui") | ||
.spawn() | ||
.expect("Could not start process"); | ||
|
||
let done_str = "[Server thread/INFO]: Done"; | ||
let stdout = res.stdout.take().expect("Could not take stdout"); | ||
|
||
let reader = BufReader::new(stdout); | ||
let mut lines = reader.lines(); | ||
while let Some(Ok(line)) = lines.next() { | ||
println!("{line}"); | ||
if line.contains(done_str) { | ||
return Some(res); | ||
} | ||
} | ||
|
||
res.kill().expect("Count not kill child process"); | ||
None | ||
} | ||
|
||
fn run_setup() { | ||
let path = &config::get().ram_server.temp_fs_path; | ||
fs::remove_dir_all(path).expect("Unable to delete old temp dir"); | ||
fs::create_dir(path).expect("Unable to create temp dir"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
fn main() { | ||
println!("Hello, World!"); | ||
let mut handle = | ||
ram_server::run_server("1.20.4-paper", 25565, true).expect("Could not start server :<"); | ||
println!("server started!"); | ||
handle.kill().expect("Unable to kill server process"); | ||
} |