feat(adb): remove adb resource

This commit is contained in:
AkiChase 2024-05-20 17:38:37 +08:00
parent c0d35cf02f
commit 4681287364
18 changed files with 100 additions and 128 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,7 +1,5 @@
use crate::resource::{ResHelper, ResourceName};
use std::{ use std::{
io::BufRead, io::BufRead,
path::PathBuf,
process::{Child, Command, Stdio}, process::{Child, Command, Stdio},
}; };
@ -18,8 +16,8 @@ pub struct Device {
impl Device { impl Device {
/// execute "adb push" to push file from src to des /// execute "adb push" to push file from src to des
pub fn cmd_push(res_dir: &PathBuf, id: &str, src: &str, des: &str) -> Result<String> { pub fn cmd_push(id: &str, src: &str, des: &str) -> Result<String> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
let res = adb_command let res = adb_command
.args(&["-s", id, "push", src, des]) .args(&["-s", id, "push", src, des])
.output() .output()
@ -28,8 +26,8 @@ impl Device {
} }
/// execute "adb reverse" to reverse the device port to local port /// execute "adb reverse" to reverse the device port to local port
pub fn cmd_reverse(res_dir: &PathBuf, id: &str, remote: &str, local: &str) -> Result<()> { pub fn cmd_reverse(id: &str, remote: &str, local: &str) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
adb_command adb_command
.args(&["-s", id, "reverse", remote, local]) .args(&["-s", id, "reverse", remote, local])
.output() .output()
@ -38,8 +36,8 @@ impl Device {
} }
/// execute "adb forward" to forward the local port to the device /// execute "adb forward" to forward the local port to the device
pub fn cmd_forward(res_dir: &PathBuf, id: &str, local: &str, remote: &str) -> Result<()> { pub fn cmd_forward(id: &str, local: &str, remote: &str) -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
adb_command adb_command
.args(&["-s", id, "forward", local, remote]) .args(&["-s", id, "forward", local, remote])
.output() .output()
@ -48,8 +46,8 @@ impl Device {
} }
/// execute "adb shell" to execute shell command on the device /// execute "adb shell" to execute shell command on the device
pub fn cmd_shell(res_dir: &PathBuf, id: &str, shell_args: &[&str]) -> Result<Child> { pub fn cmd_shell(id: &str, shell_args: &[&str]) -> Result<Child> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
let mut args = vec!["-s", id, "shell"]; let mut args = vec!["-s", id, "shell"];
args.extend_from_slice(shell_args); args.extend_from_slice(shell_args);
Ok(adb_command Ok(adb_command
@ -60,8 +58,8 @@ impl Device {
} }
/// execute "adb shell wm size" to get screen size /// execute "adb shell wm size" to get screen size
pub fn cmd_screen_size(res_dir: &PathBuf, id: &str) -> Result<(u32, u32)> { pub fn cmd_screen_size(id: &str) -> Result<(u32, u32)> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
let output = adb_command let output = adb_command
.args(&["-s", id, "shell", "wm", "size"]) .args(&["-s", id, "shell", "wm", "size"])
.output() .output()
@ -86,22 +84,19 @@ pub struct Adb;
/// Module to execute adb command and fetch output. /// Module to execute adb command and fetch output.
/// But some output of command won't be output, like adb service startup information. /// But some output of command won't be output, like adb service startup information.
impl Adb { impl Adb {
fn cmd_base(res_dir: &PathBuf) -> Command { pub fn cmd_base() -> Command {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
let mut cmd = Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb)); let mut cmd = Command::new("adb");
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW
cmd cmd
} }
#[cfg(not(target_os = "windows"))] Command::new("adb")
{
Command::new(ResHelper::get_file_path(res_dir, ResourceName::Adb))
}
} }
/// execute "adb devices" and return devices list /// execute "adb devices" and return devices list
pub fn cmd_devices(res_dir: &PathBuf) -> Result<Vec<Device>> { pub fn cmd_devices() -> Result<Vec<Device>> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
let output = adb_command let output = adb_command
.args(&["devices"]) .args(&["devices"])
.output() .output()
@ -128,8 +123,8 @@ impl Adb {
} }
/// execute "adb kill-server" /// execute "adb kill-server"
pub fn cmd_kill_server(res_dir: &PathBuf) -> Result<()> { pub fn cmd_kill_server() -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
adb_command adb_command
.args(&["kill-server"]) .args(&["kill-server"])
.output() .output()
@ -138,8 +133,8 @@ impl Adb {
} }
/// execute "adb reverse --remove-all" /// execute "adb reverse --remove-all"
pub fn cmd_reverse_remove(res_dir: &PathBuf) -> Result<()> { pub fn cmd_reverse_remove() -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
adb_command adb_command
.args(&["reverse", " --remove-all"]) .args(&["reverse", " --remove-all"])
.output() .output()
@ -148,8 +143,8 @@ impl Adb {
} }
/// execute "adb forward --remove-all" /// execute "adb forward --remove-all"
pub fn cmd_forward_remove(res_dir: &PathBuf) -> Result<()> { pub fn cmd_forward_remove() -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
adb_command adb_command
.args(&["forward", " --remove-all"]) .args(&["forward", " --remove-all"])
.output() .output()
@ -158,8 +153,8 @@ impl Adb {
} }
/// execute "adb start-server" /// execute "adb start-server"
pub fn cmd_start_server(res_dir: &PathBuf) -> Result<()> { pub fn cmd_start_server() -> Result<()> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
adb_command adb_command
.args(&["start-server"]) .args(&["start-server"])
.output() .output()
@ -167,8 +162,8 @@ impl Adb {
Ok(()) Ok(())
} }
pub fn cmd_connect(res_dir: &PathBuf, address: &str) -> Result<String> { pub fn cmd_connect(address: &str) -> Result<String> {
let mut adb_command = Adb::cmd_base(res_dir); let mut adb_command = Adb::cmd_base();
let output = adb_command let output = adb_command
.args(&["connect", address]) .args(&["connect", address])
.output() .output()
@ -178,10 +173,3 @@ impl Adb {
Ok(res) Ok(res)
} }
} }
#[test]
fn t() {
let res_dir = PathBuf::from("/Users/akichase/Projects/github/scrcpy-mask/src-tauri/resource/");
let res = Adb::cmd_connect(&res_dir, "127.0.0.1:1234").unwrap();
println!("{}", res)
}

View File

@ -3,7 +3,8 @@ use std::{io::BufRead, path::PathBuf};
use crate::{ use crate::{
adb::{Adb, Device}, adb::{Adb, Device},
resource::{ResHelper, ResourceName}, share, resource::{ResHelper, ResourceName},
share,
}; };
/** /**
@ -22,35 +23,34 @@ impl ScrcpyClient {
ResHelper::get_scrcpy_version() ResHelper::get_scrcpy_version()
} }
pub fn adb_devices(res_dir: &PathBuf) -> Result<Vec<Device>> { pub fn adb_devices() -> Result<Vec<Device>> {
Adb::cmd_devices(res_dir) Adb::cmd_devices()
} }
pub fn adb_restart_server(res_dir: &PathBuf) -> Result<()> { pub fn adb_restart_server() -> Result<()> {
Adb::cmd_kill_server(res_dir)?; Adb::cmd_kill_server()?;
Adb::cmd_start_server(res_dir)?; Adb::cmd_start_server()?;
Ok(()) Ok(())
} }
pub fn adb_reverse_remove(res_dir: &PathBuf) -> Result<()> { pub fn adb_reverse_remove() -> Result<()> {
Adb::cmd_reverse_remove(res_dir) Adb::cmd_reverse_remove()
} }
pub fn adb_forward_remove(res_dir: &PathBuf) -> Result<()> { pub fn adb_forward_remove() -> Result<()> {
Adb::cmd_forward_remove(res_dir) Adb::cmd_forward_remove()
} }
// get the screen size of the device // get the screen size of the device
pub fn get_device_screen_size(res_dir: &PathBuf, id: &str) -> Result<(u32, u32)> { pub fn get_device_screen_size(id: &str) -> Result<(u32, u32)> {
Device::cmd_screen_size(res_dir, id) Device::cmd_screen_size(id)
} }
/// push server file to current device /// push server file to current device
pub fn push_server_file(res_dir: &PathBuf, id: &str) -> Result<()> { pub fn push_server_file(dir: &PathBuf, id: &str) -> Result<()> {
let info = Device::cmd_push( let info = Device::cmd_push(
res_dir,
id, id,
&ResHelper::get_file_path(res_dir, ResourceName::ScrcpyServer).to_string_lossy(), &ResHelper::get_file_path(dir, ResourceName::ScrcpyServer).to_string_lossy(),
"/data/local/tmp/scrcpy-server.jar", "/data/local/tmp/scrcpy-server.jar",
)?; )?;
@ -59,9 +59,8 @@ impl ScrcpyClient {
} }
/// forward the local port to the device /// forward the local port to the device
pub fn forward_server_port(res_dir: &PathBuf, id: &str, scid: &str, port: u16) -> Result<()> { pub fn forward_server_port(id: &str, scid: &str, port: u16) -> Result<()> {
Device::cmd_forward( Device::cmd_forward(
res_dir,
id, id,
&format!("tcp:{}", port), &format!("tcp:{}", port),
&format!("localabstract:scrcpy_{}", scid), &format!("localabstract:scrcpy_{}", scid),
@ -71,9 +70,8 @@ impl ScrcpyClient {
} }
/// reverse the device port to the local port /// reverse the device port to the local port
pub fn reverse_server_port(res_dir: &PathBuf, id: &str, scid: &str, port: u16) -> Result<()> { pub fn reverse_server_port(id: &str, scid: &str, port: u16) -> Result<()> {
Device::cmd_reverse( Device::cmd_reverse(
res_dir,
id, id,
&format!("localabstract:scrcpy_{}", scid), &format!("localabstract:scrcpy_{}", scid),
&format!("tcp:{}", port), &format!("tcp:{}", port),
@ -83,14 +81,8 @@ impl ScrcpyClient {
} }
/// spawn a new thread to start scrcpy server /// spawn a new thread to start scrcpy server
pub fn shell_start_server( pub fn shell_start_server(id: &str, scid: &str, version: &str) -> Result<()> {
res_dir: &PathBuf,
id: &str,
scid: &str,
version: &str,
) -> Result<()> {
let mut child = Device::cmd_shell( let mut child = Device::cmd_shell(
res_dir,
id, id,
&[ &[
"CLASSPATH=/data/local/tmp/scrcpy-server.jar", "CLASSPATH=/data/local/tmp/scrcpy-server.jar",

View File

@ -13,9 +13,8 @@ use tauri::Manager;
#[tauri::command] #[tauri::command]
/// get devices info list /// get devices info list
fn adb_devices(app: tauri::AppHandle) -> Result<Vec<Device>, String> { fn adb_devices() -> Result<Vec<Device>, String> {
let dir = app.path().resource_dir().unwrap().join("resource"); match Adb::cmd_devices() {
match Adb::cmd_devices(&dir) {
Ok(devices) => Ok(devices), Ok(devices) => Ok(devices),
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
} }
@ -23,15 +22,8 @@ fn adb_devices(app: tauri::AppHandle) -> Result<Vec<Device>, String> {
#[tauri::command] #[tauri::command]
/// forward local port to the device port /// forward local port to the device port
fn forward_server_port( fn forward_server_port(id: String, scid: String, port: u16) -> Result<(), String> {
app: tauri::AppHandle, match ScrcpyClient::forward_server_port(&id, &scid, port) {
id: String,
scid: String,
port: u16,
) -> Result<(), String> {
let dir = app.path().resource_dir().unwrap().join("resource");
match ScrcpyClient::forward_server_port(&dir, &id, &scid, port) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
} }
@ -66,12 +58,11 @@ fn start_scrcpy_server(
scid.clone(), scid.clone(),
)); ));
let dir = app.path().resource_dir().unwrap().join("resource");
let version = ScrcpyClient::get_scrcpy_version(); let version = ScrcpyClient::get_scrcpy_version();
// start scrcpy server // start scrcpy server
tokio::spawn(async move { tokio::spawn(async move {
ScrcpyClient::shell_start_server(&dir, &id, &scid, &version).unwrap(); ScrcpyClient::shell_start_server(&id, &scid, &version).unwrap();
}); });
// connect to scrcpy server // connect to scrcpy server
@ -131,9 +122,8 @@ fn get_cur_client_info() -> Result<Option<share::ClientInfo>, String> {
#[tauri::command] #[tauri::command]
/// get device screen size /// get device screen size
fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32), String> { fn get_device_screen_size(id: String) -> Result<(u32, u32), String> {
let dir = app.path().resource_dir().unwrap().join("resource"); match ScrcpyClient::get_device_screen_size(&id) {
match ScrcpyClient::get_device_screen_size(&dir, &id) {
Ok(size) => Ok(size), Ok(size) => Ok(size),
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
} }
@ -141,9 +131,8 @@ fn get_device_screen_size(id: String, app: tauri::AppHandle) -> Result<(u32, u32
#[tauri::command] #[tauri::command]
/// connect to wireless device /// connect to wireless device
fn adb_connect(address: String, app: tauri::AppHandle) -> Result<String, String> { fn adb_connect(address: String) -> Result<String, String> {
let dir = app.path().resource_dir().unwrap().join("resource"); match Adb::cmd_connect(&address) {
match Adb::cmd_connect(&dir, &address) {
Ok(res) => Ok(res), Ok(res) => Ok(res),
Err(e) => Err(e.to_string()), Err(e) => Err(e.to_string()),
} }
@ -160,6 +149,14 @@ fn load_default_keyconfig(app: tauri::AppHandle) -> Result<String, String> {
} }
} }
#[tauri::command]
fn check_adb_available() -> Result<(), String> {
match Adb::cmd_base().output() {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
tauri::Builder::default() tauri::Builder::default()
@ -241,7 +238,8 @@ async fn main() {
get_cur_client_info, get_cur_client_info,
get_device_screen_size, get_device_screen_size,
adb_connect, adb_connect,
load_default_keyconfig load_default_keyconfig,
check_adb_available
]) ])
.run(tauri::generate_context!()) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");

View File

@ -2,7 +2,6 @@ use anyhow::{anyhow, Ok, Result};
use std::path::PathBuf; use std::path::PathBuf;
pub enum ResourceName { pub enum ResourceName {
Adb,
ScrcpyServer, ScrcpyServer,
DefaultKeyConfig, DefaultKeyConfig,
} }
@ -13,7 +12,9 @@ pub struct ResHelper {
impl ResHelper { impl ResHelper {
pub fn res_init(res_dir: &PathBuf) -> Result<()> { pub fn res_init(res_dir: &PathBuf) -> Result<()> {
for name in [ResourceName::Adb, ResourceName::ScrcpyServer] { let res = [ResourceName::ScrcpyServer, ResourceName::DefaultKeyConfig];
for name in res {
let file_path = ResHelper::get_file_path(res_dir, name); let file_path = ResHelper::get_file_path(res_dir, name);
if !file_path.exists() { if !file_path.exists() {
return Err(anyhow!(format!( return Err(anyhow!(format!(
@ -27,13 +28,6 @@ impl ResHelper {
} }
pub fn get_file_path(dir: &PathBuf, file_name: ResourceName) -> PathBuf { pub fn get_file_path(dir: &PathBuf, file_name: ResourceName) -> PathBuf {
match file_name { match file_name {
#[cfg(target_os = "windows")]
ResourceName::Adb => dir.join("adb-win.exe"),
#[cfg(target_os = "linux")]
ResourceName::Adb => dir.join("adb-linux"),
#[cfg(target_os = "macos")]
ResourceName::Adb => dir.join("adb-mac"),
ResourceName::ScrcpyServer => dir.join("scrcpy-server-v2.4"), ResourceName::ScrcpyServer => dir.join("scrcpy-server-v2.4"),
ResourceName::DefaultKeyConfig => dir.join("default-key-config.json"), ResourceName::DefaultKeyConfig => dir.join("default-key-config.json"),
} }

View File

@ -30,6 +30,10 @@
"icons/128x128@2x.png", "icons/128x128@2x.png",
"icons/icon.icns", "icons/icon.icns",
"icons/icon.ico" "icons/icon.ico"
],
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4"
] ]
} }
} }

View File

@ -1,9 +0,0 @@
{
"bundle": {
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4",
"resource/adb-linux"
]
}
}

View File

@ -1,9 +0,0 @@
{
"bundle": {
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4",
"resource/adb-mac"
]
}
}

View File

@ -1,11 +0,0 @@
{
"bundle": {
"resources": [
"resource/default-key-config.json",
"resource/scrcpy-server-v2.4",
"resource/adb-win.exe",
"resource/AdbWinApi.dll",
"resource/AdbWinUsbApi.dll"
]
}
}

View File

@ -296,7 +296,12 @@ async function onMenuSelect(key: string) {
async function refreshDevices() { async function refreshDevices() {
store.showLoading(); store.showLoading();
try {
devices.value = await adbDevices(); devices.value = await adbDevices();
} catch (e) {
message.error(t("pages.Device.adbDeviceError"));
console.error(e);
}
store.hideLoading(); store.hideLoading();
} }

View File

@ -20,6 +20,7 @@ import { AndroidKeycode } from "../frontcommand/android";
import { Store } from "@tauri-apps/plugin-store"; import { Store } from "@tauri-apps/plugin-store";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import { SendKeyAction, sendKey } from "../frontcommand/scrcpyMaskCmd"; import { SendKeyAction, sendKey } from "../frontcommand/scrcpyMaskCmd";
import { checkAdbAvailable } from "../invoke";
const { t } = useI18n(); const { t } = useI18n();
const store = useGlobalStore(); const store = useGlobalStore();
@ -64,12 +65,23 @@ onActivated(async () => {
}); });
onMounted(async () => { onMounted(async () => {
await checkAdb();
await loadLocalStore(); await loadLocalStore();
store.checkUpdate = checkUpdate; store.checkUpdate = checkUpdate;
store.showInputBox = showInputBox; store.showInputBox = showInputBox;
if (store.checkUpdateAtStart) checkUpdate(); if (store.checkUpdateAtStart) checkUpdate();
}); });
async function checkAdb() {
try {
await checkAdbAvailable();
} catch (e) {
message.error(t("pages.Mask.checkAdb", [e]), {
duration: 0,
});
}
}
async function loadLocalStore() { async function loadLocalStore() {
const localStore = new Store("store.bin"); const localStore = new Store("store.bin");
// loading screenSize from local store // loading screenSize from local store

View File

@ -42,7 +42,8 @@
"wsAddress": "Websocket address", "wsAddress": "Websocket address",
"inputWsAddress": "Please enter the Websocket address", "inputWsAddress": "Please enter the Websocket address",
"wsClose": "Close", "wsClose": "Close",
"wsConnect": "Control" "wsConnect": "Control",
"adbDeviceError": "Unable to get available devices"
}, },
"Mask": { "Mask": {
"inputBoxPlaceholder": "Input text and then press enter/esc", "inputBoxPlaceholder": "Input text and then press enter/esc",
@ -62,7 +63,8 @@
"content": "Please go to the device page to control any device", "content": "Please go to the device page to control any device",
"positiveText": "To control" "positiveText": "To control"
}, },
"sightMode": "Mouse is locked, press {0} to unlock" "sightMode": "Mouse is locked, press {0} to unlock",
"checkAdb": "adb is not available and the software cannot run normally. Please ensure that adb is installed on the system and added to the Path environment variable correctly: {0}"
}, },
"Setting": { "Setting": {
"tabs": { "tabs": {

View File

@ -42,7 +42,8 @@
"externalControl": "外部控制", "externalControl": "外部控制",
"wsAddress": "Websocket 地址", "wsAddress": "Websocket 地址",
"wsClose": "断开", "wsClose": "断开",
"wsConnect": "控制" "wsConnect": "控制",
"adbDeviceError": "无法获取可用设备"
}, },
"Mask": { "Mask": {
"keyconfigException": "按键方案异常,请删除此方案", "keyconfigException": "按键方案异常,请删除此方案",
@ -62,7 +63,8 @@
"positiveText": "去控制" "positiveText": "去控制"
}, },
"inputBoxPlaceholder": "输入文本后按Enter/Esc", "inputBoxPlaceholder": "输入文本后按Enter/Esc",
"sightMode": "鼠标已锁定, 按 {0} 键解锁" "sightMode": "鼠标已锁定, 按 {0} 键解锁",
"checkAdb": "adb不可用软件无法正常运行请确保系统已安装adb并正确添加到了Path环境变量中: {0}"
}, },
"Setting": { "Setting": {
"tabs": { "tabs": {

View File

@ -51,4 +51,8 @@ export async function loadDefaultKeyconfig(): Promise<string> {
return await invoke("load_default_keyconfig"); return await invoke("load_default_keyconfig");
} }
export async function checkAdbAvailable(): Promise<void>{
return await invoke("check_adb_available");
}
export type { Device }; export type { Device };