From 99ef79a97fac206235d0af24ad5e07678ccd3d35 Mon Sep 17 00:00:00 2001 From: Sebaex Date: Fri, 6 Oct 2023 17:06:26 -0300 Subject: [PATCH 1/5] Main flow overhaul - Cleaned up some code because rust-analyzer was crying in warnings - Added some sort of menu to start working in installation methods for different devices --- .gitignore | 3 ++ src/main.rs | 85 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 31 deletions(-) diff --git a/.gitignore b/.gitignore index ada8be9..922324a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,9 @@ # will have compiled files and executables debug/ target/ +platform-tools/ +platformtools.zip +magiarecord.apk # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/src/main.rs b/src/main.rs index dcc8f5f..4da7169 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,83 @@ extern crate reqwest; extern crate zip; +use std::collections::HashMap; use std::io; use std::fs; use std::fs::File; use std::process::Command; use std::env; use std::path::Path; -use std::io::Write; fn main() { let args: Vec = env::args().collect(); - let na_flag = args.iter().any(|x| x == "-NA" || x == "-na"); let force_local_adb_flag = args.iter().any(|x| x == "-forceLocalADB"); let force_adb_download_flag = args.iter().any(|x| x == "-forceADBDownload"); - let use_old_apk_flag = args.iter().any(|x| x == "-noAPKDownload"); let no_install_flag = args.iter().any(|x| x == "-noInstall"); let do_nothing_flag = args.iter().any(|x| x == "-doNothing"); - if(do_nothing_flag){ + let emulator_connection = HashMap::from([ + ("bluestacks", "todo: change this"), + ("nox", "todo: change this"), + ("wsa", "todo: change this"), + ]); + + let mut switch_option = String::new(); + let mut device_type = String::new(); + let mut emulator_type = String::new(); + //let mut apk_type = String::new(); + + println!("Select your device\n 1.- Phone\n 2.- Emulator"); + io::stdin().read_line(&mut switch_option).expect("A problem has occured"); + let device_type_option : i8 = switch_option.trim().parse().expect("an integer lol"); + match device_type_option{ + 1 => device_type = String::from("phone"), + 2 => device_type = String::from("emulator"), + _ => main() + } + + if device_type == "emulator" { + println!("Select your emulator\n 1.- Bluestacks\n 2.- Nox Player\n 3.- Windows Subsystem for Android"); + io::stdin().read_line(&mut switch_option).expect("A problem has occured"); + let emulator_type_option : i8 = switch_option.trim().parse().expect("an integer lol"); + match emulator_type_option{ + 1 => emulator_type = String::from("bluestacks"), + 2 => emulator_type = String::from("nox"), + 3 => emulator_type = String::from("wsa"), + _ => main() + } + } + + /* Pending if rayshift starts uploading releases in github, should check how to get last release whenever it's available + println!("Device type: {}", device_type); + println!("Select desired APK\n 1.- Vanilla APK\n 2.- Rayshift APK") + */ + + + if do_nothing_flag { return; } - if(force_adb_download_flag || ((!detect_native_adb() || force_local_adb_flag) && !detect_prev_downloaded_adb())){ + if force_adb_download_flag || ((!detect_native_adb() || force_local_adb_flag) && !detect_prev_downloaded_adb()) { //print!("Downloading ADB"); //todo, request confirmation of acceptance of adb licence get_platform_tools(); } - if(!use_old_apk_flag){ - if(na_flag){ - print!("Using NA APK\n"); - get_na_apk(); - } else { - print!("Using JP APK\n"); - get_jp_apk(); - } - } + println!("Using JP APK\n"); + get_jp_apk(); let phonepath = "/data/local/tmp/magiarecord.apk"; - if(!no_install_flag){ - install_apk((force_local_adb_flag || !detect_native_adb()), phonepath); + if !no_install_flag { + install_apk(force_local_adb_flag || !detect_native_adb(), phonepath); } - print!("Done! -- Press Enter to continue"); - io::stdout().flush();//print requires manual flushing of lines... + println!("Done! -- Press Enter to continue"); io::stdin().read_line(&mut String::new()).unwrap(); + } @@ -86,7 +115,7 @@ fn get_platform_tools(){ //this entire block is hacky i should really use a native adb server emulation instead //but that does not exist, so instead this will have to do let target = get_url_for_platform_tools(); - if(target == "unknown"){ + if target == "unknown" { print!("Not a known os, you should manually configure ADB for your platform before running this script"); std::process::exit(1); } @@ -113,9 +142,9 @@ fn unzip_archive(path: &str){ for i in 0 .. archive.len() { let mut file_to_extract = archive.by_index(i).unwrap(); - let outpath = file_to_extract.sanitized_name(); + let outpath = file_to_extract.mangled_name(); - if(file_to_extract.name().ends_with('/')){ + if file_to_extract.name().ends_with('/') { fs::create_dir_all(&outpath).unwrap(); } else { if let Some(p) = outpath.parent() { @@ -145,11 +174,6 @@ fn get_apk(target: &str){ io::copy(&mut resp, &mut out).expect("failed to copy content"); } -fn get_na_apk(){ - let target = "https://apkpure.com/magia-record-english/com.aniplex.magireco.en/download"; - get_apk(target); -} - fn get_jp_apk(){ let target = "https://jp.rika.ren/apk/Origin/com.aniplex.magireco.arm8.apk"; get_apk(target); @@ -165,22 +189,21 @@ fn install_apk(local: bool, phonepath: &str){ "./platform-tools/adb" }; - print!("Waiting for Android device on ADB\n"); - io::stdout().flush();//print requires manual flushing of lines... + println!("Waiting for Android device on ADB\n"); let mut adb_wait_process = Command::new(adbcmd) .arg("wait-for-device") .spawn() .expect("Failed to wait for a device on adb"); + print!("done!\nPushing APK\n"); - io::stdout().flush();//print requires manual flushing of lines... adb_wait_process.wait().expect("Failed while waiting for device"); let mut adb_push_process = Command::new(adbcmd) .args(&["push", "magiarecord.apk", phonepath]) .spawn() .expect("Failed to push apk"); adb_push_process.wait().expect("Failed while waiting for apk push"); - print!("done!\nInstalling APK\n"); - io::stdout().flush();//print requires manual flushing of lines... + + println!("done!\nInstalling APK\n"); let mut adb_install_process = Command::new(adbcmd) .args(&["shell", "pm", "install", "-i", "\"com.android.vending\"", "-r", phonepath]) .spawn() From f72128786e73370385fb4e9239dcf5198050c7c8 Mon Sep 17 00:00:00 2001 From: Sebaex Date: Sat, 7 Oct 2023 01:53:23 -0300 Subject: [PATCH 2/5] Completed installation flow New: - Finished menu to choose between different installation methods - Implemented installation via ip for emulators such as Nox and WSA, if somebody uses yet another emulator, let me know Expectations: - To Rayshift to start uploading release APKs in github - Learn how to get the latest release whenever that happens Bugs: idk :)))) --- src/main.rs | 114 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4da7169..200fdeb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,54 +17,58 @@ fn main() { let force_adb_download_flag = args.iter().any(|x| x == "-forceADBDownload"); let no_install_flag = args.iter().any(|x| x == "-noInstall"); let do_nothing_flag = args.iter().any(|x| x == "-doNothing"); - - let emulator_connection = HashMap::from([ - ("bluestacks", "todo: change this"), - ("nox", "todo: change this"), - ("wsa", "todo: change this"), + let device_type_option; + let emulator_type_option; + let emulators_ports = HashMap::from([ + ("nox", "62001"), + ("wsa", "58526"), ]); - let mut switch_option = String::new(); - let mut device_type = String::new(); - let mut emulator_type = String::new(); + let mut switch_option : String = String::new(); + let mut device_type : String = String::new(); + let mut emulator_type : String = String::new(); //let mut apk_type = String::new(); - println!("Select your device\n 1.- Phone\n 2.- Emulator"); + if do_nothing_flag { + return; + } + + if force_adb_download_flag || ((!detect_native_adb() || force_local_adb_flag) && !detect_prev_downloaded_adb()) { + //print!("Downloading ADB"); + //todo, request confirmation of acceptance of adb licence + get_platform_tools(); + } + + clear_console(); + print!("Friendly reminder to search how to turn on usb debugging in your device"); + println!("Select your device\n 1.- Phone/Bluestacks\n 2.- Other Emulator"); //Bluestacks doesn't require IP connection via adb to install an apk io::stdin().read_line(&mut switch_option).expect("A problem has occured"); - let device_type_option : i8 = switch_option.trim().parse().expect("an integer lol"); + device_type_option = switch_option.trim().parse().expect("a number"); match device_type_option{ 1 => device_type = String::from("phone"), 2 => device_type = String::from("emulator"), _ => main() } + clear_console(); + if device_type == "emulator" { - println!("Select your emulator\n 1.- Bluestacks\n 2.- Nox Player\n 3.- Windows Subsystem for Android"); + switch_option = String::new(); + println!("Select your emulator\n 1.- Nox Player\n 2.- Windows Subsystem for Android"); io::stdin().read_line(&mut switch_option).expect("A problem has occured"); - let emulator_type_option : i8 = switch_option.trim().parse().expect("an integer lol"); + emulator_type_option = switch_option.trim().parse().expect("a number"); match emulator_type_option{ - 1 => emulator_type = String::from("bluestacks"), - 2 => emulator_type = String::from("nox"), - 3 => emulator_type = String::from("wsa"), + 1 => emulator_type = String::from("nox"), + 2 => emulator_type = String::from("wsa"), _ => main() } + connect_to_emu(force_local_adb_flag || !detect_native_adb(), emulators_ports.get(emulator_type.as_str()).unwrap(), emulator_type) } - + /* Pending if rayshift starts uploading releases in github, should check how to get last release whenever it's available println!("Device type: {}", device_type); println!("Select desired APK\n 1.- Vanilla APK\n 2.- Rayshift APK") */ - - - if do_nothing_flag { - return; - } - - if force_adb_download_flag || ((!detect_native_adb() || force_local_adb_flag) && !detect_prev_downloaded_adb()) { - //print!("Downloading ADB"); - //todo, request confirmation of acceptance of adb licence - get_platform_tools(); - } println!("Using JP APK\n"); get_jp_apk(); @@ -78,8 +82,50 @@ fn main() { println!("Done! -- Press Enter to continue"); io::stdin().read_line(&mut String::new()).unwrap(); -} +} + +fn pause(){ + println!("Press enter to continue..."); + + let mut pause = String::new(); + io::stdin().read_line(&mut pause).expect("Error... somehow"); +} +fn clear_console() -> Result<(), io::Error> { + if cfg!(target_os="windows"){ + Command::new("cmd") + .args(&["/C", "cls"]) + .status()?; + } + else { + Command::new("clear") + .status()?; + }; + Ok(()) +} + +fn connect_to_emu(local: bool, port: &&str, emulator_type: String){ + clear_console(); + let adbcmd = if !local { + "adb" + } else if cfg!(windows){ + ".\\platform-tools\\adb.exe" + } else { + "./platform-tools/adb" + }; + + if emulator_type == "wsa" { + println!("ADB might say 'failed to authenticate', don't worry about and accept the usb debugging prompt that will pop up, if not, skip this message\nAnother note, since WSA doesn't have play services, you won't be able to whale"); + pause() + } + + println!("Waiting for emulator connection in ADB\n"); + let mut adb_connect_emu = Command::new(adbcmd) + .args(&["connect", format!("localhost:{}", port).as_str()]) + .spawn() + .expect("Failed to wait for a device on adb"); + adb_connect_emu.wait().expect("Failed while connecting to emulator"); +} fn detect_prev_downloaded_adb() -> bool { let path_to_check = if cfg!(windows){ @@ -90,8 +136,6 @@ fn detect_prev_downloaded_adb() -> bool { return Path::new(path_to_check).exists(); } - - fn detect_native_adb() -> bool { let res = which::which("adb").is_ok(); return res; @@ -166,7 +210,6 @@ fn unzip_archive(path: &str){ } - fn get_apk(target: &str){ //https://users.rust-lang.org/t/download-file-from-web/19863 3 lines and it worked lol let mut resp = reqwest::get(target).expect("request failed"); @@ -195,7 +238,7 @@ fn install_apk(local: bool, phonepath: &str){ .spawn() .expect("Failed to wait for a device on adb"); - print!("done!\nPushing APK\n"); + print!("done!\nPushing APK... if nothing happens, double check the usb debugging prompt and accept it\n"); adb_wait_process.wait().expect("Failed while waiting for device"); let mut adb_push_process = Command::new(adbcmd) .args(&["push", "magiarecord.apk", phonepath]) @@ -209,4 +252,11 @@ fn install_apk(local: bool, phonepath: &str){ .spawn() .expect("Failed to install apk"); adb_install_process.wait().expect("Failed while waiting on install"); + + println!("Killing adb server"); + let mut adb_kill_server = Command::new(adbcmd) + .arg("kill-server") + .spawn() + .expect("Failed to kill server"); + adb_kill_server.wait().expect("Failled to kill adb server"); } From d5b9dbeb799e0de44a5d96d8e2f784819829e98e Mon Sep 17 00:00:00 2001 From: Sebaex Date: Sat, 7 Oct 2023 16:51:06 -0300 Subject: [PATCH 3/5] Removed Scripts for NA - Rip NA --- scripts_for_NA_install/NA-general.sh | 3 --- scripts_for_NA_install/NA-windows.bat | 1 - 2 files changed, 4 deletions(-) delete mode 100644 scripts_for_NA_install/NA-general.sh delete mode 100644 scripts_for_NA_install/NA-windows.bat diff --git a/scripts_for_NA_install/NA-general.sh b/scripts_for_NA_install/NA-general.sh deleted file mode 100644 index ecd210a..0000000 --- a/scripts_for_NA_install/NA-general.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash - -./magiarecord_automatic_updater -NA \ No newline at end of file diff --git a/scripts_for_NA_install/NA-windows.bat b/scripts_for_NA_install/NA-windows.bat deleted file mode 100644 index 4ed7b47..0000000 --- a/scripts_for_NA_install/NA-windows.bat +++ /dev/null @@ -1 +0,0 @@ -magiarecord_automatic_updater.exe -NA \ No newline at end of file From 620f398b368b6d90ce2e66093778e597ee04c781 Mon Sep 17 00:00:00 2001 From: Sebaex Date: Sat, 7 Oct 2023 21:28:00 -0300 Subject: [PATCH 4/5] Changed lines to LF - Scripts weren't working on WSL so had to make this change, if this somehow doesn't work when downloading again, will revert --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index fd0594b..e08833b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "magiarecord_automatic_updater" -version = "0.1.0" -authors = ["wxf"] +version = "1.1.0" +authors = ["wxf", "sebaex"] edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From 89b54d382d0ed656e176abf447511f462fa77865 Mon Sep 17 00:00:00 2001 From: Sebaex Date: Sat, 7 Oct 2023 21:41:32 -0300 Subject: [PATCH 5/5] oopsie whoopsie --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 200fdeb..3f2306f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,7 @@ fn main() { } clear_console(); - print!("Friendly reminder to search how to turn on usb debugging in your device"); + println!("Friendly reminder to search how to turn on usb debugging in your device"); println!("Select your device\n 1.- Phone/Bluestacks\n 2.- Other Emulator"); //Bluestacks doesn't require IP connection via adb to install an apk io::stdin().read_line(&mut switch_option).expect("A problem has occured"); device_type_option = switch_option.trim().parse().expect("a number");