| 3 | |
| 4 | |
| 5 | int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) |
| 6 | { |
| 7 | #ifdef _DEBUG |
| 8 | g_enable_log = true; |
| 9 | #endif |
| 10 | /*AllocConsole(); |
| 11 | freopen("conout$", "w", stdout); |
| 12 | SetConsoleOutputCP(CP_UTF8);*/ |
| 13 | |
| 14 | using namespace nana; |
| 15 | std::setlocale(LC_ALL, "en_US.UTF-8"); |
| 16 | int argc; |
| 17 | LPWSTR *argv {CommandLineToArgvW(GetCommandLineW(), &argc)}; |
| 18 | fs::path modpath {util::app_path()}, appdir {modpath.parent_path()}; |
| 19 | fs::current_path(appdir); |
| 20 | std::error_code ec; |
| 21 | if(fs::exists(appdir / "7zxa.dll")) |
| 22 | fs::remove(appdir / "7zxa.dll", ec); |
| 23 | |
| 24 | if(argc > 1) |
| 25 | { |
| 26 | const std::wstring first_arg {argv[1]}; |
| 27 | if(first_arg == L"debug_log") |
| 28 | { |
| 29 | g_enable_log = true; |
| 30 | } |
| 31 | else if(first_arg == L"update") |
| 32 | { |
| 33 | const fs::path arc_path {argv[2]}, target_dir {argv[3]}; |
| 34 | const bool self_only {argc == 5 && std::wstring {argv[4]} == L"self_only"}; |
| 35 | const auto pid {util::other_instance(target_dir / modpath.filename())}; |
| 36 | if(pid) |
| 37 | { |
| 38 | auto hwnds {util::hwnds_from_pid(pid)}; |
| 39 | for(auto hwnd : hwnds) |
| 40 | if(IsWindow(hwnd)) |
| 41 | SendMessage(hwnd, WM_CLOSE, 0, 0); |
| 42 | } |
| 43 | auto res {util::extract_7z(arc_path, target_dir, 0, self_only)}; |
| 44 | fs::remove(arc_path, ec); |
| 45 | fs::remove(fs::temp_directory_path() / "7z.dll", ec); |
| 46 | if(!res.empty()) |
| 47 | { |
| 48 | msgbox mbox {"ytdlp-interface - updating failed"}; |
| 49 | mbox.icon(msgbox::icon_error); |
| 50 | (mbox << "failed to extract the downloaded 7z archive: " << res)(); |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | auto newself {target_dir / "ytdlp-interface.exe"}; |
| 55 | ShellExecuteW(NULL, L"open", newself.wstring().data(), L"cleanup", NULL, SW_SHOW); |
| 56 | } |
| 57 | LocalFree(argv); |
| 58 | return 0; |
| 59 | } |
| 60 | else if(first_arg == L"cleanup") |
| 61 | { |
| 62 | fs::remove(fs::temp_directory_path() / "ytdlp-interface.exe", ec); |