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() {
WCHAR exe_path[MAX_PATH];
if (SUCCEEDED(GetModuleFileNameW(nullptr, exe_path, MAX_PATH))) {
std::wstring wide_exe_path(exe_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 "
"process");
}
return fs::path{};
}
fs::path GetAppDataRoamingDirectory() {
PWSTR appdata_roaming_path = nullptr;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appdata_roaming_path))) {
std::wstring wide_appdata_roaming_path(appdata_roaming_path);
CoTaskMemFree(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{};
}

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& command,
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
std::string filename = "";
@ -2869,7 +2869,6 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
#if defined(__linux__) || defined(__FreeBSD__) // Linux and FreeBSD
// Reference for the desktop file template:
// https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-1.0.html
try {
// Plus 'Type' is required
if (name.empty()) {
@ -2882,54 +2881,40 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
// Create shortcut file
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, "Type=Application\n");
fmt::print(shortcut_stream, "Version=1.0\n");
fmt::print(shortcut_stream, "Name={}\n", name);
if (!comment.empty()) {
fmt::print(shortcut_stream, "Comment={}\n", comment);
}
if (std::filesystem::is_regular_file(icon_path)) {
fmt::print(shortcut_stream, "Icon={}\n", icon_path.string());
}
fmt::print(shortcut_stream, "TryExec={}\n", command.string());
fmt::print(shortcut_stream, "Exec={}", command.string());
if (!arguments.empty()) {
fmt::print(shortcut_stream, " {}", arguments);
}
fmt::print(shortcut_stream, "\n");
if (!categories.empty()) {
fmt::print(shortcut_stream, "Categories={}\n", categories);
}
if (!keywords.empty()) {
fmt::print(shortcut_stream, "Keywords={}\n", keywords);
}
// Flush and close file
shortcut_stream.flush();
shortcut_stream.close();
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
HRESULT hr = CoInitialize(NULL);
if (SUCCEEDED(hr)) {
if (FAILED(hr)) {
LOG_ERROR(Frontend, "CoInitialize failed");
return false;
}
IShellLinkW* ps1 = nullptr;
IPersistFile* persist_file = nullptr;
@ -2945,69 +2930,59 @@ bool GMainWindow::CreateShortcutLink(const std::filesystem::path& shortcut_path,
CoUninitialize();
});
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLinkW, (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);
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkW,
(void**)&ps1);
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()) {
hres = ps1->SetArguments(warguments.data());
hres = ps1->SetArguments(Common::UTF8ToUTF16W(arguments).data());
}
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)) {
hres = ps1->SetIconLocation(wicon_path.data(), 0);
hres = ps1->SetIconLocation(Common::UTF8ToUTF16W(icon_path.string()).data(), 0);
}
if (SUCCEEDED(hres)) {
hres = ps1->QueryInterface(IID_IPersistFile, (void**)&persist_file);
}
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)) {
return true;
} else {
}
LOG_ERROR(Frontend, "Failed to create shortcut");
}
}
} else {
LOG_ERROR(Frontend, "Failed to create CoCreateInstance");
}
}
return false;
#else // Unsupported platform
return false;
#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
bool GMainWindow::CreateShortcutMessagesGUI(QWidget* parent, int imsg, const QString& game_title) {
QMessageBox::StandardButtons buttons;
int result = 0;
QMessageBox::StandardButtons buttons;
switch (imsg) {
case GMainWindow::CREATE_SHORTCUT_MSGBOX_FULLSCREEN_YES:
buttons = QMessageBox::Yes | QMessageBox::No;
result =
QMessageBox::information(parent, tr("Create Shortcut"),
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:
QMessageBox::information(parent, tr("Create Shortcut"),
tr("Successfully created a shortcut to %1").arg(game_title));
break;
return false;
case GMainWindow::CREATE_SHORTCUT_MSGBOX_APPVOLATILE_WARNING:
buttons = QMessageBox::StandardButton::Ok | QMessageBox::StandardButton::Cancel;
result =
@ -3015,20 +2990,19 @@ bool GMainWindow::CreateShortcutMessagesGUI(QWidget* parent, int imsg, const QSt
tr("This will create a shortcut to the current AppImage. This may "
"not work well if you update. Continue?"),
buttons);
return (result == QMessageBox::Ok);
return result == QMessageBox::Ok;
case GMainWindow::CREATE_SHORTCUT_MSGBOX_ADMIN:
buttons = QMessageBox::Ok;
QMessageBox::critical(parent, tr("Create Shortcut"),
tr("Cannot create shortcut in Apps. Restart yuzu as administrator."),
buttons);
break;
return false;
default:
buttons = QMessageBox::Ok;
QMessageBox::critical(parent, tr("Create Shortcut"),
tr("Failed to create a shortcut to %1").arg(game_title), buttons);
break;
}
return false;
}
}
bool GMainWindow::MakeShortcutIcoPath(const u64 program_id, const std::string_view game_file_name,