From 9fa69dbad6a992d3620d9baeb3b9c6c72963cb45 Mon Sep 17 00:00:00 2001 From: Jeroen Date: Thu, 24 Aug 2023 08:10:43 +0200 Subject: [PATCH] code style fixes --- src/yuzu/main.cpp | 33 ++++++++++++++++----------------- src/yuzu/util/util.cpp | 42 +++++++++++++++++++++--------------------- 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 0b48f52da3..b26038c231 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -2750,23 +2750,20 @@ void GMainWindow::OnGameListCreateShortcut(u64 program_id, const std::string& ga target_directory / (program_id == 0 ? fmt::format("yuzu-{}.desktop", game_file_name) : fmt::format("yuzu-{:016X}.desktop", program_id)); #elif defined(WIN32) - // replace colons, which are illegal in windows filenames, by a dash. - for (auto& c : title) - if (c == ':') - c = '-'; + std::replace(title.begin(), title.end(), ':', '-'); const std::filesystem::path shortcut_path = target_directory / (title + ".lnk").c_str(); #else const std::filesystem::path shortcut_path{}; #endif std::filesystem::path icon_path = - icons_path / ((program_id == 0 ? fmt::format("yuzu-{}", game_file_name) - : fmt::format("yuzu-{:016X}", program_id)) + - icon_extension); + icons_path / ((program_id == 0 ? fmt::format("yuzu-{}", game_file_name) + : fmt::format("yuzu-{:016X}", program_id)) + + icon_extension); // Get icon from game file std::vector icon_image_file{}; - if (control.second != nullptr) { + if (control.second) { icon_image_file = control.second->ReadAllBytes(); } else if (loader->ReadIcon(icon_image_file) != Loader::ResultStatus::Success) { LOG_WARNING(Frontend, "Could not read icon from {:s}", game_path); @@ -2775,7 +2772,6 @@ void GMainWindow::OnGameListCreateShortcut(u64 program_id, const std::string& ga QImage icon_data = QImage::fromData(icon_image_file.data(), static_cast(icon_image_file.size())); - #if defined(__linux__) || defined(__FreeBSD__) // Convert and write the icon if (!icon_data.save(QString::fromStdString(icon_path.string()))) { @@ -3806,17 +3802,18 @@ bool GMainWindow::CreateShortcut(const std::string& shortcut_path, const std::st return true; #endif #if defined(WIN32) - auto wcommand = std::wstring(command.begin(), command.end()); - auto warguments = std::wstring(arguments.begin(), arguments.end()); - auto wcomment = std::wstring(comment.begin(), comment.end()); - auto wshortcut_path = std::wstring(shortcut_path.begin(), shortcut_path.end()); - auto wicon_path = std::wstring(icon_path.begin(), icon_path.end()); + auto wcommand = Common::UTF8ToUTF16W(command); + auto warguments = Common::UTF8ToUTF16W(arguments); + auto wcomment = Common::UTF8ToUTF16W(comment); + auto wshortcut_path = Common::UTF8ToUTF16W(shortcut_path); + auto wicon_path = Common::UTF8ToUTF16W(icon_path); IShellLink* pShellLink; auto hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&pShellLink); - if (FAILED(hres)) + if (FAILED(hres)) { return false; + } pShellLink->SetPath(wcommand.data()); // Path to the object we are referring to pShellLink->SetArguments(warguments.data()); pShellLink->SetDescription(wcomment.data()); @@ -3824,12 +3821,14 @@ bool GMainWindow::CreateShortcut(const std::string& shortcut_path, const std::st IPersistFile* pPersistFile; hres = pShellLink->QueryInterface(IID_IPersistFile, (void**)&pPersistFile); - if (FAILED(hres)) + if (FAILED(hres)) { return false; + } hres = pPersistFile->Save(wshortcut_path.data(), TRUE); - if (FAILED(hres)) + if (FAILED(hres)) { return false; + } pPersistFile->Release(); pShellLink->Release(); diff --git a/src/yuzu/util/util.cpp b/src/yuzu/util/util.cpp index aeb8a7a0d3..0786675b69 100644 --- a/src/yuzu/util/util.cpp +++ b/src/yuzu/util/util.cpp @@ -5,7 +5,10 @@ #include #include #include "yuzu/util/util.h" +#if defined(WIN32) #include +#include +#endif QFont GetMonospaceFont() { QFont font(QStringLiteral("monospace")); @@ -40,16 +43,14 @@ QPixmap CreateCirclePixmapFromColor(const QColor& color) { } #if defined(WIN32) -#include - #pragma pack(push, 2) -struct ICONDIR { - WORD idReserved; - WORD idType; - WORD idCount; +struct IconDir { + WORD id_reserved; + WORD id_type; + WORD id_count; }; -struct ICONDIRENTRY { +struct IconDirEntry { BYTE bWidth; BYTE bHeight; BYTE bColorCount; @@ -59,7 +60,6 @@ struct ICONDIRENTRY { DWORD dwBytesInRes; DWORD dwImageOffset; }; - #pragma pack(pop) bool SaveIconToFile(const char* path, QImage image) { @@ -74,37 +74,37 @@ bool SaveIconToFile(const char* path, QImage image) { bmih.biWidth = sourceImage.width(); bmih.biHeight = sourceImage.height() * 2; bmih.biPlanes = 1; - bmih.biBitCount = 32; + bmih.biBitCount = bytesPerPixel * 8; bmih.biCompression = BI_RGB; // Create an ICO header - ICONDIR iconDir; - iconDir.idReserved = 0; - iconDir.idType = 1; - iconDir.idCount = 1; + IconDir iconDir; + iconDir.id_reserved = 0; + iconDir.id_type = 1; + iconDir.id_count = 1; // Create an ICONDIRENTRY - ICONDIRENTRY iconEntry; + IconDirEntry iconEntry; iconEntry.bWidth = sourceImage.width(); iconEntry.bHeight = sourceImage.height() * 2; iconEntry.bColorCount = 0; iconEntry.bReserved = 0; iconEntry.wPlanes = 1; - iconEntry.wBitCount = 32; + iconEntry.wBitCount = bytesPerPixel * 8; iconEntry.dwBytesInRes = sizeof(BITMAPINFOHEADER) + imageSize; - iconEntry.dwImageOffset = sizeof(ICONDIR) + sizeof(ICONDIRENTRY); + iconEntry.dwImageOffset = sizeof(IconDir) + sizeof(IconDirEntry); // Save the icon data to a file std::ofstream iconFile(path, std::ios::binary); if (iconFile.fail()) return false; - iconFile.write(reinterpret_cast(&iconDir), sizeof(ICONDIR)); - iconFile.write(reinterpret_cast(&iconEntry), sizeof(ICONDIRENTRY)); + iconFile.write(reinterpret_cast(&iconDir), sizeof(IconDir)); + iconFile.write(reinterpret_cast(&iconEntry), sizeof(IconDirEntry)); iconFile.write(reinterpret_cast(&bmih), sizeof(BITMAPINFOHEADER)); for (int y = 0; y < image.height(); y++) { - auto line = (char*)sourceImage.scanLine(sourceImage.height() - 1 - y); - iconFile.write(line, sourceImage.width() * 4); + auto line = reinterpret_cast(sourceImage.scanLine(sourceImage.height() - 1 - y)); + iconFile.write(line, sourceImage.width() * bytesPerPixel); } iconFile.close(); @@ -115,4 +115,4 @@ bool SaveIconToFile(const char* path, QImage image) { bool SaveAsIco(QImage image) { return false; } -#endif \ No newline at end of file +#endif