diff options
| author | 2023-10-15 02:02:22 -0300 | |
|---|---|---|
| committer | 2023-10-15 02:02:22 -0300 | |
| commit | 3062a35eb1297067446156c43e9d0df2f684edff (patch) | |
| tree | f2ca58e0b8a6c413f3c6783a1501204729e0394c /src/common | |
| parent | Merge pull request #11780 from Darkness4/master (diff) | |
| download | yuzu-3062a35eb1297067446156c43e9d0df2f684edff.tar.gz yuzu-3062a35eb1297067446156c43e9d0df2f684edff.tar.xz yuzu-3062a35eb1297067446156c43e9d0df2f684edff.zip | |
Improved shortcut: add games in applist for Windows, question for start game at fullscreen & better unicode support for some Windows path funcs.
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/fs/fs_util.cpp | 59 | ||||
| -rw-r--r-- | src/common/fs/fs_util.h | 22 | ||||
| -rw-r--r-- | src/common/fs/path_util.cpp | 87 | ||||
| -rw-r--r-- | src/common/fs/path_util.h | 15 |
4 files changed, 168 insertions, 15 deletions
diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index 813a713c3..442f63728 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp | |||
| @@ -36,4 +36,63 @@ std::string PathToUTF8String(const std::filesystem::path& path) { | |||
| 36 | return ToUTF8String(path.u8string()); | 36 | return ToUTF8String(path.u8string()); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | std::u8string U8FilenameSantizer(const std::u8string_view u8filename) { | ||
| 40 | std::u8string u8path_santized{u8filename.begin(), u8filename.end()}; | ||
| 41 | size_t eSizeSanitized = u8path_santized.size(); | ||
| 42 | |||
| 43 | // Special case for ":", for example: 'Pepe: La secuela' --> 'Pepe - La | ||
| 44 | // secuela' or 'Pepe : La secuela' --> 'Pepe - La secuela' | ||
| 45 | for (size_t i = 0; i < eSizeSanitized; i++) { | ||
| 46 | switch (u8path_santized[i]) { | ||
| 47 | case u8':': | ||
| 48 | if (i == 0 || i == eSizeSanitized - 1) { | ||
| 49 | u8path_santized.replace(i, 1, u8"_"); | ||
| 50 | } else if (u8path_santized[i - 1] == u8' ') { | ||
| 51 | u8path_santized.replace(i, 1, u8"-"); | ||
| 52 | } else { | ||
| 53 | u8path_santized.replace(i, 1, u8" -"); | ||
| 54 | eSizeSanitized++; | ||
| 55 | } | ||
| 56 | break; | ||
| 57 | case u8'\\': | ||
| 58 | case u8'/': | ||
| 59 | case u8'*': | ||
| 60 | case u8'?': | ||
| 61 | case u8'\"': | ||
| 62 | case u8'<': | ||
| 63 | case u8'>': | ||
| 64 | case u8'|': | ||
| 65 | case u8'\0': | ||
| 66 | u8path_santized.replace(i, 1, u8"_"); | ||
| 67 | break; | ||
| 68 | default: | ||
| 69 | break; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | // Delete duplicated spaces || Delete duplicated dots (MacOS i think) | ||
| 74 | for (size_t i = 0; i < eSizeSanitized - 1; i++) { | ||
| 75 | if ((u8path_santized[i] == u8' ' && u8path_santized[i + 1] == u8' ') || | ||
| 76 | (u8path_santized[i] == u8'.' && u8path_santized[i + 1] == u8'.')) { | ||
| 77 | u8path_santized.erase(i, 1); | ||
| 78 | i--; | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | // Delete all spaces and dots at the end (Windows almost) | ||
| 83 | while (u8path_santized.back() == u8' ' || u8path_santized.back() == u8'.') { | ||
| 84 | u8path_santized.pop_back(); | ||
| 85 | } | ||
| 86 | |||
| 87 | if (u8path_santized.empty()) { | ||
| 88 | return u8""; | ||
| 89 | } | ||
| 90 | |||
| 91 | return u8path_santized; | ||
| 92 | } | ||
| 93 | |||
| 94 | std::string UTF8FilenameSantizer(const std::string_view filename) { | ||
| 95 | return ToUTF8String(U8FilenameSantizer(ToU8String(filename))); | ||
| 96 | } | ||
| 97 | |||
| 39 | } // namespace Common::FS | 98 | } // namespace Common::FS |
diff --git a/src/common/fs/fs_util.h b/src/common/fs/fs_util.h index 2492a9f94..dbb4f5a9a 100644 --- a/src/common/fs/fs_util.h +++ b/src/common/fs/fs_util.h | |||
| @@ -82,4 +82,24 @@ concept IsChar = std::same_as<T, char>; | |||
| 82 | */ | 82 | */ |
| 83 | [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path); | 83 | [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path); |
| 84 | 84 | ||
| 85 | } // namespace Common::FS | 85 | /** |
| 86 | * Fix filename (remove invalid characters) | ||
| 87 | * | ||
| 88 | * @param u8_string dirty encoded filename string | ||
| 89 | * | ||
| 90 | * @returns utf8_string santized filename string | ||
| 91 | * | ||
| 92 | */ | ||
| 93 | [[nodiscard]] std::u8string U8FilenameSantizer(const std::u8string_view u8filename); | ||
| 94 | |||
| 95 | /** | ||
| 96 | * Fix filename (remove invalid characters) | ||
| 97 | * | ||
| 98 | * @param utf8_string dirty encoded filename string | ||
| 99 | * | ||
| 100 | * @returns utf8_string santized filename string | ||
| 101 | * | ||
| 102 | */ | ||
| 103 | [[nodiscard]] std::string UTF8FilenameSantizer(const std::string_view filename); | ||
| 104 | |||
| 105 | } // namespace Common::FS \ No newline at end of file | ||
diff --git a/src/common/fs/path_util.cpp b/src/common/fs/path_util.cpp index 0abd81a45..a461161ed 100644 --- a/src/common/fs/path_util.cpp +++ b/src/common/fs/path_util.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include <unordered_map> | 6 | #include <unordered_map> |
| 7 | 7 | ||
| 8 | #include "common/fs/fs.h" | 8 | #include "common/fs/fs.h" |
| 9 | #include "common/string_util.h" | ||
| 9 | #ifdef ANDROID | 10 | #ifdef ANDROID |
| 10 | #include "common/fs/fs_android.h" | 11 | #include "common/fs/fs_android.h" |
| 11 | #endif | 12 | #endif |
| @@ -14,7 +15,7 @@ | |||
| 14 | #include "common/logging/log.h" | 15 | #include "common/logging/log.h" |
| 15 | 16 | ||
| 16 | #ifdef _WIN32 | 17 | #ifdef _WIN32 |
| 17 | #include <shlobj.h> // Used in GetExeDirectory() | 18 | #include <shlobj.h> // Used in GetExeDirectory() and GetWindowsDesktop() |
| 18 | #else | 19 | #else |
| 19 | #include <cstdlib> // Used in Get(Home/Data)Directory() | 20 | #include <cstdlib> // Used in Get(Home/Data)Directory() |
| 20 | #include <pwd.h> // Used in GetHomeDirectory() | 21 | #include <pwd.h> // Used in GetHomeDirectory() |
| @@ -250,30 +251,39 @@ void SetYuzuPath(YuzuPath yuzu_path, const fs::path& new_path) { | |||
| 250 | #ifdef _WIN32 | 251 | #ifdef _WIN32 |
| 251 | 252 | ||
| 252 | fs::path GetExeDirectory() { | 253 | fs::path GetExeDirectory() { |
| 253 | wchar_t exe_path[MAX_PATH]; | 254 | WCHAR exe_path[MAX_PATH]; |
| 254 | 255 | ||
| 255 | if (GetModuleFileNameW(nullptr, exe_path, MAX_PATH) == 0) { | 256 | if (SUCCEEDED(GetModuleFileNameW(nullptr, exe_path, MAX_PATH))) { |
| 257 | std::wstring wideExePath(exe_path); | ||
| 258 | |||
| 259 | // UTF-16 filesystem lib to UTF-8 is broken, so we need to convert to UTF-8 with the with | ||
| 260 | // the Windows library (Filesystem converts the strings literally). | ||
| 261 | return fs::path{Common::UTF16ToUTF8(wideExePath)}.parent_path(); | ||
| 262 | } else { | ||
| 256 | LOG_ERROR(Common_Filesystem, | 263 | LOG_ERROR(Common_Filesystem, |
| 257 | "Failed to get the path to the executable of the current process"); | 264 | "[GetExeDirectory] Failed to get the path to the executable of the current " |
| 265 | "process"); | ||
| 258 | } | 266 | } |
| 259 | 267 | ||
| 260 | return fs::path{exe_path}.parent_path(); | 268 | return fs::path{}; |
| 261 | } | 269 | } |
| 262 | 270 | ||
| 263 | fs::path GetAppDataRoamingDirectory() { | 271 | fs::path GetAppDataRoamingDirectory() { |
| 264 | PWSTR appdata_roaming_path = nullptr; | 272 | PWSTR appdata_roaming_path = nullptr; |
| 265 | 273 | ||
| 266 | SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &appdata_roaming_path); | 274 | if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &appdata_roaming_path))) { |
| 267 | 275 | std::wstring wideAppdataRoamingPath(appdata_roaming_path); | |
| 268 | auto fs_appdata_roaming_path = fs::path{appdata_roaming_path}; | 276 | CoTaskMemFree(appdata_roaming_path); |
| 269 | |||
| 270 | CoTaskMemFree(appdata_roaming_path); | ||
| 271 | 277 | ||
| 272 | if (fs_appdata_roaming_path.empty()) { | 278 | // UTF-16 filesystem lib to UTF-8 is broken, so we need to convert to UTF-8 with the with |
| 273 | LOG_ERROR(Common_Filesystem, "Failed to get the path to the %APPDATA% directory"); | 279 | // the Windows library (Filesystem converts the strings literally). |
| 280 | return fs::path{Common::UTF16ToUTF8(wideAppdataRoamingPath)}; | ||
| 281 | } else { | ||
| 282 | LOG_ERROR(Common_Filesystem, | ||
| 283 | "[GetAppDataRoamingDirectory] Failed to get the path to the %APPDATA% directory"); | ||
| 274 | } | 284 | } |
| 275 | 285 | ||
| 276 | return fs_appdata_roaming_path; | 286 | return fs::path{}; |
| 277 | } | 287 | } |
| 278 | 288 | ||
| 279 | #else | 289 | #else |
| @@ -338,6 +348,57 @@ fs::path GetBundleDirectory() { | |||
| 338 | 348 | ||
| 339 | #endif | 349 | #endif |
| 340 | 350 | ||
| 351 | fs::path GetDesktopPath() { | ||
| 352 | #if defined(_WIN32) | ||
| 353 | PWSTR DesktopPath = nullptr; | ||
| 354 | |||
| 355 | if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &DesktopPath))) { | ||
| 356 | std::wstring wideDesktopPath(DesktopPath); | ||
| 357 | CoTaskMemFree(DesktopPath); | ||
| 358 | |||
| 359 | // UTF-16 filesystem lib to UTF-8 is broken, so we need to convert to UTF-8 with the with | ||
| 360 | // the Windows library (Filesystem converts the strings literally). | ||
| 361 | return fs::path{Common::UTF16ToUTF8(wideDesktopPath)}; | ||
| 362 | } else { | ||
| 363 | LOG_ERROR(Common_Filesystem, | ||
| 364 | "[GetDesktopPath] Failed to get the path to the desktop directory"); | ||
| 365 | } | ||
| 366 | #else | ||
| 367 | fs::path shortcut_path = GetHomeDirectory() / "Desktop"; | ||
| 368 | if (fs::exists(shortcut_path)) { | ||
| 369 | return shortcut_path; | ||
| 370 | } | ||
| 371 | #endif | ||
| 372 | return fs::path{}; | ||
| 373 | } | ||
| 374 | |||
| 375 | fs::path GetAppsShortcutsPath() { | ||
| 376 | #if defined(_WIN32) | ||
| 377 | PWSTR AppShortcutsPath = nullptr; | ||
| 378 | |||
| 379 | if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonPrograms, 0, NULL, &AppShortcutsPath))) { | ||
| 380 | std::wstring wideAppShortcutsPath(AppShortcutsPath); | ||
| 381 | CoTaskMemFree(AppShortcutsPath); | ||
| 382 | |||
| 383 | // UTF-16 filesystem lib to UTF-8 is broken, so we need to convert to UTF-8 with the with | ||
| 384 | // the Windows library (Filesystem converts the strings literally). | ||
| 385 | return fs::path{Common::UTF16ToUTF8(wideAppShortcutsPath)}; | ||
| 386 | } else { | ||
| 387 | LOG_ERROR(Common_Filesystem, | ||
| 388 | "[GetAppsShortcutsPath] Failed to get the path to the App Shortcuts directory"); | ||
| 389 | } | ||
| 390 | #else | ||
| 391 | fs::path shortcut_path = GetHomeDirectory() / ".local/share/applications"; | ||
| 392 | if (!fs::exists(shortcut_path)) { | ||
| 393 | shortcut_path = std::filesystem::path("/usr/share/applications"); | ||
| 394 | return shortcut_path; | ||
| 395 | } else { | ||
| 396 | return shortcut_path; | ||
| 397 | } | ||
| 398 | #endif | ||
| 399 | return fs::path{}; | ||
| 400 | } | ||
| 401 | |||
| 341 | // vvvvvvvvvv Deprecated vvvvvvvvvv // | 402 | // vvvvvvvvvv Deprecated vvvvvvvvvv // |
| 342 | 403 | ||
| 343 | std::string_view RemoveTrailingSlash(std::string_view path) { | 404 | std::string_view RemoveTrailingSlash(std::string_view path) { |
diff --git a/src/common/fs/path_util.h b/src/common/fs/path_util.h index 63801c924..b88a388d1 100644 --- a/src/common/fs/path_util.h +++ b/src/common/fs/path_util.h | |||
| @@ -244,7 +244,6 @@ void SetYuzuPath(YuzuPath yuzu_path, const Path& new_path) { | |||
| 244 | * @returns The path of the current user's %APPDATA% directory. | 244 | * @returns The path of the current user's %APPDATA% directory. |
| 245 | */ | 245 | */ |
| 246 | [[nodiscard]] std::filesystem::path GetAppDataRoamingDirectory(); | 246 | [[nodiscard]] std::filesystem::path GetAppDataRoamingDirectory(); |
| 247 | |||
| 248 | #else | 247 | #else |
| 249 | 248 | ||
| 250 | /** | 249 | /** |
| @@ -275,6 +274,20 @@ void SetYuzuPath(YuzuPath yuzu_path, const Path& new_path) { | |||
| 275 | 274 | ||
| 276 | #endif | 275 | #endif |
| 277 | 276 | ||
| 277 | /** | ||
| 278 | * Gets the path of the current user's desktop directory. | ||
| 279 | * | ||
| 280 | * @returns The path of the current user's desktop directory. | ||
| 281 | */ | ||
| 282 | [[nodiscard]] std::filesystem::path GetDesktopPath(); | ||
| 283 | |||
| 284 | /** | ||
| 285 | * Gets the path of the current user's apps directory. | ||
| 286 | * | ||
| 287 | * @returns The path of the current user's apps directory. | ||
| 288 | */ | ||
| 289 | [[nodiscard]] std::filesystem::path GetAppsShortcutsPath(); | ||
| 290 | |||
| 278 | // vvvvvvvvvv Deprecated vvvvvvvvvv // | 291 | // vvvvvvvvvv Deprecated vvvvvvvvvv // |
| 279 | 292 | ||
| 280 | // Removes the final '/' or '\' if one exists | 293 | // Removes the final '/' or '\' if one exists |