More @liamwhite suggestions applied.

This commit is contained in:
boludoz 2023-10-16 23:42:45 -03:00
parent 1ae0f0f3f6
commit 1afe6d51ee
2 changed files with 98 additions and 130 deletions

View File

@ -252,29 +252,23 @@ void SetYuzuPath(YuzuPath yuzu_path, const fs::path& new_path) {
fs::path GetExeDirectory() { fs::path GetExeDirectory() {
WCHAR exe_path[MAX_PATH]; WCHAR exe_path[MAX_PATH];
if (SUCCEEDED(GetModuleFileNameW(nullptr, exe_path, MAX_PATH))) { if (SUCCEEDED(GetModuleFileNameW(nullptr, exe_path, MAX_PATH))) {
std::wstring wide_exe_path(exe_path); std::wstring wide_exe_path(exe_path);
return fs::path{Common::UTF16ToUTF8(wide_exe_path)}.parent_path(); return fs::path{Common::UTF16ToUTF8(wide_exe_path)}.parent_path();
} else { }
LOG_ERROR(Common_Filesystem, "Failed to get the path to the executable of the current " LOG_ERROR(Common_Filesystem, "Failed to get the path to the executable of the current "
"process"); "process");
}
return fs::path{}; return fs::path{};
} }
fs::path GetAppDataRoamingDirectory() { fs::path GetAppDataRoamingDirectory() {
PWSTR appdata_roaming_path = nullptr; PWSTR appdata_roaming_path = nullptr;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appdata_roaming_path))) { if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appdata_roaming_path))) {
std::wstring wide_appdata_roaming_path(appdata_roaming_path); std::wstring wide_appdata_roaming_path(appdata_roaming_path);
CoTaskMemFree(appdata_roaming_path); CoTaskMemFree(appdata_roaming_path);
return fs::path{Common::UTF16ToUTF8(wide_appdata_roaming_path)}; return fs::path{Common::UTF16ToUTF8(wide_appdata_roaming_path)};
} else {
LOG_ERROR(Common_Filesystem, "Failed to get the path to the %APPDATA% directory");
} }
LOG_ERROR(Common_Filesystem, "Failed to get the path to the %APPDATA% directory");
return fs::path{}; return fs::path{};
} }

View File

