Skip to content

Commit

Permalink
Convert to MacOS, enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Loudbooks committed Feb 2, 2024
1 parent 43a579f commit 23bd13c
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 46 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ A program to download any version of your favorite Minecraft server distribution
- Double click the executable file.

## MacOS
- Run `chmod +x ./server-installer-osx-x86_64` or `chmod +x ./server-installer-osx-aarch64`
- Run `./server-installer-osx-x86_64` or `./server-installer-osx-aarch64`
- Run `chmod +x ./server-installer-macos-x86_64` or `chmod +x ./server-installer-macos-aarch64`
- Run `./server-installer-macos-x86_64` or `./server-installer-macos-aarch64`

## Linux
- Run `chmod +x ./server-installer-linux-x86_64` or `chmod +x ./server-installer-linux-aarch64`
Expand Down
4 changes: 2 additions & 2 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cargo build --release --target x86_64-pc-windows-gnu

mkdir target/out

cp target/x86_64-apple-darwin/release/minecraft_server_installer target/out/server-installer-osx-x86_64
cp target/aarch64-apple-darwin/release/minecraft_server_installer target/out/server-installer-osx-aarch64
cp target/x86_64-apple-darwin/release/minecraft_server_installer target/out/server-installer-macos-x86_64
cp target/aarch64-apple-darwin/release/minecraft_server_installer target/out/server-installer-macos-aarch64
cp target/x86_64-unknown-linux-gnu/release/minecraft_server_installer target/out/server-installer-linux-x86_64
cp target/aarch64-unknown-linux-gnu/release/minecraft_server_installer target/out/server-installer-linux-aarch64
cp target/x86_64-pc-windows-gnu/release/minecraft_server_installer.exe target/out/server-installer-windows-x86_64.exe
53 changes: 35 additions & 18 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ struct Config {
#[derive(Deserialize, Serialize)]
struct JavaPaths {
java_install_paths: String,
osx_8: String,
osx_16: String,
osx_17: String,
macos_8: String,
macos_16: String,
macos_17: String,
linux_8: String,
linux_16: String,
linux_17: String,
Expand All @@ -33,12 +33,12 @@ struct JavaPaths {

#[derive(Deserialize, Serialize)]
struct JavaDownloads {
osx_8: String,
osx_16: String,
osx_17: String,
osx_arm_8: String,
osx_arm_16: String,
osx_arm_17: String,
macos_8: String,
macos_16: String,
macos_17: String,
macos_arm_8: String,
macos_arm_16: String,
macos_arm_17: String,
linux_8: String,
linux_16: String,
linux_17: String,
Expand Down Expand Up @@ -67,6 +67,23 @@ impl ConfigFile {
file.write_all(toml_config.as_bytes()).expect("Failed to write config to file");
}

pub fn test(&self) {
let string = fs::read_to_string(self.path.to_string() + "/msi-config.toml");

if string.is_err() {
self.create();
}

if toml::from_str::<Config>(&string.unwrap()).is_err() {
println!("Failed to parse config file, regenerating file.");
fs::copy(self.path.to_string() + "/msi-config.toml", self.path.to_string() + "/msi-config-old.toml").expect("Failed to backup config file");
println!("Backed up old config file to msi-config-old.toml");

self.create();
}

}

pub fn get_java_download(&self, key: String, version: i32) -> Option<String> {
let config = self.clone().get_config();

Expand Down Expand Up @@ -129,9 +146,9 @@ impl ConfigFile {
Config {
java_paths: JavaPaths {
java_install_paths: "./java".to_string(),
osx_8: "/jdk8u402-b06-jre/Contents/Home/bin/java".to_string(),
osx_16: "/jdk-16.0.2+7-jre/Contents/Home/bin/java".to_string(),
osx_17: "/jdk-17.0.10+7-jre/Contents/Home/bin/java".to_string(),
macos_8: "/jdk8u402-b06-jre/Contents/Home/bin/java".to_string(),
macos_16: "/jdk-16.0.2+7-jre/Contents/Home/bin/java".to_string(),
macos_17: "/jdk-17.0.10+7-jre/Contents/Home/bin/java".to_string(),
linux_8: "/jdk8u402-b06-jre/bin/java".to_string(),
linux_16: "/jdk-16.0.2+7-jre/bin/java".to_string(),
linux_17: "/jdk-17.0.10+7-jre/bin/java".to_string(),
Expand All @@ -140,12 +157,12 @@ impl ConfigFile {
windows_17: "/jdk-17.0.10+7-jre/bin/java.exe".to_string(),
},
java_downloads: JavaDownloads {
osx_8: "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u402-b06/OpenJDK8U-jre_x64_mac_hotspot_8u402b06.tar.gz".to_string(),
osx_16: "https://github.com/adoptium/temurin16-binaries/releases/download/jdk16u-2021-09-14-01-32-beta/OpenJDK16U-jre_x64_mac_hotspot_2021-09-14-01-32.tar.gz".to_string(),
osx_17: "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_x64_mac_hotspot_17.0.10_7.tar.gz".to_string(),
osx_arm_8: "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u402-b06/OpenJDK8U-jre_x64_mac_hotspot_8u402b06.tar.gz".to_string(),
osx_arm_16: "https://github.com/adoptium/temurin16-binaries/releases/download/jdk16u-2021-09-14-01-32-beta/OpenJDK16U-jre_aarch64_linux_hotspot_2021-09-14-01-32.tar.gz".to_string(),
osx_arm_17: "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_aarch64_mac_hotspot_17.0.10_7.tar.gz".to_string(),
macos_8: "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u402-b06/OpenJDK8U-jre_x64_mac_hotspot_8u402b06.tar.gz".to_string(),
macos_16: "https://github.com/adoptium/temurin16-binaries/releases/download/jdk16u-2021-09-14-01-32-beta/OpenJDK16U-jre_x64_mac_hotspot_2021-09-14-01-32.tar.gz".to_string(),
macos_17: "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_x64_mac_hotspot_17.0.10_7.tar.gz".to_string(),
macos_arm_8: "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u402-b06/OpenJDK8U-jre_x64_mac_hotspot_8u402b06.tar.gz".to_string(),
macos_arm_16: "https://github.com/adoptium/temurin16-binaries/releases/download/jdk16u-2021-09-14-01-32-beta/OpenJDK16U-jre_aarch64_linux_hotspot_2021-09-14-01-32.tar.gz".to_string(),
macos_arm_17: "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_aarch64_mac_hotspot_17.0.10_7.tar.gz".to_string(),
linux_8: "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u402-b06/OpenJDK8U-jre_x64_linux_hotspot_8u402b06.tar.gz".to_string(),
linux_16: "https://github.com/adoptium/temurin16-binaries/releases/download/jdk16u-2021-09-14-01-32-beta/OpenJDK16U-jre_x64_linux_hotspot_2021-09-14-01-32.tar.gz".to_string(),
linux_17: "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10%2B7/OpenJDK17U-jre_x64_linux_hotspot_17.0.10_7.tar.gz".to_string(),
Expand Down
9 changes: 5 additions & 4 deletions src/downloaders/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ use std::path::Path;
use crate::downloader::download_file;

use crate::extract;
use crate::os::OS;

pub async fn download_java(client: &Client, java_install_path: &str, java_path: &str, url: &str) -> Result<(), String> {
pub async fn download_java(client: &Client, java_install_path: &str, java_path: &str, url: &str, os: &OS) -> Result<(), String> {
if !Path::new(java_path).exists() {
if cfg!(target_os = "windows") {
if os == &OS::Windows {
println!("Downloading Java...");
download_file(client, url, "./java.zip")
.await
.expect("Failed to download Java");

println!("Extracting Java...");
extract(&File::open("./java.zip").expect("Failed to unzip old Java file"), java_install_path);
extract(&File::open("./java.zip").expect("Failed to unzip old Java file"), java_install_path, os);

println!("Deleting old Java file...");
std::fs::remove_file("./java.zip").expect("Failed to delete old Java file");
Expand All @@ -25,7 +26,7 @@ pub async fn download_java(client: &Client, java_install_path: &str, java_path:
.expect("Failed to download Java");

println!("Extracting Java...");
extract(&File::open("./java.tar.gz").expect("Failed to unzip old Java file"), java_install_path);
extract(&File::open("./java.tar.gz").expect("Failed to unzip old Java file"), java_install_path, os);

println!("Deleting old Java file...");
std::fs::remove_file("./java.tar.gz").expect("Failed to delete old Java file");
Expand Down
43 changes: 23 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod downloaders;
pub mod config;
pub mod downloader;
pub mod downloaderror;
pub mod os;

use flate2::read::GzDecoder;
use reqwest::Client;
Expand All @@ -20,27 +21,27 @@ use crate::downloaders::java::download_java;
use crate::downloaders::neoforge::NeoForge;
use crate::downloaders::paper::Paper;
use crate::downloaders::vanilla::Vanilla;
use crate::os::OS;

#[tokio::main]
async fn main() {
prepare_hook();

let is_arm = env::consts::ARCH.contains("arch64") || env::consts::ARCH.contains("arm");
let os = if cfg!(target_os = "macos") {
"osx"
OS::MacOS
} else if cfg!(target_os = "linux") {
"linux"
OS::Linux
} else if cfg!(target_os = "windows") {
"windows"
OS::Windows
} else {
println!("Unsupported OS.");
return;
panic!("Unsupported OS.");
};

let config = config::ConfigFile {
path: if os == "windows" {
path: if os == OS::Windows {
env::var("APPDATA").expect("Failed to retrieve APPDATA variable") + "\\MinecraftServerInstaller"
} else if os == "linux" {
} else if os == OS::Linux {
env::var("XDG_CONFIG_HOME").expect("Failed to retrieve XDG_CONFIG_HOME variable") + "/MinecraftServerInstaller"
} else {
env::var("HOME").expect("Failed to retrieve HOME variable") + "/Library/Application Support/MinecraftServerInstaller"
Expand All @@ -55,6 +56,8 @@ async fn main() {
config.create();
}

config.test();

println!("Welcome to the Minecraft Server Installer!");
println!("This tool will help you set up a Minecraft server with ease.");
println!();
Expand All @@ -65,7 +68,7 @@ async fn main() {
loop {
let mut ready = false;

if os == "windows" {
if os == OS::Windows {
if File::open("./launch.bat").is_ok() {
ready = true;
}
Expand Down Expand Up @@ -99,7 +102,7 @@ async fn main() {
let num = selection.parse::<i32>().expect("Failed to parse selection");

if num == 1 {
run_launch_file(os).await;
run_launch_file(&os).await;
continue
} else if num == 2 {
change_ram();
Expand Down Expand Up @@ -161,7 +164,7 @@ async fn main() {

println!("Using Java {}", java_version);

download_java(&client, java_install_path.as_str(), java_path.as_str(), config.get_java_download(java_key, java_version).unwrap().as_str())
download_java(&client, java_install_path.as_str(), java_path.as_str(), config.get_java_download(java_key, java_version).unwrap().as_str(), &os)
.await
.expect("Failed to download Java");

Expand Down Expand Up @@ -191,7 +194,7 @@ async fn main() {
println!();

if num != 5 {
create_launch_script(Some(java_path.as_str()), java_version, os, 3);
create_launch_script(Some(java_path.as_str()), java_version, &os, 3);
} else {
create_args_file(3);
}
Expand All @@ -206,7 +209,7 @@ async fn main() {
print!("Would you like to run your server now? (y/n): ");

if yes_or_no() {
run_launch_file(os).await;
run_launch_file(&os).await;
} else {
goodbye();
wait_for_enter("exit");
Expand Down Expand Up @@ -247,11 +250,11 @@ fn yes_or_no() -> bool {
input == "y"
}

fn create_launch_script(java_path: Option<&str>, java_version: i32, os: &str, ram: i32) {
fn create_launch_script(java_path: Option<&str>, java_version: i32, os: &OS, ram: i32) {
println!("Creating launch script...");
create_args_file(ram);

let file_name = if os == "windows" {
let file_name = if os == &OS::Windows {
"./launch.bat"
} else {
"./launch.sh"
Expand Down Expand Up @@ -286,7 +289,7 @@ fn create_launch_script(java_path: Option<&str>, java_version: i32, os: &str, ra
let file = File::create(file_name).unwrap();
let mut file = BufWriter::new(file);

if os == "windows" {
if os == &OS::Windows {
file.write_all(
format!(
"@echo off\n\"{}\" {} -jar server.jar",
Expand Down Expand Up @@ -322,7 +325,7 @@ fn create_launch_script(java_path: Option<&str>, java_version: i32, os: &str, ra
}
}

if os != "windows" {
if os != &OS::Windows {
Command::new("chmod")
.arg("+x")
.arg("./launch.sh")
Expand Down Expand Up @@ -378,8 +381,8 @@ async fn accept_eula() {
file.write_all("eula=true".as_bytes()).unwrap();
}

fn extract(file: &File, path: &str) {
if cfg!(target_os = "windows") {
fn extract(file: &File, path: &str, os: &OS) {
if os == &OS::Windows {
let mut archive = zip::ZipArchive::new(file).expect("Failed to create ZipArchive");
archive.extract(path).expect("Failed to extract Java file");
} else {
Expand All @@ -405,12 +408,12 @@ fn wait_for_enter(message: &str) {
let _ = stdin().read_line(&mut String::new());
}

async fn run_launch_file(os: &str) {
async fn run_launch_file(os: &OS) {
println!("Starting server...");

let mut content = String::new();

if os == "windows" {
if os == &OS::Windows {
File::open("./launch.bat").expect("Failed to open launch.bat").read_to_string(&mut content).expect("Failed to read launch.bat");
} else {
File::open("./launch.sh").expect("Failed to open launch.sh").read_to_string(&mut content).expect("Failed to read launch.sh");
Expand Down
20 changes: 20 additions & 0 deletions src/os.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::fmt::{Display, Formatter};

#[allow(clippy::enum_variant_names)]
#[derive(PartialEq, Eq)]
pub enum OS {
Windows,
Linux,
MacOS,
}

impl Display for OS {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let str = match self {
OS::Windows => "windows".to_string(),
OS::Linux => "linux".to_string(),
OS::MacOS => "macos".to_string(),
};
write!(f, "{}", str)
}
}

0 comments on commit 23bd13c

Please sign in to comment.