cargo_mobile2/os/windows/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
mod env;
pub(super) mod info;
pub mod ln;

use crate::{env::ExplicitEnv, DuctExpressionExt};
use std::{
    ffi::{OsStr, OsString},
    os::windows::ffi::{OsStrExt, OsStringExt},
    path::Path,
    slice::from_raw_parts,
};
use thiserror::Error;
use windows::{
    core::{self, w, PCWSTR, PWSTR},
    Win32::{
        Foundation::{LocalFree, ERROR_NO_ASSOCIATION, HLOCAL, MAX_PATH},
        System::Registry::HKEY_LOCAL_MACHINE,
        UI::Shell::{
            AssocQueryStringW, CommandLineToArgvW, SHRegGetPathW, ASSOCF_INIT_IGNOREUNKNOWN,
            ASSOCSTR_COMMAND,
        },
    },
};

pub use env::Env;
use which::which;

#[derive(Debug, Error)]
pub enum DetectEditorError {
    #[error("No default editor is set for \".rs\" and \".txt\"")]
    NoDefaultEditorSet,
    #[error("An error occured while calling AssocQueryStringW: {0}")]
    IOError(#[source] std::io::Error),
}

impl From<core::Error> for DetectEditorError {
    fn from(err: core::Error) -> Self {
        Self::IOError(err.into())
    }
}

#[derive(Debug, Error)]
pub enum OpenFileError {
    #[error("Launch Failed: {0}")]
    LaunchFailed(#[source] std::io::Error),
    #[error("An error occured while calling OS API: {0}")]
    IOError(#[source] std::io::Error),
}

pub struct Application {
    argv: Vec<OsString>,
}

const RUST_EXT: PCWSTR = w!(".rs");
const TEXT_EXT: PCWSTR = w!(".txt");

impl Application {
    pub fn detect_editor() -> Result<Self, DetectEditorError> {
        let editor_command =
            Self::detect_associated_command(RUST_EXT).or_else(|err| match err {
                DetectEditorError::NoDefaultEditorSet => Self::detect_associated_command(TEXT_EXT),
                _ => Err(err),
            })?;
        let argv: Vec<_> = NativeArgv::new(&editor_command).into();
        Ok(Self { argv })
    }

    pub fn open_file(&self, path: impl AsRef<Path>) -> Result<(), OpenFileError> {
        let args = self.argv[1..]
            .iter()
            .map(|arg| Self::replace_command_arg(arg, &path.as_ref().as_os_str()))
            .collect::<Vec<_>>();
        duct::cmd(&self.argv[0], args)
            .run_and_detach()
            .map_err(OpenFileError::LaunchFailed)?;
        Ok(())
    }

    fn detect_associated_command(ext: PCWSTR) -> Result<Vec<u16>, DetectEditorError> {
        let mut len: u32 = 0;
        if let Err(e) = unsafe {
            AssocQueryStringW(
                ASSOCF_INIT_IGNOREUNKNOWN,
                ASSOCSTR_COMMAND,
                // In Shlwapi.h, this parameter's type is `LPCWSTR`.
                // So it's not modified actually.
                PCWSTR::from_raw(ext.as_ptr()),
                PCWSTR::null(),
                PWSTR::null(),
                &mut len as _,
            )
            .ok()
        } {
            if e.code().0 == (0x80070000 | ERROR_NO_ASSOCIATION.0) as i32 {
                return Err(DetectEditorError::NoDefaultEditorSet);
            }
            return Err(DetectEditorError::IOError(e.into()));
        }
        let mut command: Vec<u16> = vec![0; len as usize];
        unsafe {
            AssocQueryStringW(
                ASSOCF_INIT_IGNOREUNKNOWN,
                ASSOCSTR_COMMAND,
                // In Shlwapi.h, this parameter's type is `LPCWSTR`.
                // So it's not modified actually.
                PCWSTR::from_raw(RUST_EXT.as_ptr()),
                PCWSTR::null(),
                PWSTR(command.as_mut_ptr()),
                &mut len as _,
            )
            .ok()?;
        }
        Ok(command)
    }

    // Replace %0 or %1 to arg1, and other % is unescape
    fn replace_command_arg(arg: &OsStr, arg1: &OsStr) -> OsString {
        let mut is_percent = false;
        let mut iter = arg.encode_wide();
        let mut buffer = vec![];
        const ZERO: u16 = '0' as u16;
        const ONE: u16 = '1' as u16;
        const TWO: u16 = '2' as u16;
        const NINE: u16 = '9' as u16;
        const PERCENT: u16 = '%' as u16;
        loop {
            match (iter.next(), is_percent) {
                (Some(ZERO..=ONE), true) => {
                    buffer.extend(arg1.encode_wide());
                }
                (Some(TWO..=NINE), true) => {
                    // Nothing to do.
                }
                (Some(PERCENT), false) => {
                    is_percent = true;
                    continue;
                }
                (Some(c), _) => {
                    buffer.push(c);
                }
                (None, _) => break,
            }
            is_percent = false;
        }
        OsString::from_wide(&buffer)
    }
}

pub fn open_file_with(
    application: impl AsRef<OsStr>,
    path: impl AsRef<OsStr>,
    env: &Env,
) -> Result<(), OpenFileError> {
    // In windows, there is no standerd way to find application by name.
    match application.as_ref().to_str() {
        Some("Android Studio") => open_file_with_android_studio(path, env),
        _ => {
            unimplemented!()
        }
    }
}

const ANDROID_STUDIO_UNINSTALL_KEY_PATH: PCWSTR =
    w!("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Android Studio");
const ANDROID_STUDIO_UNINSTALLER_VALUE: PCWSTR = w!("UninstallString");
#[cfg(target_pointer_width = "64")]
const STUDIO_EXE_PATH: &str = "bin/studio64.exe";
#[cfg(target_pointer_width = "32")]
const STUDIO_EXE_PATH: &str = "bin/studio.exe";

fn open_file_with_android_studio(path: impl AsRef<OsStr>, env: &Env) -> Result<(), OpenFileError> {
    let mut application_path = which("studio.cmd").unwrap_or_default();
    if !application_path.is_file() {
        let mut buffer = [0; MAX_PATH as usize];
        unsafe {
            SHRegGetPathW(
                HKEY_LOCAL_MACHINE,
                PCWSTR::from_raw(ANDROID_STUDIO_UNINSTALL_KEY_PATH.as_ptr()),
                PCWSTR::from_raw(ANDROID_STUDIO_UNINSTALLER_VALUE.as_ptr()),
                &mut buffer,
                0,
            )
            .ok()
            .map_err(|e| OpenFileError::IOError(e.into()))?
        };
        let len = NullTerminatedWTF16Iterator(buffer.as_ptr()).count();
        let uninstaller_path = OsString::from_wide(&buffer[..len]);
        application_path = Path::new(&uninstaller_path)
            .parent()
            .expect("Failed to get Android Studio uninstaller's parent path")
            .join(STUDIO_EXE_PATH);
    }
    duct::cmd(
        application_path,
        [
            dunce::canonicalize(Path::new(path.as_ref()))
                .expect("Failed to canonicalize file path"),
        ],
    )
    .vars(env.explicit_env())
    .run_and_detach()
    .map_err(OpenFileError::LaunchFailed)?;
    Ok(())
}

pub fn command_path(name: &str) -> std::io::Result<std::process::Output> {
    duct::cmd("where.exe", [name]).run()
}

struct NativeArgv {
    argv: *mut PWSTR,
    len: i32,
}

impl NativeArgv {
    // The buffer must be null terminated.
    fn new(buffer: &[u16]) -> Self {
        let mut len = 0;
        // In shellap.h, lpcmdline's type is `LPCWSTR`.
        // So it's not modified actually.
        let argv = unsafe { CommandLineToArgvW(PCWSTR::from_raw(buffer.as_ptr()), &mut len as _) };
        Self { argv, len }
    }
}

impl Drop for NativeArgv {
    fn drop(&mut self) {
        let _ = unsafe { LocalFree(HLOCAL(self.argv as _)) };
    }
}

impl From<NativeArgv> for Vec<OsString> {
    fn from(native_argv: NativeArgv) -> Self {
        let mut argv = Vec::with_capacity(native_argv.len as usize);
        let argv_slice = unsafe { from_raw_parts(native_argv.argv, native_argv.len as _) };
        for pwstr in argv_slice {
            let len = NullTerminatedWTF16Iterator(pwstr.0).count();
            let arg = OsString::from_wide(unsafe { std::slice::from_raw_parts(pwstr.0, len) });
            argv.push(arg);
        }
        argv
    }
}

struct NullTerminatedWTF16Iterator(*const u16);

impl Iterator for NullTerminatedWTF16Iterator {
    type Item = u16;
    fn next(&mut self) -> Option<Self::Item> {
        match unsafe { *self.0 } {
            0 => None,
            c => {
                self.0 = unsafe { self.0.offset(1) };
                Some(c)
            }
        }
    }
}

// Directly invoking code.cmd behaves strangely.
// For example, if running `cargo mobile new foo` in C:\Users\MyHome,
// %~dp0 will expand to C:\Users\MyHome\foo in code.cmd, which is completely broken.
// Running it through powershell.exe does not have this problem.
pub fn code_command() -> duct::Expression {
    duct::cmd!("code.cmd")
}

pub fn replace_path_separator(path: OsString) -> OsString {
    let buf = path
        .encode_wide()
        .map(|c| if c == '\\' as u16 { '/' as u16 } else { c })
        .collect::<Vec<_>>();
    OsString::from_wide(&buf)
}

pub mod consts {
    pub const CLANG: &str = "clang.cmd";
    pub const CLANGXX: &str = "clang++.cmd";
    pub const LD: &str = "ld.exe";
    pub const AR: &str = "ar.exe";
    pub const READELF: &str = "readelf.exe";
    pub const NDK_STACK: &str = "ndk-stack.cmd";
}