A tool for deriving PKG packet encryption keys for ps4 written in c++
| 1 | // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include <cassert> |
| 5 | #include <fstream> |
| 6 | #include <string> |
| 7 | #include <fmt/core.h> |
| 8 | #include <fmt/xchar.h> // for wstring support |
| 9 | |
| 10 | #include "common/path_util.h" |
| 11 | #include "config.h" |
| 12 | #include "logging/formatter.h" |
| 13 | #include "version.h" |
| 14 | |
| 15 | namespace Config { |
| 16 | |
| 17 | static bool isNeo = false; |
| 18 | static bool isFullscreen = false; |
| 19 | static std::string fullscreenMode = "borderless"; |
| 20 | static bool playBGM = false; |
| 21 | static bool isTrophyPopupDisabled = false; |
| 22 | static int BGMvolume = 50; |
| 23 | static bool enableDiscordRPC = false; |
| 24 | static u32 screenWidth = 1280; |
| 25 | static u32 screenHeight = 720; |
| 26 | static s32 gpuId = -1; // Vulkan physical device index. Set to negative for auto select |
| 27 | static std::string logFilter; |
| 28 | static std::string logType = "async"; |
| 29 | static std::string userName = "shadPS4"; |
| 30 | static std::string updateChannel; |
| 31 | static std::string chooseHomeTab; |
| 32 | static u16 deadZoneLeft = 2.0; |
| 33 | static u16 deadZoneRight = 2.0; |
| 34 | static std::string backButtonBehavior = "left"; |
| 35 | static bool useSpecialPad = false; |
| 36 | static int specialPadClass = 1; |
| 37 | static bool isMotionControlsEnabled = true; |
| 38 | static bool isDebugDump = false; |
| 39 | static bool isShaderDebug = false; |
| 40 | static bool isShowSplash = false; |
| 41 | static bool isAutoUpdate = false; |
| 42 | static bool isNullGpu = false; |
| 43 | static bool shouldCopyGPUBuffers = false; |
| 44 | static bool shouldDumpShaders = false; |
| 45 | static bool shouldPatchShaders = true; |
| 46 | static u32 vblankDivider = 1; |
| 47 | static bool vkValidation = false; |
| 48 | static bool vkValidationSync = false; |
| 49 | static bool vkValidationGpu = false; |
| 50 | static bool vkCrashDiagnostic = false; |
| 51 | static bool vkHostMarkers = false; |
| 52 | static bool vkGuestMarkers = false; |
| 53 | static bool rdocEnable = false; |
| 54 | static s16 cursorState = HideCursorState::Idle; |
| 55 | static int cursorHideTimeout = 5; // 5 seconds (default) |
| 56 | static bool separateupdatefolder = false; |
| 57 | static bool compatibilityData = false; |
| 58 | static bool checkCompatibilityOnStartup = false; |
| 59 | static std::string trophyKey; |
| 60 | |
| 61 | // Gui |
| 62 | static bool load_game_size = true; |
| 63 | std::vector<std::filesystem::path> settings_install_dirs = {}; |
| 64 | std::filesystem::path settings_addon_install_dir = {}; |
| 65 | std::filesystem::path save_data_path = {}; |
| 66 | u32 main_window_geometry_x = 400; |
| 67 | u32 main_window_geometry_y = 400; |
| 68 | u32 main_window_geometry_w = 1280; |
| 69 | u32 main_window_geometry_h = 720; |
| 70 | u32 mw_themes = 0; |
| 71 | u32 m_icon_size = 36; |
| 72 | u32 m_icon_size_grid = 69; |
| 73 | u32 m_slider_pos = 0; |
| 74 | u32 m_slider_pos_grid = 0; |
| 75 | u32 m_table_mode = 0; |
| 76 | u32 m_window_size_W = 1280; |
| 77 | u32 m_window_size_H = 720; |
| 78 | std::vector<std::string> m_pkg_viewer; |
| 79 | std::vector<std::string> m_elf_viewer; |
| 80 | std::vector<std::string> m_recent_files; |
| 81 | std::string emulator_language = "en"; |
| 82 | |
| 83 | // Language |
| 84 | u32 m_language = 1; // english |
| 85 | |
| 86 | std::string getTrophyKey() { |
| 87 | return ""; |
| 88 | } |
| 89 | |
| 90 | void setTrophyKey(std::string key) { |
| 91 | trophyKey = key; |
| 92 | } |
| 93 | |
| 94 | bool GetLoadGameSizeEnabled() { |
| 95 | return load_game_size; |
| 96 | } |
| 97 | |
| 98 | std::filesystem::path GetSaveDataPath() { |
| 99 | if (save_data_path.empty()) { |
| 100 | return Common::FS::GetUserPath(Common::FS::PathType::SaveDataDir); |
| 101 | } |
| 102 | return save_data_path; |
| 103 | } |
| 104 | |
| 105 | void setLoadGameSizeEnabled(bool enable) { |
| 106 | load_game_size = enable; |
| 107 | } |
| 108 | |
| 109 | bool isNeoModeConsole() { |
| 110 | return isNeo; |
| 111 | } |
| 112 | |
| 113 | bool getIsFullscreen() { |
| 114 | return isFullscreen; |
| 115 | } |
| 116 | |
| 117 | std::string getFullscreenMode() { |
| 118 | return fullscreenMode; |
| 119 | } |
| 120 | |
| 121 | bool getisTrophyPopupDisabled() { |
| 122 | return isTrophyPopupDisabled; |
| 123 | } |
| 124 | |
| 125 | bool getPlayBGM() { |
| 126 | return playBGM; |
| 127 | } |
| 128 | |
| 129 | int getBGMvolume() { |
| 130 | return BGMvolume; |
| 131 | } |
| 132 | |
| 133 | bool getEnableDiscordRPC() { |
| 134 | return enableDiscordRPC; |
| 135 | } |
| 136 | |
| 137 | u16 leftDeadZone() { |
| 138 | return deadZoneLeft; |
| 139 | } |
| 140 | |
| 141 | u16 rightDeadZone() { |
| 142 | return deadZoneRight; |
| 143 | } |
| 144 | |
| 145 | s16 getCursorState() { |
| 146 | return cursorState; |
| 147 | } |
| 148 | |
| 149 | int getCursorHideTimeout() { |
| 150 | return cursorHideTimeout; |
| 151 | } |
| 152 | |
| 153 | u32 getScreenWidth() { |
| 154 | return screenWidth; |
| 155 | } |
| 156 | |
| 157 | u32 getScreenHeight() { |
| 158 | return screenHeight; |
| 159 | } |
| 160 | |
| 161 | s32 getGpuId() { |
| 162 | return gpuId; |
| 163 | } |
| 164 | |
| 165 | std::string getLogFilter() { |
| 166 | return logFilter; |
| 167 | } |
| 168 | |
| 169 | std::string getLogType() { |
| 170 | return logType; |
| 171 | } |
| 172 | |
| 173 | std::string getUserName() { |
| 174 | return userName; |
| 175 | } |
| 176 | |
| 177 | std::string getUpdateChannel() { |
| 178 | return updateChannel; |
| 179 | } |
| 180 | |
| 181 | std::string getChooseHomeTab() { |
| 182 | return chooseHomeTab; |
| 183 | } |
| 184 | |
| 185 | std::string getBackButtonBehavior() { |
| 186 | return backButtonBehavior; |
| 187 | } |
| 188 | |
| 189 | bool getUseSpecialPad() { |
| 190 | return useSpecialPad; |
| 191 | } |
| 192 | |
| 193 | int getSpecialPadClass() { |
| 194 | return specialPadClass; |
| 195 | } |
| 196 | |
| 197 | bool getIsMotionControlsEnabled() { |
| 198 | return isMotionControlsEnabled; |
| 199 | } |
| 200 | |
| 201 | bool debugDump() { |
| 202 | return isDebugDump; |
| 203 | } |
| 204 | |
| 205 | bool collectShadersForDebug() { |
| 206 | return isShaderDebug; |
| 207 | } |
| 208 | |
| 209 | bool showSplash() { |
| 210 | return isShowSplash; |
| 211 | } |
| 212 | |
| 213 | bool autoUpdate() { |
| 214 | return isAutoUpdate; |
| 215 | } |
| 216 | |
| 217 | bool nullGpu() { |
| 218 | return isNullGpu; |
| 219 | } |
| 220 | |
| 221 | bool copyGPUCmdBuffers() { |
| 222 | return shouldCopyGPUBuffers; |
| 223 | } |
| 224 | |
| 225 | bool dumpShaders() { |
| 226 | return shouldDumpShaders; |
| 227 | } |
| 228 | |
| 229 | bool patchShaders() { |
| 230 | return shouldPatchShaders; |
| 231 | } |
| 232 | |
| 233 | bool isRdocEnabled() { |
| 234 | return rdocEnable; |
| 235 | } |
| 236 | |
| 237 | u32 vblankDiv() { |
| 238 | return vblankDivider; |
| 239 | } |
| 240 | |
| 241 | bool vkValidationEnabled() { |
| 242 | return vkValidation; |
| 243 | } |
| 244 | |
| 245 | bool vkValidationSyncEnabled() { |
| 246 | return vkValidationSync; |
| 247 | } |
| 248 | |
| 249 | bool vkValidationGpuEnabled() { |
| 250 | return vkValidationGpu; |
| 251 | } |
| 252 | |
| 253 | bool getVkCrashDiagnosticEnabled() { |
| 254 | return vkCrashDiagnostic; |
| 255 | } |
| 256 | |
| 257 | bool getVkHostMarkersEnabled() { |
| 258 | return vkHostMarkers; |
| 259 | } |
| 260 | |
| 261 | bool getVkGuestMarkersEnabled() { |
| 262 | return vkGuestMarkers; |
| 263 | } |
| 264 | |
| 265 | void setVkCrashDiagnosticEnabled(bool enable) { |
| 266 | vkCrashDiagnostic = enable; |
| 267 | } |
| 268 | |
| 269 | void setVkHostMarkersEnabled(bool enable) { |
| 270 | vkHostMarkers = enable; |
| 271 | } |
| 272 | |
| 273 | void setVkGuestMarkersEnabled(bool enable) { |
| 274 | vkGuestMarkers = enable; |
| 275 | } |
| 276 | |
| 277 | bool getSeparateUpdateEnabled() { |
| 278 | return separateupdatefolder; |
| 279 | } |
| 280 | |
| 281 | bool getCompatibilityEnabled() { |
| 282 | return compatibilityData; |
| 283 | } |
| 284 | |
| 285 | bool getCheckCompatibilityOnStartup() { |
| 286 | return checkCompatibilityOnStartup; |
| 287 | } |
| 288 | |
| 289 | void setGpuId(s32 selectedGpuId) { |
| 290 | gpuId = selectedGpuId; |
| 291 | } |
| 292 | |
| 293 | void setScreenWidth(u32 width) { |
| 294 | screenWidth = width; |
| 295 | } |
| 296 | |
| 297 | void setScreenHeight(u32 height) { |
| 298 | screenHeight = height; |
| 299 | } |
| 300 | |
| 301 | void setDebugDump(bool enable) { |
| 302 | isDebugDump = enable; |
| 303 | } |
| 304 | |
| 305 | void setCollectShaderForDebug(bool enable) { |
| 306 | isShaderDebug = enable; |
| 307 | } |
| 308 | |
| 309 | void setShowSplash(bool enable) { |
| 310 | isShowSplash = enable; |
| 311 | } |
| 312 | |
| 313 | void setAutoUpdate(bool enable) { |
| 314 | isAutoUpdate = enable; |
| 315 | } |
| 316 | |
| 317 | void setNullGpu(bool enable) { |
| 318 | isNullGpu = enable; |
| 319 | } |
| 320 | |
| 321 | void setCopyGPUCmdBuffers(bool enable) { |
| 322 | shouldCopyGPUBuffers = enable; |
| 323 | } |
| 324 | |
| 325 | void setDumpShaders(bool enable) { |
| 326 | shouldDumpShaders = enable; |
| 327 | } |
| 328 | |
| 329 | void setVkValidation(bool enable) { |
| 330 | vkValidation = enable; |
| 331 | } |
| 332 | |
| 333 | void setVkSyncValidation(bool enable) { |
| 334 | vkValidationSync = enable; |
| 335 | } |
| 336 | |
| 337 | void setRdocEnabled(bool enable) { |
| 338 | rdocEnable = enable; |
| 339 | } |
| 340 | |
| 341 | void setVblankDiv(u32 value) { |
| 342 | vblankDivider = value; |
| 343 | } |
| 344 | |
| 345 | void setIsFullscreen(bool enable) { |
| 346 | isFullscreen = enable; |
| 347 | } |
| 348 | |
| 349 | void setFullscreenMode(std::string mode) { |
| 350 | fullscreenMode = mode; |
| 351 | } |
| 352 | |
| 353 | void setisTrophyPopupDisabled(bool disable) { |
| 354 | isTrophyPopupDisabled = disable; |
| 355 | } |
| 356 | |
| 357 | void setPlayBGM(bool enable) { |
| 358 | playBGM = enable; |
| 359 | } |
| 360 | |
| 361 | void setBGMvolume(int volume) { |
| 362 | BGMvolume = volume; |
| 363 | } |
| 364 | |
| 365 | void setEnableDiscordRPC(bool enable) { |
| 366 | enableDiscordRPC = enable; |
| 367 | } |
| 368 | |
| 369 | void setCursorState(s16 newCursorState) { |
| 370 | cursorState = newCursorState; |
| 371 | } |
| 372 | |
| 373 | void setCursorHideTimeout(int newcursorHideTimeout) { |
| 374 | cursorHideTimeout = newcursorHideTimeout; |
| 375 | } |
| 376 | |
| 377 | void setLanguage(u32 language) { |
| 378 | m_language = language; |
| 379 | } |
| 380 | |
| 381 | void setNeoMode(bool enable) { |
| 382 | isNeo = enable; |
| 383 | } |
| 384 | |
| 385 | void setLogType(const std::string& type) { |
| 386 | logType = type; |
| 387 | } |
| 388 | |
| 389 | void setLogFilter(const std::string& type) { |
| 390 | logFilter = type; |
| 391 | } |
| 392 | |
| 393 | void setUserName(const std::string& type) { |
| 394 | userName = type; |
| 395 | } |
| 396 | |
| 397 | void setUpdateChannel(const std::string& type) { |
| 398 | updateChannel = type; |
| 399 | } |
| 400 | void setChooseHomeTab(const std::string& type) { |
| 401 | chooseHomeTab = type; |
| 402 | } |
| 403 | |
| 404 | void setBackButtonBehavior(const std::string& type) { |
| 405 | backButtonBehavior = type; |
| 406 | } |
| 407 | |
| 408 | void setUseSpecialPad(bool use) { |
| 409 | useSpecialPad = use; |
| 410 | } |
| 411 | |
| 412 | void setSpecialPadClass(int type) { |
| 413 | specialPadClass = type; |
| 414 | } |
| 415 | |
| 416 | void setIsMotionControlsEnabled(bool use) { |
| 417 | isMotionControlsEnabled = use; |
| 418 | } |
| 419 | |
| 420 | void setSeparateUpdateEnabled(bool use) { |
| 421 | separateupdatefolder = use; |
| 422 | } |
| 423 | |
| 424 | void setCompatibilityEnabled(bool use) { |
| 425 | compatibilityData = use; |
| 426 | } |
| 427 | |
| 428 | void setCheckCompatibilityOnStartup(bool use) { |
| 429 | checkCompatibilityOnStartup = use; |
| 430 | } |
| 431 | |
| 432 | void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h) { |
| 433 | main_window_geometry_x = x; |
| 434 | main_window_geometry_y = y; |
| 435 | main_window_geometry_w = w; |
| 436 | main_window_geometry_h = h; |
| 437 | } |
| 438 | |
| 439 | bool addGameInstallDir(const std::filesystem::path& dir) { |
| 440 | if (std::find(settings_install_dirs.begin(), settings_install_dirs.end(), dir) == |
| 441 | settings_install_dirs.end()) { |
| 442 | settings_install_dirs.push_back(dir); |
| 443 | return true; |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | void removeGameInstallDir(const std::filesystem::path& dir) { |
| 449 | auto iterator = std::find(settings_install_dirs.begin(), settings_install_dirs.end(), dir); |
| 450 | if (iterator != settings_install_dirs.end()) { |
| 451 | settings_install_dirs.erase(iterator); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void setAddonInstallDir(const std::filesystem::path& dir) { |
| 456 | settings_addon_install_dir = dir; |
| 457 | } |
| 458 | |
| 459 | void setMainWindowTheme(u32 theme) { |
| 460 | mw_themes = theme; |
| 461 | } |
| 462 | |
| 463 | void setIconSize(u32 size) { |
| 464 | m_icon_size = size; |
| 465 | } |
| 466 | |
| 467 | void setIconSizeGrid(u32 size) { |
| 468 | m_icon_size_grid = size; |
| 469 | } |
| 470 | |
| 471 | void setSliderPosition(u32 pos) { |
| 472 | m_slider_pos = pos; |
| 473 | } |
| 474 | |
| 475 | void setSliderPositionGrid(u32 pos) { |
| 476 | m_slider_pos_grid = pos; |
| 477 | } |
| 478 | |
| 479 | void setTableMode(u32 mode) { |
| 480 | m_table_mode = mode; |
| 481 | } |
| 482 | |
| 483 | void setMainWindowWidth(u32 width) { |
| 484 | m_window_size_W = width; |
| 485 | } |
| 486 | |
| 487 | void setMainWindowHeight(u32 height) { |
| 488 | m_window_size_H = height; |
| 489 | } |
| 490 | |
| 491 | void setPkgViewer(const std::vector<std::string>& pkgList) { |
| 492 | m_pkg_viewer.resize(pkgList.size()); |
| 493 | m_pkg_viewer = pkgList; |
| 494 | } |
| 495 | |
| 496 | void setElfViewer(const std::vector<std::string>& elfList) { |
| 497 | m_elf_viewer.resize(elfList.size()); |
| 498 | m_elf_viewer = elfList; |
| 499 | } |
| 500 | |
| 501 | void setRecentFiles(const std::vector<std::string>& recentFiles) { |
| 502 | m_recent_files.resize(recentFiles.size()); |
| 503 | m_recent_files = recentFiles; |
| 504 | } |
| 505 | |
| 506 | void setEmulatorLanguage(std::string language) { |
| 507 | emulator_language = language; |
| 508 | } |
| 509 | |
| 510 | void setGameInstallDirs(const std::vector<std::filesystem::path>& settings_install_dirs_config) { |
| 511 | settings_install_dirs = settings_install_dirs_config; |
| 512 | } |
| 513 | |
| 514 | void setSaveDataPath(const std::filesystem::path& path) { |
| 515 | save_data_path = path; |
| 516 | } |
| 517 | |
| 518 | u32 getMainWindowGeometryX() { |
| 519 | return main_window_geometry_x; |
| 520 | } |
| 521 | |
| 522 | u32 getMainWindowGeometryY() { |
| 523 | return main_window_geometry_y; |
| 524 | } |
| 525 | |
| 526 | u32 getMainWindowGeometryW() { |
| 527 | return main_window_geometry_w; |
| 528 | } |
| 529 | |
| 530 | u32 getMainWindowGeometryH() { |
| 531 | return main_window_geometry_h; |
| 532 | } |
| 533 | |
| 534 | const std::vector<std::filesystem::path>& getGameInstallDirs() { |
| 535 | return settings_install_dirs; |
| 536 | } |
| 537 | |
| 538 | std::filesystem::path getAddonInstallDir() { |
| 539 | if (settings_addon_install_dir.empty()) { |
| 540 | // Default for users without a config file or a config file from before this option existed |
| 541 | return Common::FS::GetUserPath(Common::FS::PathType::UserDir) / "addcont"; |
| 542 | } |
| 543 | return settings_addon_install_dir; |
| 544 | } |
| 545 | |
| 546 | u32 getMainWindowTheme() { |
| 547 | return mw_themes; |
| 548 | } |
| 549 | |
| 550 | u32 getIconSize() { |
| 551 | return m_icon_size; |
| 552 | } |
| 553 | |
| 554 | u32 getIconSizeGrid() { |
| 555 | return m_icon_size_grid; |
| 556 | } |
| 557 | |
| 558 | u32 getSliderPosition() { |
| 559 | return m_slider_pos; |
| 560 | } |
| 561 | |
| 562 | u32 getSliderPositionGrid() { |
| 563 | return m_slider_pos_grid; |
| 564 | } |
| 565 | |
| 566 | u32 getTableMode() { |
| 567 | return m_table_mode; |
| 568 | } |
| 569 | |
| 570 | u32 getMainWindowWidth() { |
| 571 | return m_window_size_W; |
| 572 | } |
| 573 | |
| 574 | u32 getMainWindowHeight() { |
| 575 | return m_window_size_H; |
| 576 | } |
| 577 | |
| 578 | std::vector<std::string> getPkgViewer() { |
| 579 | return m_pkg_viewer; |
| 580 | } |
| 581 | |
| 582 | std::vector<std::string> getElfViewer() { |
| 583 | return m_elf_viewer; |
| 584 | } |
| 585 | |
| 586 | std::vector<std::string> getRecentFiles() { |
| 587 | return m_recent_files; |
| 588 | } |
| 589 | |
| 590 | std::string getEmulatorLanguage() { |
| 591 | return emulator_language; |
| 592 | } |
| 593 | |
| 594 | u32 GetLanguage() { |
| 595 | return m_language; |
| 596 | } |
| 597 | |
| 598 | void setDefaultValues() { |
| 599 | isNeo = false; |
| 600 | isFullscreen = false; |
| 601 | isTrophyPopupDisabled = false; |
| 602 | playBGM = false; |
| 603 | BGMvolume = 50; |
| 604 | enableDiscordRPC = true; |
| 605 | screenWidth = 1280; |
| 606 | screenHeight = 720; |
| 607 | logFilter = ""; |
| 608 | logType = "async"; |
| 609 | userName = "shadPS4"; |
| 610 | if (Common::isRelease) { |
| 611 | updateChannel = "Release"; |
| 612 | } else { |
| 613 | updateChannel = "Nightly"; |
| 614 | } |
| 615 | chooseHomeTab = "General"; |
| 616 | cursorState = HideCursorState::Idle; |
| 617 | cursorHideTimeout = 5; |
| 618 | backButtonBehavior = "left"; |
| 619 | useSpecialPad = false; |
| 620 | specialPadClass = 1; |
| 621 | isDebugDump = false; |
| 622 | isShaderDebug = false; |
| 623 | isShowSplash = false; |
| 624 | isAutoUpdate = false; |
| 625 | isNullGpu = false; |
| 626 | shouldDumpShaders = false; |
| 627 | vblankDivider = 1; |
| 628 | vkValidation = false; |
| 629 | vkValidationSync = false; |
| 630 | vkValidationGpu = false; |
| 631 | vkCrashDiagnostic = false; |
| 632 | vkHostMarkers = false; |
| 633 | vkGuestMarkers = false; |
| 634 | rdocEnable = false; |
| 635 | emulator_language = "en"; |
| 636 | m_language = 1; |
| 637 | gpuId = -1; |
| 638 | separateupdatefolder = false; |
| 639 | compatibilityData = false; |
| 640 | checkCompatibilityOnStartup = false; |
| 641 | } |
| 642 | |
| 643 | } // namespace Config |