feat: update app_links

This commit is contained in:
xkeyC
2025-12-26 23:03:41 +08:00
parent 1d59acff2d
commit a673f70862
19 changed files with 996 additions and 26 deletions

View File

@@ -0,0 +1,400 @@
use std::env;
/// Applinks URL scheme registration result
#[derive(Debug, Clone)]
pub struct ApplinksRegistrationResult {
/// Whether registration was successful
pub success: bool,
/// Detailed message about the operation
pub message: String,
/// Whether the registry was modified (false if already configured correctly)
pub was_modified: bool,
}
/// Check if the URL scheme is already registered with the correct executable path
#[cfg(target_os = "windows")]
pub fn check_applinks_registration(scheme: String) -> anyhow::Result<ApplinksRegistrationResult> {
use windows::core::{HSTRING, PCWSTR};
use windows::Win32::System::Registry::{
RegCloseKey, RegOpenKeyExW, RegQueryValueExW, HKEY_CURRENT_USER, KEY_READ,
REG_VALUE_TYPE,
};
let app_path = env::current_exe()
.map_err(|e| anyhow::anyhow!("Failed to get current executable path: {}", e))?
.to_string_lossy()
.to_string();
let expected_command = format!("\"{}\" \"%1\"", app_path);
let protocol_key_path = format!("Software\\Classes\\{}", scheme);
let command_key_path = format!("{}\\shell\\open\\command", protocol_key_path);
unsafe {
// Check if URL Protocol value exists
let mut protocol_key = std::mem::zeroed();
let protocol_key_hstring = HSTRING::from(&protocol_key_path);
if RegOpenKeyExW(
HKEY_CURRENT_USER,
PCWSTR(protocol_key_hstring.as_ptr()),
Some(0),
KEY_READ,
&mut protocol_key,
)
.is_err()
{
return Ok(ApplinksRegistrationResult {
success: false,
message: format!("URL scheme '{}' is not registered", scheme),
was_modified: false,
});
}
// Check URL Protocol value
let url_protocol_name = HSTRING::from("URL Protocol");
let mut data_type: REG_VALUE_TYPE = REG_VALUE_TYPE::default();
let mut data_size: u32 = 0;
if RegQueryValueExW(
protocol_key,
PCWSTR(url_protocol_name.as_ptr()),
None,
Some(&mut data_type),
None,
Some(&mut data_size),
)
.is_err()
{
let _ = RegCloseKey(protocol_key);
return Ok(ApplinksRegistrationResult {
success: false,
message: format!("URL Protocol value not found for scheme '{}'", scheme),
was_modified: false,
});
}
let _ = RegCloseKey(protocol_key);
// Check command key
let mut command_key = std::mem::zeroed();
let command_key_hstring = HSTRING::from(&command_key_path);
if RegOpenKeyExW(
HKEY_CURRENT_USER,
PCWSTR(command_key_hstring.as_ptr()),
Some(0),
KEY_READ,
&mut command_key,
)
.is_err()
{
return Ok(ApplinksRegistrationResult {
success: false,
message: format!("Command key not found for scheme '{}'", scheme),
was_modified: false,
});
}
// Read command value (default value with empty name)
let empty_name = HSTRING::from("");
let mut data_size: u32 = 0;
if RegQueryValueExW(
command_key,
PCWSTR(empty_name.as_ptr()),
None,
Some(&mut data_type),
None,
Some(&mut data_size),
)
.is_err()
|| data_size == 0
{
let _ = RegCloseKey(command_key);
return Ok(ApplinksRegistrationResult {
success: false,
message: format!("Command value not found for scheme '{}'", scheme),
was_modified: false,
});
}
// Read the actual command value
let mut buffer: Vec<u8> = vec![0; data_size as usize];
if RegQueryValueExW(
command_key,
PCWSTR(empty_name.as_ptr()),
None,
Some(&mut data_type),
Some(buffer.as_mut_ptr()),
Some(&mut data_size),
)
.is_err()
{
let _ = RegCloseKey(command_key);
return Ok(ApplinksRegistrationResult {
success: false,
message: format!("Failed to read command value for scheme '{}'", scheme),
was_modified: false,
});
}
let _ = RegCloseKey(command_key);
// Convert buffer to string (UTF-16 to UTF-8)
let command_value = String::from_utf16_lossy(
&buffer
.chunks_exact(2)
.map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
.take_while(|&c| c != 0)
.collect::<Vec<u16>>(),
);
// Compare with expected command (case-insensitive for path)
if command_value.to_lowercase() == expected_command.to_lowercase() {
Ok(ApplinksRegistrationResult {
success: true,
message: format!("URL scheme '{}' is already registered correctly", scheme),
was_modified: false,
})
} else {
Ok(ApplinksRegistrationResult {
success: false,
message: format!(
"URL scheme '{}' is registered but with different path. Current: '{}', Expected: '{}'",
scheme, command_value, expected_command
),
was_modified: false,
})
}
}
}
#[cfg(not(target_os = "windows"))]
pub fn check_applinks_registration(scheme: String) -> anyhow::Result<ApplinksRegistrationResult> {
Ok(ApplinksRegistrationResult {
success: false,
message: format!(
"URL scheme registration check is not supported on this platform for scheme '{}'",
scheme
),
was_modified: false,
})
}
/// Register URL scheme in Windows registry
/// This will create or update the registry keys for the custom URL scheme
///
/// # Arguments
/// * `scheme` - The URL scheme to register (e.g., "sctoolbox")
/// * `app_name` - Optional application display name (e.g., "SCToolBox"). If provided,
/// the registry will show "URL:{app_name} Protocol" as the scheme description.
#[cfg(target_os = "windows")]
pub fn register_applinks(scheme: String, app_name: Option<String>) -> anyhow::Result<ApplinksRegistrationResult> {
use windows::core::{HSTRING, PCWSTR};
use windows::Win32::System::Registry::{
RegCloseKey, RegCreateKeyExW, RegSetValueExW, HKEY_CURRENT_USER, KEY_WRITE,
REG_CREATE_KEY_DISPOSITION, REG_OPTION_NON_VOLATILE, REG_SZ,
};
// First check if already registered correctly
let check_result = check_applinks_registration(scheme.clone())?;
if check_result.success {
return Ok(check_result);
}
let app_path = env::current_exe()
.map_err(|e| anyhow::anyhow!("Failed to get current executable path: {}", e))?
.to_string_lossy()
.to_string();
let command_value = format!("\"{}\" \"%1\"", app_path);
let protocol_key_path = format!("Software\\Classes\\{}", scheme);
let command_key_path = format!("{}\\shell\\open\\command", protocol_key_path);
unsafe {
// Create protocol key
let mut protocol_key = std::mem::zeroed();
let protocol_key_hstring = HSTRING::from(&protocol_key_path);
let mut disposition: REG_CREATE_KEY_DISPOSITION = REG_CREATE_KEY_DISPOSITION::default();
if RegCreateKeyExW(
HKEY_CURRENT_USER,
PCWSTR(protocol_key_hstring.as_ptr()),
Some(0),
PCWSTR::null(),
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
None,
&mut protocol_key,
Some(&mut disposition),
)
.is_err()
{
return Err(anyhow::anyhow!(
"Failed to create registry key '{}'",
protocol_key_path
));
}
// Set default value (display name) if app_name is provided
if let Some(ref name) = app_name {
let display_name = format!("URL:{} Protocol", name);
let empty_name = HSTRING::from("");
let display_name_bytes: Vec<u8> = display_name
.encode_utf16()
.chain(std::iter::once(0))
.flat_map(|c| c.to_le_bytes())
.collect();
if RegSetValueExW(
protocol_key,
PCWSTR(empty_name.as_ptr()),
Some(0),
REG_SZ,
Some(&display_name_bytes),
)
.is_err()
{
let _ = RegCloseKey(protocol_key);
return Err(anyhow::anyhow!("Failed to set display name value"));
}
}
// Set URL Protocol value (empty string)
let url_protocol_name = HSTRING::from("URL Protocol");
let empty_value: [u8; 2] = [0, 0]; // Empty UTF-16 string
if RegSetValueExW(
protocol_key,
PCWSTR(url_protocol_name.as_ptr()),
Some(0),
REG_SZ,
Some(&empty_value),
)
.is_err()
{
let _ = RegCloseKey(protocol_key);
return Err(anyhow::anyhow!("Failed to set URL Protocol value"));
}
let _ = RegCloseKey(protocol_key);
// Create command key
let mut command_key = std::mem::zeroed();
let command_key_hstring = HSTRING::from(&command_key_path);
if RegCreateKeyExW(
HKEY_CURRENT_USER,
PCWSTR(command_key_hstring.as_ptr()),
Some(0),
PCWSTR::null(),
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
None,
&mut command_key,
Some(&mut disposition),
)
.is_err()
{
return Err(anyhow::anyhow!(
"Failed to create command key '{}'",
command_key_path
));
}
// Set command value
let empty_name = HSTRING::from("");
let command_bytes: Vec<u8> = command_value
.encode_utf16()
.chain(std::iter::once(0))
.flat_map(|c| c.to_le_bytes())
.collect();
if RegSetValueExW(
command_key,
PCWSTR(empty_name.as_ptr()),
Some(0),
REG_SZ,
Some(&command_bytes),
)
.is_err()
{
let _ = RegCloseKey(command_key);
return Err(anyhow::anyhow!("Failed to set command value"));
}
let _ = RegCloseKey(command_key);
Ok(ApplinksRegistrationResult {
success: true,
message: format!(
"Successfully registered URL scheme '{}' with command '{}'",
scheme, command_value
),
was_modified: true,
})
}
}
#[cfg(not(target_os = "windows"))]
pub fn register_applinks(scheme: String, _app_name: Option<String>) -> anyhow::Result<ApplinksRegistrationResult> {
Ok(ApplinksRegistrationResult {
success: false,
message: format!(
"URL scheme registration is not supported on this platform for scheme '{}'",
scheme
),
was_modified: false,
})
}
/// Unregister URL scheme from Windows registry
#[cfg(target_os = "windows")]
pub fn unregister_applinks(scheme: String) -> anyhow::Result<ApplinksRegistrationResult> {
use windows::core::{HSTRING, PCWSTR};
use windows::Win32::System::Registry::{RegDeleteTreeW, HKEY_CURRENT_USER};
let protocol_key_path = format!("Software\\Classes\\{}", scheme);
unsafe {
let protocol_key_hstring = HSTRING::from(&protocol_key_path);
let result = RegDeleteTreeW(HKEY_CURRENT_USER, PCWSTR(protocol_key_hstring.as_ptr()));
if result.is_err() {
// Check if the key doesn't exist (not an error in this case)
let error_code = result.0 as u32;
if error_code == 2 {
// ERROR_FILE_NOT_FOUND
return Ok(ApplinksRegistrationResult {
success: true,
message: format!("URL scheme '{}' was not registered", scheme),
was_modified: false,
});
}
return Err(anyhow::anyhow!(
"Failed to delete registry key '{}': error code {}",
protocol_key_path,
error_code
));
}
Ok(ApplinksRegistrationResult {
success: true,
message: format!("Successfully unregistered URL scheme '{}'", scheme),
was_modified: true,
})
}
}
#[cfg(not(target_os = "windows"))]
pub fn unregister_applinks(scheme: String) -> anyhow::Result<ApplinksRegistrationResult> {
Ok(ApplinksRegistrationResult {
success: false,
message: format!(
"URL scheme unregistration is not supported on this platform for scheme '{}'",
scheme
),
was_modified: false,
})
}

