| 9 | #include "../ProgramLog/error/std.hpp" |
| 10 | |
| 11 | std::filesystem::path LoadableDll::GetDllPath(const std::optional<std::filesystem::path> &storageFolder, bool copyDll, std::wstring_view dll) |
| 12 | { |
| 13 | std::error_code err; |
| 14 | |
| 15 | const auto [loc, hr] = win32::GetExeLocation(); |
| 16 | HresultVerify(hr, spdlog::level::critical, L"Failed to determine executable location!"); |
| 17 | |
| 18 | std::filesystem::path dllPath = loc.parent_path() / dll; |
| 19 | |
| 20 | if (copyDll) |
| 21 | { |
| 22 | // copy the file over to a place Explorer can read. It can't be injected from WindowsApps. |
| 23 | // when running portable, copy it to the temp folder anyways, to allow ejecting the device TTB is running from. |
| 24 | std::filesystem::path tempFolder; |
| 25 | if (storageFolder) |
| 26 | { |
| 27 | tempFolder = *storageFolder / L"TempState"; |
| 28 | } |
| 29 | else |
| 30 | { |
| 31 | tempFolder = std::filesystem::temp_directory_path(err); |
| 32 | if (!err) |
| 33 | { |
| 34 | tempFolder /= APP_NAME; |
| 35 | } |
| 36 | else |
| 37 | { |
| 38 | StdErrorCodeHandle(err, spdlog::level::critical, L"Failed to get temp folder path."); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | std::filesystem::create_directories(tempFolder, err); |
| 43 | if (err) [[unlikely]] |
| 44 | { |
| 45 | StdErrorCodeHandle(err, spdlog::level::critical, L"Failed to create " APP_NAME " temp folder."); |
| 46 | } |
| 47 | |
| 48 | auto tempDllPath = tempFolder / dll; |
| 49 | std::filesystem::copy_file(dllPath, tempDllPath, std::filesystem::copy_options::update_existing, err); |
| 50 | if (err) [[unlikely]] |
| 51 | { |
| 52 | if (err.category() == std::system_category() && err.value() == ERROR_SHARING_VIOLATION) |
| 53 | { |
| 54 | Localization::ShowLocalizedMessageBox(IDS_RESTART_REQUIRED, MB_OK | MB_ICONWARNING | MB_SETFOREGROUND).join(); |
| 55 | ExitProcess(1); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | StdErrorCodeHandle(err, spdlog::level::critical, std::format(L"Failed to copy {}", dll)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return tempDllPath; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | return dllPath; |
| 68 | } |
nothing calls this directly
no outgoing calls
no test coverage detected