@ -2845,7 +2845,7 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
const std::filesystem::path& icon_path, const std::filesystem::path& icon_path,
const std::filesystem::path& command, const std::filesystem::path& command,
const std::string& arguments, const std::string& categories, const std::string& arguments, const std::string& categories,
const std::string& keywords, const std::string& name) { const std::string& keywords, const std::string& name) try {
// Copy characters if they are not illegal in Windows filenames // Copy characters if they are not illegal in Windows filenames
std::string filename = ""; std::string filename = "";
@ -2869,7 +2869,6 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
#if defined(__linux__) || defined(__FreeBSD__) // Linux and FreeBSD #if defined(__linux__) || defined(__FreeBSD__) // Linux and FreeBSD
// Reference for the desktop file template: // Reference for the desktop file template:
// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html // https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html
try {
// Plus 'Type' is required // Plus 'Type' is required
if (name.empty()) { if (name.empty()) {
@ -2882,54 +2881,40 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
// Create shortcut file // Create shortcut file
std::ofstream shortcut_stream(shortcut_path_full, std::ios::binary | std::ios::trunc); std::ofstream shortcut_stream(shortcut_path_full, std::ios::binary | std::ios::trunc);
if (!shortcut_stream.is_open()) {
if (shortcut_stream.is_open()) { LOG_ERROR(Frontend, "Failed to create shortcut");
return false;
}
fmt::print(shortcut_stream, "[Desktop Entry]\n"); fmt::print(shortcut_stream, "[Desktop Entry]\n");
fmt::print(shortcut_stream, "Type=Application\n"); fmt::print(shortcut_stream, "Type=Application\n");
fmt::print(shortcut_stream, "Version=1.0\n"); fmt::print(shortcut_stream, "Version=1.0\n");
fmt::print(shortcut_stream, "Name={}\n", name); fmt::print(shortcut_stream, "Name={}\n", name);
if (!comment.empty()) { if (!comment.empty()) {
fmt::print(shortcut_stream, "Comment={}\n", comment); fmt::print(shortcut_stream, "Comment={}\n", comment);
} }
if (std::filesystem::is_regular_file(icon_path)) { if (std::filesystem::is_regular_file(icon_path)) {
fmt::print(shortcut_stream, "Icon={}\n", icon_path.string()); fmt::print(shortcut_stream, "Icon={}\n", icon_path.string());
} }
fmt::print(shortcut_stream, "TryExec={}\n", command.string()); fmt::print(shortcut_stream, "TryExec={}\n", command.string());
fmt::print(shortcut_stream, "Exec={}", command.string()); fmt::print(shortcut_stream, "Exec={}", command.string());
if (!arguments.empty()) { if (!arguments.empty()) {
fmt::print(shortcut_stream, " {}", arguments); fmt::print(shortcut_stream, " {}", arguments);
} }
fmt::print(shortcut_stream, "\n"); fmt::print(shortcut_stream, "\n");
if (!categories.empty()) { if (!categories.empty()) {
fmt::print(shortcut_stream, "Categories={}\n", categories); fmt::print(shortcut_stream, "Categories={}\n", categories);
} }
if (!keywords.empty()) { if (!keywords.empty()) {
fmt::print(shortcut_stream, "Keywords={}\n", keywords); fmt::print(shortcut_stream, "Keywords={}\n", keywords);
} }
// Flush and close file
shortcut_stream.flush();
shortcut_stream.close();
return true; return true;
} else {
LOG_ERROR(Frontend, "Failed to create shortcut");
}
shortcut_stream.close();
} catch (const std::exception& e) {
LOG_ERROR(Frontend, "Failed to create shortcut: {}", e.what());
}
return false;
#elif defined(_WIN32) // Windows #elif defined(_WIN32) // Windows
HRESULT hr = CoInitialize(NULL); HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr)) { if (FAILED(hr)) {
LOG_ERROR(Frontend, "CoInitialize failed");
return false;
}
IShellLinkW* ps1 = nullptr; IShellLinkW* ps1 = nullptr;
IPersistFile* persist_file = nullptr; IPersistFile* persist_file = nullptr;
@ -2945,69 +2930,59 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
CoUninitialize(); CoUninitialize();
}); });
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW,
IID_IShellLinkW, (void**)&ps1); (void**)&ps1);
std::wstring wshortcut_path_full =
Common::UTF8ToUTF16W((shortcut_path_full).string() + ".lnk");
std::wstring wicon_path = Common::UTF8ToUTF16W(icon_path.string());
std::wstring wcommand = Common::UTF8ToUTF16W(command.string());
std::wstring warguments = Common::UTF8ToUTF16W(arguments);
std::wstring wcomment = Common::UTF8ToUTF16W(comment);
if (SUCCEEDED(hres)) { if (SUCCEEDED(hres)) {
hres = ps1->SetPath(wcommand.data()); hres = ps1->SetPath(Common::UTF8ToUTF16W(command.string()).data());
} else {
LOG_ERROR(Frontend, "Failed to create CoCreateInstance");
return false;
}
if (SUCCEEDED(hres) && !arguments.empty()) { if (SUCCEEDED(hres) && !arguments.empty()) {
hres = ps1->SetArguments(warguments.data()); hres = ps1->SetArguments(Common::UTF8ToUTF16W(arguments).data());
} }
if (SUCCEEDED(hres) && !comment.empty()) { if (SUCCEEDED(hres) && !comment.empty()) {
hres = ps1->SetDescription(wcomment.data()); hres = ps1->SetDescription(Common::UTF8ToUTF16W(comment).data());
} }
if (SUCCEEDED(hres) && std::filesystem::is_regular_file(icon_path)) { if (SUCCEEDED(hres) && std::filesystem::is_regular_file(icon_path)) {
hres = ps1->SetIconLocation(wicon_path.data(), 0); hres = ps1->SetIconLocation(Common::UTF8ToUTF16W(icon_path.string()).data(), 0);
} }
if (SUCCEEDED(hres)) { if (SUCCEEDED(hres)) {
hres = ps1->QueryInterface(IID_IPersistFile, (void**)&persist_file); hres = ps1->QueryInterface(IID_IPersistFile, (void**)&persist_file);
} }
if (SUCCEEDED(hres) && persist_file != nullptr) { if (SUCCEEDED(hres) && persist_file != nullptr) {
hres = persist_file->Save(wshortcut_path_full.data(), TRUE); hres = persist_file->Save(
Common::UTF8ToUTF16W((shortcut_path_full).string() + ".lnk").data(), TRUE);
}
if (SUCCEEDED(hres)) { if (SUCCEEDED(hres)) {
return true; return true;
} else { }
LOG_ERROR(Frontend, "Failed to create shortcut"); LOG_ERROR(Frontend, "Failed to create shortcut");
}
}
} else {
LOG_ERROR(Frontend, "Failed to create CoCreateInstance");
}
}
return false; return false;
#else // Unsupported platform #else // Unsupported platform
return false; return false;
#endif #endif
} catch (const std::exception& e) {
LOG_ERROR(Frontend, "Failed to create shortcut: {}", e.what());
return false;
} }
// Messages in pre-defined message boxes for less code spaghetti // Messages in pre-defined message boxes for less code spaghetti
bool GMainWindow::CreateShortcutMessagesGUI(QWidget* parent, int imsg, const QString& game_title) { bool GMainWindow::CreateShortcutMessagesGUI(QWidget* parent, int imsg, const QString& game_title) {
QMessageBox::StandardButtons buttons;
int result = 0; int result = 0;
QMessageBox::StandardButtons buttons;
switch (imsg) { switch (imsg) {
case GMainWindow::CREATE_SHORTCUT_MSGBOX_FULLSCREEN_YES: case GMainWindow::CREATE_SHORTCUT_MSGBOX_FULLSCREEN_YES:
buttons = QMessageBox::Yes | QMessageBox::No; buttons = QMessageBox::Yes | QMessageBox::No;
result = result =
QMessageBox::information(parent, tr("Create Shortcut"), QMessageBox::information(parent, tr("Create Shortcut"),
tr("Do you want to launch the game in fullscreen?"), buttons); tr("Do you want to launch the game in fullscreen?"), buttons);
return (result == QMessageBox::Yes); return result == QMessageBox::Yes;
case GMainWindow::CREATE_SHORTCUT_MSGBOX_SUCCESS: case GMainWindow::CREATE_SHORTCUT_MSGBOX_SUCCESS:
QMessageBox::information(parent, tr("Create Shortcut"), QMessageBox::information(parent, tr("Create Shortcut"),
tr("Successfully created a shortcut to %1").arg(game_title)); tr("Successfully created a shortcut to %1").arg(game_title));
break; return false;
case GMainWindow::CREATE_SHORTCUT_MSGBOX_APPVOLATILE_WARNING: case GMainWindow::CREATE_SHORTCUT_MSGBOX_APPVOLATILE_WARNING:
buttons = QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel; buttons = QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel;
result = result =
@ -3015,21 +2990,20 @@ bool GMainWindow::CreateShortcutMessagesGUI(QWidget* parent, int imsg, const QSt
tr("This will create a shortcut to the current AppImage. This may " tr("This will create a shortcut to the current AppImage. This may "
"not work well if you update. Continue?"), "not work well if you update. Continue?"),
buttons); buttons);
return (result == QMessageBox::Ok); return result == QMessageBox::Ok;
case GMainWindow::CREATE_SHORTCUT_MSGBOX_ADMIN: case GMainWindow::CREATE_SHORTCUT_MSGBOX_ADMIN:
buttons = QMessageBox::Ok; buttons = QMessageBox::Ok;
QMessageBox::critical(parent, tr("Create Shortcut"), QMessageBox::critical(parent, tr("Create Shortcut"),
tr("Cannot create shortcut in Apps. Restart yuzu as administrator."), tr("Cannot create shortcut in Apps. Restart yuzu as administrator."),
buttons); buttons);
break; return false;
default: default:
buttons = QMessageBox::Ok; buttons = QMessageBox::Ok;
QMessageBox::critical(parent, tr("Create Shortcut"), QMessageBox::critical(parent, tr("Create Shortcut"),
tr("Failed to create a shortcut to %1").arg(game_title), buttons); tr("Failed to create a shortcut to %1").arg(game_title), buttons);
break;
}
return false; return false;
} }
}
bool GMainWindow::MakeShortcutIcoPath(const u64 program_id, const std::string_view game_file_name, bool GMainWindow::MakeShortcutIcoPath(const u64 program_id, const std::string_view game_file_name,
std::filesystem::path& out_icon_path) { std::filesystem::path& out_icon_path) {