From f6a1deb1e5c415bf792a56657c9002156fb0188d Mon Sep 17 00:00:00 2001 From: Loudbook Date: Thu, 1 Feb 2024 16:40:02 -0500 Subject: [PATCH] External Java args file --- src/downloaders/forge.rs | 2 + src/downloaders/neoforge.rs | 2 + src/main.rs | 87 +++++++++++++++++++++++++++++-------- 3 files changed, 73 insertions(+), 18 deletions(-) diff --git a/src/downloaders/forge.rs b/src/downloaders/forge.rs index a445767..c8ce3d6 100644 --- a/src/downloaders/forge.rs +++ b/src/downloaders/forge.rs @@ -95,6 +95,8 @@ pub async fn build_server(java_path: String, mut minecraft_version: Option) -> Result> { diff --git a/src/downloaders/neoforge.rs b/src/downloaders/neoforge.rs index 5dab9e0..694f0d6 100644 --- a/src/downloaders/neoforge.rs +++ b/src/downloaders/neoforge.rs @@ -62,6 +62,8 @@ pub async fn build_server(java_path: String, mut minecraft_version: Option().unwrap() != 5 { + if num != 5 { create_launch_script(Some(java_path.as_str()), os, 3); + } else { + create_args_file(3); } println!(); @@ -213,19 +215,19 @@ async fn main() { } } -fn change_ram(os: &str) { +fn change_ram() { print!("Enter the amount of RAM you want to allocate to the server in gigabytes: "); let mut ram_input = user_input(); while ram_input.parse::().is_err() || ram_input.parse::().unwrap() < 1 { - println!("Please enter a valid number."); + print!("Please enter a valid number: "); ram_input = user_input(); } let ram = ram_input.parse::().expect("Failed to parse RAM"); - create_launch_script(None, os, ram); + create_args_file(ram); } fn goodbye() { @@ -257,21 +259,32 @@ fn create_launch_script(java_path: Option<&str>, os: &str, ram: i32) { match java_path { None => { let original_content = fs::read_to_string(file_name).expect("Failed to read launch file"); - let original_java_path = original_content.split_whitespace().collect::>()[0]; + let original_java_path = original_content.lines() + .filter(|line| !line.starts_with('#') && !line.starts_with("REM" ) && !line.starts_with('@')) + .collect::>().first().unwrap().split_whitespace().collect::>()[0]; fs::remove_file(file_name).expect("Failed to remove launch file"); let file = File::create(file_name).unwrap(); let mut file = BufWriter::new(file); - file.write_all( - format!( - "{} -Xms1024M -Xmx{}G -jar server.jar \npause", - original_java_path, - ram - ) - .as_bytes(), - ).expect("Failed to write to launch file"); + if os == "windows" { + file.write_all( + format!( + "@echo off\n\"{}\" @user_jvm_args.txt -jar server.jar", + original_java_path.replace('"', "") + ) + .as_bytes(), + ).expect("Failed to write to launch file"); + } else { + file.write_all( + format!( + "#!#!/usr/bin/env sh\n\"{}\" @user_jvm_args.txt -jar server.jar", + original_java_path.replace('"', "") + ) + .as_bytes(), + ).expect("Failed to write to launch file"); + } } Some(java_path) => { let file = File::create(file_name).unwrap(); @@ -279,16 +292,17 @@ fn create_launch_script(java_path: Option<&str>, os: &str, ram: i32) { file.write_all( format!( - "\"{}\" -Xms1024M -Xmx{}G -jar server.jar \npause", + "\"{}\" @user_jvm_args.txt -jar server.jar", java_path, - ram ) .as_bytes(), ) - .expect("Failed to write to launch file") + .expect("Failed to write to launch file"); } } + create_args_file(ram); + if os != "windows" { Command::new("chmod") .arg("+x") @@ -300,6 +314,43 @@ fn create_launch_script(java_path: Option<&str>, os: &str, ram: i32) { println!("Launch script was created!"); } +fn create_args_file(ram: i32) { + match File::open("user_jvm_args.txt") { + Ok(_) => { + let mut file = File::open("user_jvm_args.txt").expect("Failed to open user_jvm_args.txt"); + let mut content = String::new(); + file.read_to_string(&mut content).expect("Failed to read user_jvm_args.txt"); + + let new_script = if content.contains("-Xmx") { + content.lines().collect::>().iter().map(|s| { + if s.contains("-Xmx") && !s.starts_with('#') { + format!("-Xmx{}G", ram) + } else { + s.to_string() + } + }).collect::>().join("\n") + } else { + format!("{} -Xms1024M -Xmx{}G", content, ram) + }; + + fs::write("user_jvm_args.txt", new_script).expect("Failed to write to user_jvm_args.txt"); + } + Err(_) => { + let file = File::create("user_jvm_args.txt").unwrap(); + let mut file = BufWriter::new(file); + + file.write_all( + format!( + "-Xms1024M -Xmx{}G", + ram + ) + .as_bytes(), + ) + .expect("Failed to write to user_jvm_args.txt"); + } + }; +} + async fn accept_eula() { println!("Checking EULA..."); let file = File::create("./eula.txt").unwrap(); @@ -347,7 +398,7 @@ async fn run_launch_file(os: &str) { }; let args_no_comments = content.lines().filter( - |line| !line.starts_with('#')).collect::>(); + |line| !line.starts_with('#') && !line.starts_with("REM") && !line.starts_with('@')).collect::>(); let java_path = &args_no_comments.clone().first().unwrap().split_whitespace().collect::>()[0].replace('"', ""); let args = &args_no_comments.first().unwrap().split_whitespace().collect::>()[1..];