Finished Installation

This commit is contained in:
Samuel Walker 2024-06-23 00:28:08 -06:00
parent a0c9fd14c9
commit 3d34878ae6

View File

@ -1,11 +1,12 @@
use crate::ftp;
use crate::system_dirs::{get_local_data_directory, get_prism_executable};
use std::env;
use crate::{ftp, java};
use crate::system_dirs::{get_data_directory, get_java_executable, get_local_data_directory, get_prism_executable};
use std::time::Duration;
use std::{env, thread};
use std::fs::File;
use std::process::Command;
use std::{io::Cursor, path::PathBuf};
use std::io::{Read, Seek};
use std::io::{Read, Seek, Write};
use serde_json::Value;
use serde::Serialize;
use serde::Deserialize;
@ -66,14 +67,61 @@ pub fn launch_modpack(id: String){
install_modpack(id.clone());
}
// Launch
let mut child = Command::new(get_local_data_directory().join("prism").join(get_prism_executable())).arg("-l").arg(get_modpack_name(id)).spawn().unwrap();
}
fn install_modpack(id: String){
let versions = get_versions(id.clone());
let path = env::temp_dir().join(format!("{}.mrpack", get_modpack_name(id.clone())));
let mut file = File::create(path.clone()).unwrap();
ftp::ftp_retr(PathBuf::new().join(id).join(versions[versions.len()-1].version.clone()), file, |_| return);
let child = Command::new(get_local_data_directory().join("prism").join(get_prism_executable())).arg("-I").arg(path).spawn().unwrap();
let ftp_path = PathBuf::new().join(id.clone()).join(versions[versions.len()-1].file.clone().as_str());
println!("Downloading file {}", ftp_path.to_str().unwrap());
ftp::ftp_retr(ftp_path, &mut file, |_| return).unwrap();
let mut child = Command::new(get_local_data_directory().join("prism").join(get_prism_executable())).arg("-I").arg(path).spawn().unwrap();
loop {
let version_path = get_data_directory().join("prism").join("instances").join(get_modpack_name(id.clone())).join(".minecraft").join("version.txt");
if version_path.clone().exists() {
let mut ver_file = File::open(version_path).unwrap();
let mut buf = String::new();
ver_file.read_to_string(&mut buf).unwrap();
if buf == versions[versions.len()-1].version.clone().as_str() {
break;
}
}
thread::sleep(Duration::from_secs(3));
}
thread::sleep(Duration::from_secs(1));
child.kill();
let info_path = get_local_data_directory().join("prism").join("instances").join(get_modpack_name(id.clone())).join("mmc-pack.json");
let mut info_file = File::open(info_path.clone()).unwrap();
let info_json: Value = serde_json::from_reader(info_file).unwrap();
let mut mc_version = "0.0";
for component in info_json["components"].as_array().unwrap() {
if component["uid"] == "net.minecraft" {
mc_version = component["version"].as_str().unwrap();
}
}
java::install_java(get_java_version(mc_version));
let option_path = get_local_data_directory().join("prism").join("instances").join(get_modpack_name(id.clone())).join("instance.cfg");
let mut option_file = File::open(option_path.clone()).unwrap();
let mut buf = String::new();
option_file.read_to_string(&mut buf);
let mut option_file = File::create(option_path).unwrap();
let mut set = false;
for line in buf.lines() {
if line.starts_with("JavaPath=") {
option_file.write_all(format!("JavaPath={}/java/java-{}/bin/{}\n", get_data_directory().join("java").into_os_string().to_str().unwrap().replace("\\", "/"), if env::consts::OS == "windows" {"win"} else {"lin"}, get_java_executable()).as_bytes());
set = true;
} else {
option_file.write_all(format!("{}\n",line).as_bytes());
}
}
if !set {
option_file.write_all(format!("JavaPath={}/java/java-{}/bin/{}\n", get_data_directory().join("java").into_os_string().to_str().unwrap().replace("\\", "/"), if env::consts::OS == "windows" {"win"} else {"lin"}, get_java_executable()).as_bytes());
option_file.write_all("OverrideJavaLocation=true\n".as_bytes());
option_file.write_all("OverrideJava=true\n".as_bytes());
}
}
@ -102,7 +150,24 @@ fn get_versions(id: String) -> Vec<VersionEntry> {
buf.rewind();
let v: Value = serde_json::from_reader(buf).unwrap();
for version in v.as_array().unwrap() {
versions.push(VersionEntry{version: version["Version"].to_string(), file: version["File"].to_string(), date: version["Date"].to_string()});
versions.push(VersionEntry{version: version["Version"].as_str().unwrap().to_string(), file: version["File"].as_str().unwrap().to_string(), date: version["Date"].as_str().unwrap().to_string()});
}
return versions.clone();
}
fn get_java_version(mc_version: &str) -> u8{
let components: Vec<&str> = mc_version.split(".").collect();
let mut java = 8;
if components[1] == "17" {
java = 17
} else if components[1] == "18" || components[1] == "19" {
java = 17
} else if components[1].parse::<i32>().unwrap() > 19 {
if components[1] == "20" && components[1].parse::<i32>().unwrap() < 5 {
java = 17
} else {
java = 21
}
}
return java;
}