View File

@@ -1,6 +1,7 @@
//
// Do not put code in `mod.rs`, but put in e.g. `simple.rs`.
//
pub mod applinks_api;
pub mod http_api;
pub mod rs_process;
pub mod win32_api;

View File

@@ -37,7 +37,7 @@ flutter_rust_bridge::frb_generated_boilerplate!(
default_rust_auto_opaque = RustAutoOpaqueNom,
);
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_VERSION: &str = "2.11.1";
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -1903117367;
pub(crate) const FLUTTER_RUST_BRIDGE_CODEGEN_CONTENT_HASH: i32 = -351025706;
// Section: executor
@@ -66,6 +66,30 @@ fn wire__crate__api__win32_api__add_nvme_patch_impl(
},
)
}
fn wire__crate__api__applinks_api__check_applinks_registration_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
scheme: impl CstDecode<String>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "check_applinks_registration",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let api_scheme = scheme.cst_decode();
move |context| {
transform_result_dco::<_, _, flutter_rust_bridge::for_generated::anyhow::Error>(
(move || {
let output_ok =
crate::api::applinks_api::check_applinks_registration(api_scheme)?;
Ok(output_ok)
})(),
)
}
},
)
}
fn wire__crate__api__win32_api__check_nvme_patch_status_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
) {
@@ -1462,6 +1486,32 @@ fn wire__crate__api__unp4k_api__p4k_open_impl(
},
)
}
fn wire__crate__api__applinks_api__register_applinks_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
scheme: impl CstDecode<String>,
app_name: impl CstDecode<Option<String>>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "register_applinks",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let api_scheme = scheme.cst_decode();
let api_app_name = app_name.cst_decode();
move |context| {
transform_result_dco::<_, _, flutter_rust_bridge::for_generated::anyhow::Error>(
(move || {
let output_ok =
crate::api::applinks_api::register_applinks(api_scheme, api_app_name)?;
Ok(output_ok)
})(),
)
}
},
)
}
fn wire__crate__api__win32_api__remove_nvme_patch_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
) {
@@ -1485,7 +1535,7 @@ fn wire__crate__api__win32_api__remove_nvme_patch_impl(
}
fn wire__crate__api__win32_api__resolve_shortcut_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
_lnk_path: impl CstDecode<String>,
lnk_path: impl CstDecode<String>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
@@ -1494,11 +1544,11 @@ fn wire__crate__api__win32_api__resolve_shortcut_impl(
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let api__lnk_path = _lnk_path.cst_decode();
let api_lnk_path = lnk_path.cst_decode();
move |context| {
transform_result_dco::<_, _, flutter_rust_bridge::for_generated::anyhow::Error>(
(move || {
let output_ok = crate::api::win32_api::resolve_shortcut(api__lnk_path)?;
let output_ok = crate::api::win32_api::resolve_shortcut(api_lnk_path)?;
Ok(output_ok)
})(),
)
@@ -1788,6 +1838,29 @@ fn wire__crate__api__ort_api__unload_translation_model_impl(
},
)
}
fn wire__crate__api__applinks_api__unregister_applinks_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
scheme: impl CstDecode<String>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
debug_name: "unregister_applinks",
port: Some(port_),
mode: flutter_rust_bridge::for_generated::FfiCallMode::Normal,
},
move || {
let api_scheme = scheme.cst_decode();
move |context| {
transform_result_dco::<_, _, flutter_rust_bridge::for_generated::anyhow::Error>(
(move || {
let output_ok = crate::api::applinks_api::unregister_applinks(api_scheme)?;
Ok(output_ok)
})(),
)
}
},
)
}
fn wire__crate__api__webview_api__web_view_configuration_default_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
) {
@@ -2296,6 +2369,20 @@ impl SseDecode for String {
}
}
impl SseDecode for crate::api::applinks_api::ApplinksRegistrationResult {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut var_success = <bool>::sse_decode(deserializer);
let mut var_message = <String>::sse_decode(deserializer);
let mut var_wasModified = <bool>::sse_decode(deserializer);
return crate::api::applinks_api::ApplinksRegistrationResult {
success: var_success,
message: var_message,
was_modified: var_wasModified,
};
}
}
impl SseDecode for bool {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
@@ -2940,6 +3027,28 @@ fn pde_ffi_dispatcher_sync_impl(
// Section: rust2dart
// Codec=Dco (DartCObject based), see doc to use other codecs
impl flutter_rust_bridge::IntoDart for crate::api::applinks_api::ApplinksRegistrationResult {
fn into_dart(self) -> flutter_rust_bridge::for_generated::DartAbi {
[
self.success.into_into_dart().into_dart(),
self.message.into_into_dart().into_dart(),
self.was_modified.into_into_dart().into_dart(),
]
.into_dart()
}
}
impl flutter_rust_bridge::for_generated::IntoDartExceptPrimitive
for crate::api::applinks_api::ApplinksRegistrationResult
{
}
impl flutter_rust_bridge::IntoIntoDart<crate::api::applinks_api::ApplinksRegistrationResult>
for crate::api::applinks_api::ApplinksRegistrationResult
{
fn into_into_dart(self) -> crate::api::applinks_api::ApplinksRegistrationResult {
self
}
}
// Codec=Dco (DartCObject based), see doc to use other codecs
impl flutter_rust_bridge::IntoDart for crate::api::unp4k_api::DcbRecordItem {
fn into_dart(self) -> flutter_rust_bridge::for_generated::DartAbi {
@@ -3416,6 +3525,15 @@ impl SseEncode for String {
}
}
impl SseEncode for crate::api::applinks_api::ApplinksRegistrationResult {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<bool>::sse_encode(self.success, serializer);
<String>::sse_encode(self.message, serializer);
<bool>::sse_encode(self.was_modified, serializer);
}
}
impl SseEncode for bool {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
@@ -3978,6 +4096,18 @@ mod io {
String::from_utf8(vec).unwrap()
}
}
impl CstDecode<crate::api::applinks_api::ApplinksRegistrationResult>
for wire_cst_applinks_registration_result
{
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> crate::api::applinks_api::ApplinksRegistrationResult {
crate::api::applinks_api::ApplinksRegistrationResult {
success: self.success.cst_decode(),
message: self.message.cst_decode(),
was_modified: self.was_modified.cst_decode(),
}
}
}
impl CstDecode<bool> for *mut bool {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> bool {
@@ -4321,6 +4451,20 @@ mod io {
}
}
}
impl NewWithNullPtr for wire_cst_applinks_registration_result {
fn new_with_null_ptr() -> Self {
Self {
success: Default::default(),
message: core::ptr::null_mut(),
was_modified: Default::default(),
}
}
}
impl Default for wire_cst_applinks_registration_result {
fn default() -> Self {
Self::new_with_null_ptr()
}
}
impl NewWithNullPtr for wire_cst_dcb_record_item {
fn new_with_null_ptr() -> Self {
Self {
@@ -4557,6 +4701,14 @@ mod io {
wire__crate__api__win32_api__add_nvme_patch_impl(port_)
}
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__applinks_api__check_applinks_registration(
port_: i64,
scheme: *mut wire_cst_list_prim_u_8_strict,
) {
wire__crate__api__applinks_api__check_applinks_registration_impl(port_, scheme)
}
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__win32_api__check_nvme_patch_status(
port_: i64,
@@ -5042,6 +5194,15 @@ mod io {
wire__crate__api__unp4k_api__p4k_open_impl(port_, p4k_path)
}
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__applinks_api__register_applinks(
port_: i64,
scheme: *mut wire_cst_list_prim_u_8_strict,
app_name: *mut wire_cst_list_prim_u_8_strict,
) {
wire__crate__api__applinks_api__register_applinks_impl(port_, scheme, app_name)
}
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__win32_api__remove_nvme_patch(
port_: i64,
@@ -5052,9 +5213,9 @@ mod io {
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__win32_api__resolve_shortcut(
port_: i64,
_lnk_path: *mut wire_cst_list_prim_u_8_strict,
lnk_path: *mut wire_cst_list_prim_u_8_strict,
) {
wire__crate__api__win32_api__resolve_shortcut_impl(port_, _lnk_path)
wire__crate__api__win32_api__resolve_shortcut_impl(port_, lnk_path)
}
#[unsafe(no_mangle)]
@@ -5154,6 +5315,14 @@ mod io {
wire__crate__api__ort_api__unload_translation_model_impl(port_, model_key)
}
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__applinks_api__unregister_applinks(
port_: i64,
scheme: *mut wire_cst_list_prim_u_8_strict,
) {
wire__crate__api__applinks_api__unregister_applinks_impl(port_, scheme)
}
#[unsafe(no_mangle)]
pub extern "C" fn frbgen_starcitizen_doctor_wire__crate__api__webview_api__web_view_configuration_default(
port_: i64,
@@ -5467,6 +5636,13 @@ mod io {
flutter_rust_bridge::for_generated::new_leak_box_ptr(wrap)
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct wire_cst_applinks_registration_result {
success: bool,
message: *mut wire_cst_list_prim_u_8_strict,
was_modified: bool,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct wire_cst_dcb_record_item {