Skip to content

Commit

Permalink
Configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
Loudbooks committed Feb 3, 2024
1 parent caf0d9b commit 7d00ebe
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 10 deletions.
40 changes: 40 additions & 0 deletions src/downloaders/geyser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

use async_trait::async_trait;
use crate::downloader::{basic_server_address_from_string, download_file, Installer};
use crate::servertype::ServerType;
use crate::servertype::ServerType::Server;

pub(crate) struct Geyser {}

#[async_trait]
impl Installer for Geyser {
fn get_name(&self) -> String {
"Geyser".to_string()
}

fn get_description(&self) -> String {
"A server that support Bedrock <-> Java crossplay..".to_string()
}

fn get_type(&self) -> ServerType {
Server
}

fn custom_script(&self) -> bool {
false
}

fn version_required(&self) -> bool {
false
}

async fn startup_message(&self, string: String) -> Option<std::net::SocketAddrV4> {
basic_server_address_from_string(string).await
}

async fn download(&self, client: reqwest::Client, _minecraft_version: Option<String>) -> Result<String, crate::downloaderror::DownloadError> {
download_file(&client, "https://download.geysermc.org/v2/projects/geyser/versions/latest/builds/latest/downloads/standalone", "./server.jar").await?;

Ok("".to_string())
}
}
1 change: 1 addition & 0 deletions src/downloaders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub(crate) mod neoforge;
pub(crate) mod bungeecord;
pub(crate) mod velocity;
pub(crate) mod waterfall;
pub(crate) mod geyser;
6 changes: 2 additions & 4 deletions src/downloaders/velocity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::error::Error;
use std::net::SocketAddrV4;
use async_trait::async_trait;
use reqwest::Client;
use crate::downloader::{basic_proxy_address_from_string, basic_server_address_from_string, download_file, Installer};
use crate::downloader::{basic_proxy_address_from_string, download_file, Installer};
use crate::downloaderror::DownloadError;
use crate::servertype::ServerType;
use crate::servertype::ServerType::{Proxy, Server};
use crate::servertype::ServerType::Proxy;

pub(crate) struct Velocity {}

