diff --git a/src/lib.rs b/src/lib.rs index fd21160..e072aac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ use mlua::prelude::*; use mlua::Value; use crate::mods::*; -use crate::updater::self_update_balamod; +use crate::updater::{get_latest_cli_version, self_update}; mod core; mod mods; @@ -49,8 +49,8 @@ fn balalib(lua: &Lua) -> LuaResult { lua.create_function(|lua, mod_info: ModInfo| is_mod_present(lua, mod_info))?, )?; exports.set( - "self_update_balamod", - lua.create_function(|_, ()| self_update_balamod("v0.1.11"))?, + "self_update", + lua.create_function(|_, ()| self_update(get_latest_cli_version().as_str()))?, )?; exports.set("restart", lua.create_function(|_, ()| restart())?)?; exports.set( diff --git a/src/scripts/update.cmd b/src/scripts/update.cmd new file mode 100644 index 0000000..7c27774 --- /dev/null +++ b/src/scripts/update.cmd @@ -0,0 +1,4 @@ +taskkill /f /im balatro.exe +balamod.exe -u -a +start balatro.exe +pause \ No newline at end of file diff --git a/src/tests.rs b/src/tests.rs index ab45c75..68d9ec7 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -2,6 +2,7 @@ mod tests { use crate::utils::minify_lua; use std::fs; + use crate::updater::get_latest_cli_version; #[test] fn test_update() { @@ -26,4 +27,9 @@ mod tests { r#"function test() print("Hello World!") a = function() print("Hello World!") end a() end test()"# ); } + + #[test] + fn test_get_last_cli_version() { + println!("Latest CLI version: {}", get_latest_cli_version()); + } } diff --git a/src/updater.rs b/src/updater.rs index ea0d0b4..73eede4 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -33,7 +33,7 @@ pub fn need_update(current_version: String) -> LuaResult { } #[cfg(target_os = "windows")] -pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> { +pub fn self_update(cli_ver: &str) -> LuaResult<()> { let url = format!( "https://github.com/balamod/balamod/releases/download/{}/balamod-{}-windows.exe", cli_ver, cli_ver @@ -49,18 +49,24 @@ pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> { std::fs::rename("balamod.tmp", "balamod.exe")?; drop(file); - let output = std::process::Command::new("balamod.exe") - .arg("-u") - .arg("-a") - .output()?; + let script = include_bytes!("scripts/update.cmd"); - println!("{:?}", output); + let mut file = std::fs::File::create("update.cmd").unwrap(); + file.write_all(script).unwrap(); + drop(file); - restart() + // opens it in a new cmd window + std::process::Command::new("cmd") + .arg("/C") + .arg("start") + .arg("update.cmd") + .spawn()?; + + std::process::exit(0); } #[cfg(any(target_os = "macos", target_os = "linux"))] -pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> { +pub fn self_update(cli_ver: &str) -> LuaResult<()> { use std::os::unix::fs::PermissionsExt; let mut filename = format!("balamod-{}-", cli_ver); @@ -114,3 +120,34 @@ pub fn self_update_balamod(cli_ver: &str) -> LuaResult<()> { restart() } + +pub fn get_latest_cli_version() -> String { + let client = reqwest::blocking::Client::builder() + .user_agent("balalib") + .build() + .unwrap(); + + match client + .get("https://api.github.com/repos/balamod/balamod/releases") + .send() + { + Ok(response) => match response.text() { + Ok(text) => { + let releases: Vec = serde_json::from_str(&text) + .expect(format!("Failed to parse json: {}", text).as_str()); + let latest_version = releases + .iter() + .find(|release| { + !release["prerelease"].as_bool().unwrap() + && !release["draft"].as_bool().unwrap() + }) + .unwrap()["tag_name"] + .as_str() + .unwrap(); + latest_version.to_string() + } + Err(_) => "0.0.0".to_string(), + }, + Err(_) => "0.0.0".to_string(), + } +} \ No newline at end of file