yuzu.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <iostream>
  5. #include <memory>
  6. #include <string>
  7. #include <thread>
  8. #include "common/common_paths.h"
  9. #include "common/logging/backend.h"
  10. #include "common/logging/filter.h"
  11. #include "common/logging/log.h"
  12. #include "common/microprofile.h"
  13. #include "common/scm_rev.h"
  14. #include "common/scope_exit.h"
  15. #include "common/string_util.h"
  16. #include "core/core.h"
  17. #include "core/gdbstub/gdbstub.h"
  18. #include "core/loader/loader.h"
  19. #include "core/settings.h"
  20. #include "yuzu_cmd/config.h"
  21. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  22. #include <getopt.h>
  23. #include "core/crypto/key_manager.h"
  24. #ifndef _MSC_VER
  25. #include <unistd.h>
  26. #endif
  27. #ifdef _WIN32
  28. // windows.h needs to be included before shellapi.h
  29. #include <windows.h>
  30. #include <shellapi.h>
  31. #endif
  32. #ifdef _WIN32
  33. extern "C" {
  34. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  35. // graphics
  36. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  37. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  38. }
  39. #endif
  40. static void PrintHelp(const char* argv0) {
  41. std::cout << "Usage: " << argv0
  42. << " [options] <filename>\n"
  43. "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
  44. "-f, --fullscreen Start in fullscreen mode\n"
  45. "-h, --help Display this help and exit\n"
  46. "-v, --version Output version information and exit\n";
  47. }
  48. static void PrintVersion() {
  49. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  50. }
  51. static void InitializeLogging() {
  52. Log::Filter log_filter(Log::Level::Debug);
  53. log_filter.ParseFilterString(Settings::values.log_filter);
  54. Log::SetGlobalFilter(log_filter);
  55. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  56. const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  57. FileUtil::CreateFullPath(log_dir);
  58. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  59. }
  60. /// Application entry point
  61. int main(int argc, char** argv) {
  62. Config config;
  63. // Initialize keys
  64. std::string keys_dir = FileUtil::GetHactoolConfigurationPath();
  65. if (Settings::values.use_dev_keys) {
  66. Crypto::keys.SetValidationMode(true);
  67. if (FileUtil::Exists(keys_dir + DIR_SEP + "dev.keys"))
  68. Crypto::keys.LoadFromFile(keys_dir + DIR_SEP + "dev.keys", false);
  69. } else {
  70. if (FileUtil::Exists(keys_dir + DIR_SEP + "prod.keys"))
  71. Crypto::keys.LoadFromFile(keys_dir + DIR_SEP + "prod.keys", false);
  72. }
  73. if (FileUtil::Exists(keys_dir + DIR_SEP + "title.keys"))
  74. Crypto::keys.LoadFromFile(keys_dir + DIR_SEP + "title.keys", true);
  75. int option_index = 0;
  76. bool use_gdbstub = Settings::values.use_gdbstub;
  77. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  78. char* endarg;
  79. #ifdef _WIN32
  80. int argc_w;
  81. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  82. if (argv_w == nullptr) {
  83. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  84. return -1;
  85. }
  86. #endif
  87. std::string filepath;
  88. bool fullscreen = false;
  89. static struct option long_options[] = {
  90. {"gdbport", required_argument, 0, 'g'},
  91. {"fullscreen", no_argument, 0, 'f'},
  92. {"help", no_argument, 0, 'h'},
  93. {"version", no_argument, 0, 'v'},
  94. {0, 0, 0, 0},
  95. };
  96. while (optind < argc) {
  97. char arg = getopt_long(argc, argv, "g:fhv", long_options, &option_index);
  98. if (arg != -1) {
  99. switch (arg) {
  100. case 'g':
  101. errno = 0;
  102. gdb_port = strtoul(optarg, &endarg, 0);
  103. use_gdbstub = true;
  104. if (endarg == optarg)
  105. errno = EINVAL;
  106. if (errno != 0) {
  107. perror("--gdbport");
  108. exit(1);
  109. }
  110. break;
  111. case 'f':
  112. fullscreen = true;
  113. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  114. break;
  115. case 'h':
  116. PrintHelp(argv[0]);
  117. return 0;
  118. case 'v':
  119. PrintVersion();
  120. return 0;
  121. }
  122. } else {
  123. #ifdef _WIN32
  124. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  125. #else
  126. filepath = argv[optind];
  127. #endif
  128. optind++;
  129. }
  130. }
  131. #ifdef _WIN32
  132. LocalFree(argv_w);
  133. #endif
  134. InitializeLogging();
  135. MicroProfileOnThreadCreate("EmuThread");
  136. SCOPE_EXIT({ MicroProfileShutdown(); });
  137. if (filepath.empty()) {
  138. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  139. return -1;
  140. }
  141. // Apply the command line arguments
  142. Settings::values.gdbstub_port = gdb_port;
  143. Settings::values.use_gdbstub = use_gdbstub;
  144. Settings::Apply();
  145. std::unique_ptr<EmuWindow_SDL2> emu_window{std::make_unique<EmuWindow_SDL2>(fullscreen)};
  146. if (!Settings::values.use_multi_core) {
  147. // Single core mode must acquire OpenGL context for entire emulation session
  148. emu_window->MakeCurrent();
  149. }
  150. Core::System& system{Core::System::GetInstance()};
  151. SCOPE_EXIT({ system.Shutdown(); });
  152. const Core::System::ResultStatus load_result{system.Load(emu_window.get(), filepath)};
  153. switch (load_result) {
  154. case Core::System::ResultStatus::ErrorGetLoader:
  155. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
  156. return -1;
  157. case Core::System::ResultStatus::ErrorLoader:
  158. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  159. return -1;
  160. case Core::System::ResultStatus::ErrorLoader_ErrorEncrypted:
  161. LOG_CRITICAL(Frontend, "The game that you are trying to load must be decrypted before "
  162. "being used with yuzu. \n\n For more information on dumping and "
  163. "decrypting games, please refer to: "
  164. "https://yuzu-emu.org/wiki/dumping-game-cartridges/");
  165. return -1;
  166. case Core::System::ResultStatus::ErrorLoader_ErrorInvalidFormat:
  167. LOG_CRITICAL(Frontend, "Error while loading ROM: The ROM format is not supported.");
  168. return -1;
  169. case Core::System::ResultStatus::ErrorNotInitialized:
  170. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  171. return -1;
  172. case Core::System::ResultStatus::ErrorSystemMode:
  173. LOG_CRITICAL(Frontend, "Failed to determine system mode!");
  174. return -1;
  175. case Core::System::ResultStatus::ErrorVideoCore:
  176. LOG_CRITICAL(Frontend, "VideoCore not initialized");
  177. return -1;
  178. case Core::System::ResultStatus::Success:
  179. break; // Expected case
  180. }
  181. Core::Telemetry().AddField(Telemetry::FieldType::App, "Frontend", "SDL");
  182. while (emu_window->IsOpen()) {
  183. system.RunLoop();
  184. }
  185. return 0;
  186. }