Expand Down Expand Up @@ -38,9 +38,7 @@ impl Installer for Velocity {

async fn download(&self, client: Client, _minecraft_version: Option<String>) -> Result<String, DownloadError> {
let velocity_version = get_latest_velocity_version().await.expect("Failed to get latest velocity version");
println!("{}", velocity_version);
let latest_build = get_latest_build(&velocity_version).await.expect("Failed to get latest velocity build");
println!("{}", latest_build);

println!(
"Using Velocity version {} with build {}.",
Expand Down
125 changes: 119 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::downloader::Installer;
use crate::downloaders::fabric::Fabric;
use crate::downloaders::bungeecord::BungeeCord;
use crate::downloaders::forge::Forge;
use crate::downloaders::geyser::Geyser;
use crate::downloaders::java::download_java;
use crate::downloaders::neoforge::NeoForge;
use crate::downloaders::paper::Paper;
Expand All @@ -40,9 +41,10 @@ async fn main() {
Box::new(Fabric {}),
Box::new(Forge {}),
Box::new(NeoForge {}),
Box::new(Geyser {}),
Box::new(BungeeCord {}),
Box::new(Velocity {}),
Box::new(Waterfall {})
Box::new(Waterfall {}),
];

let is_arm = env::consts::ARCH.contains("arch64") || env::consts::ARCH.contains("arm");
Expand Down Expand Up @@ -132,7 +134,7 @@ async fn main() {
change_ram();
continue
} else if num == 3 {
change_port();
change_port(server_object);
continue
} else if num == 4 && fs::remove_file("./server.jar").is_ok() {
println!("Server file was removed.");
Expand Down Expand Up @@ -179,10 +181,10 @@ async fn main() {
let num = server_type.parse::<i32>().expect("Failed to parse server type");
let server_object = downloaders.get((num - 1) as usize).expect("Failed to get server object");

println!();
print!("What version of Minecraft do you want to run? Type latest for the latest version: ");

let minecraft_version = if server_object.version_required() {
println!();
print!("What version of Minecraft do you want to run? Type latest for the latest version: ");

let input = user_input();

if input == "latest" {
Expand Down Expand Up @@ -488,7 +490,118 @@ async fn run_launch_file(os: &OS, server: &dyn Installer) {
wait_for_enter("continue");
}

fn change_port() {
fn change_port(server: &dyn Installer) {
if server.get_name() == "Geyser" {
let config = fs::read_to_string("./config.yml").expect("config.yml not found. Make sure you have run the server at least once!");
let config = config.split('\n').collect::<Vec<&str>>();

let mut new_config: Vec<String> = Vec::new();

let mut section = "Bedrock";

for line in config {
if line.starts_with(" port:") {
print!("Enter the new port you want to use for {}: ", section);

let mut new_port = user_input();

while new_port.parse::<i32>().is_err() || new_port.parse::<i32>().unwrap() < 1 || new_port.parse::<i32>().unwrap() > 65535 {
print!("Please enter a valid port: ");
new_port = user_input();
}

let host_string = format!(" port: {}", new_port);
let final_string = host_string;

new_config.push(final_string);

section = "Java";
} else {
new_config.push(line.to_string());
}
}

let file = File::create("./config.yml").expect("Failed to create config.yml");
let mut file = BufWriter::new(file);

for mut line in new_config {
line += "\n";
file.write_all(line.as_bytes()).expect("Failed to write to config.yml");
}

return
} else if server.get_name() == "BungeeCord" || server.get_name() == "Waterfall" {
let config = fs::read_to_string("./config.yml").expect("config.yml not found. Make sure you have run the server at least once!");
let config = config.split('\n').collect::<Vec<&str>>();

let mut new_config: Vec<String> = Vec::new();

print!("Enter the new port you want to use: ");

let mut new_port = user_input();

while new_port.parse::<i32>().is_err() || new_port.parse::<i32>().unwrap() < 1 || new_port.parse::<i32>().unwrap() > 65535 {
print!("Please enter a valid port: ");
new_port = user_input();
}

for line in config {
if line.starts_with(" host:") {
let host_string = format!(" host: 0.0.0.0:{}", new_port);
let final_string = host_string;

new_config.push(final_string);
} else {
new_config.push(line.to_string());
}
}

let file = File::create("./config.yml").expect("Failed to create config.yml");
let mut file = BufWriter::new(file);

for mut line in new_config {
line += "\n";
file.write_all(line.as_bytes()).expect("Failed to write to config.yml");
}

return
} else if server.get_name() == "Velocity" {
let toml = fs::read_to_string("./velocity.toml").expect("velocity.toml not found. Make sure you have run the server at least once!");
let toml = toml.split('\n').collect::<Vec<&str>>();

let mut new_toml: Vec<String> = Vec::new();

print!("Enter the new port you want to use: ");

let mut new_port = user_input();

while new_port.parse::<i32>().is_err() || new_port.parse::<i32>().unwrap() < 1 || new_port.parse::<i32>().unwrap() > 65535 {
print!("Please enter a valid port: ");
new_port = user_input();
}

for line in toml {
if line.starts_with("bind") {
let bind_string = format!("bind = \"0.0.0.0:{}\"", new_port);
let final_string = bind_string;

new_toml.push(final_string);
} else {
new_toml.push(line.to_string());
}
}

let file = File::create("./velocity.toml").expect("Failed to create velocity.toml");
let mut file = BufWriter::new(file);

for mut line in new_toml {
line += "\n";
file.write_all(line.as_bytes()).expect("Failed to write to velocity.toml");
}

return
}

let file = File::open("./server.properties").expect("Failed to open server.properties");
let mut file = BufReader::new(file);

Expand Down

0 comments on commit 7d00ebe

Please sign in to comment.