yuzu.cpp 6.3 KB

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