Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
common/path_util.h
ShadPKG / common / path_util.h
1// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3 
4#pragma once
5 
6#include <filesystem>
7#include <vector>
8 
9#ifdef ENABLE_QT_GUI
10class QString; // to avoid including <QString> in this header
11#endif
12 
13namespace Common::FS {
14 
15enum class PathType {
16 UserDir, // Where shadPS4 stores its data.
17 LogDir, // Where log files are stored.
18 ScreenshotsDir, // Where screenshots are stored.
19 ShaderDir, // Where shaders are stored.
20 SaveDataDir, // Where guest save data is stored.
21 TempDataDir, // Where game temp data is stored.
22 GameDataDir, // Where game data is stored.
23 SysModuleDir, // Where system modules are stored.
24 DownloadDir, // Where downloads/temp files are stored.
25 CapturesDir, // Where rdoc captures are stored.
26 CheatsDir, // Where cheats are stored.
27 PatchesDir, // Where patches are stored.
28 MetaDataDir, // Where game metadata (e.g. trophies and menu backgrounds) is stored.
29};
30 
31constexpr auto PORTABLE_DIR = "user";
32 
33// Sub-directories contained within a user data directory
34constexpr auto LOG_DIR = "log";
35constexpr auto SCREENSHOTS_DIR = "screenshots";
36constexpr auto SHADER_DIR = "shader";
37constexpr auto SAVEDATA_DIR = "savedata";
38constexpr auto GAMEDATA_DIR = "data";
39constexpr auto TEMPDATA_DIR = "temp";
40constexpr auto SYSMODULES_DIR = "sys_modules";
41constexpr auto DOWNLOAD_DIR = "download";
42constexpr auto CAPTURES_DIR = "captures";
43constexpr auto CHEATS_DIR = "cheats";
44constexpr auto PATCHES_DIR = "patches";
45constexpr auto METADATA_DIR = "game_data";
46 
47// Filenames
48constexpr auto LOG_FILE = "shad_log.txt";
49 
50/**
51 * Validates a given path.
52 *
53 * A given path is valid if it meets these conditions:
54 * - The path is not empty
55 * - The path is not too long
56 *
57 * @param path Filesystem path
58 *
59 * @returns True if the path is valid, false otherwise.
60 */
61[[nodiscard]] bool ValidatePath(const std::filesystem::path& path);
62 
63/**
64 * Converts a filesystem path to a UTF-8 encoded std::string.
65 *
66 * @param path Filesystem path
67 *
68 * @returns UTF-8 encoded std::string.
69 */
70[[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path);
71 
72/**
73 * Gets the filesystem path associated with the PathType enum.
74 *
75 * @param user_path PathType enum
76 *
77 * @returns The filesystem path associated with the PathType enum.
78 */
79[[nodiscard]] const std::filesystem::path& GetUserPath(PathType user_path);
80 
81/**
82 * Gets the filesystem path associated with the PathType enum as a UTF-8 encoded std::string.
83 *
84 * @param user_path PathType enum
85 *
86 * @returns The filesystem path associated with the PathType enum as a UTF-8 encoded std::string.
87 */
88[[nodiscard]] std::string GetUserPathString(PathType user_path);
89 
90/**
91 * Sets a new filesystem path associated with the PathType enum.
92 * If the filesystem object at new_path is not a directory, this function will not do anything.
93 *
94 * @param user_path PathType enum
95 * @param new_path New filesystem path
96 */
97void SetUserPath(PathType user_path, const std::filesystem::path& new_path);
98 
99#ifdef ENABLE_QT_GUI
100/**
101 * Converts an std::filesystem::path to a QString.
102 * The native underlying string of a path is wstring on Windows and string on POSIX.
103 *
104 * @param result The resulting QString
105 * @param path The path to convert
106 */
107void PathToQString(QString& result, const std::filesystem::path& path);
108 
109/**
110 * Converts a QString to an std::filesystem::path.
111 * The native underlying string of a path is wstring on Windows and string on POSIX.
112 *
113 * @param path The path to convert
114 */
115[[nodiscard]] std::filesystem::path PathFromQString(const QString& path);
116#endif
117 
118} // namespace Common::FS
119