yuzu.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. int option_index = 0;
  64. bool use_gdbstub = Settings::values.use_gdbstub;
  65. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  66. char* endarg;
  67. #ifdef _WIN32
  68. int argc_w;
  69. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  70. if (argv_w == nullptr) {
  71. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  72. return -1;
  73. }
  74. #endif
  75. std::string filepath;
  76. bool fullscreen = false;
  77. static struct option long_options[] = {
  78. {"gdbport", required_argument, 0, 'g'},
  79. {"fullscreen", no_argument, 0, 'f'},
  80. {"help", no_argument, 0, 'h'},
  81. {"version", no_argument, 0, 'v'},
  82. {0, 0, 0, 0},
  83. };
  84. while (optind < argc) {
  85. char arg = getopt_long(argc, argv, "g:fhv", long_options, &option_index);
  86. if (arg != -1) {
  87. switch (arg) {
  88. case 'g':
  89. errno = 0;
  90. gdb_port = strtoul(optarg, &endarg, 0);
  91. use_gdbstub = true;
  92. if (endarg == optarg)
  93. errno = EINVAL;
  94. if (errno != 0) {
  95. perror("--gdbport");
  96. exit(1);
  97. }
  98. break;
  99. case 'f':
  100. fullscreen = true;
  101. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  102. break;
  103. case 'h':
  104. PrintHelp(argv[0]);
  105. return 0;
  106. case 'v':
  107. PrintVersion();
  108. return 0;
  109. }
  110. } else {
  111. #ifdef _WIN32
  112. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  113. #else
  114. filepath = argv[optind];
  115. #endif
  116. optind++;
  117. }
  118. }
  119. #ifdef _WIN32
  120. LocalFree(argv_w);
  121. #endif
  122. InitializeLogging();
  123. MicroProfileOnThreadCreate("EmuThread");
  124. SCOPE_EXIT({ MicroProfileShutdown(); });
  125. if (filepath.empty()) {
  126. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  127. return -1;
  128. }
  129. // Apply the command line arguments
  130. Settings::values.gdbstub_port = gdb_port;
  131. Settings::values.use_gdbstub = use_gdbstub;
  132. Settings::Apply();
  133. std::unique_ptr<EmuWindow_SDL2> emu_window{std::make_unique<EmuWindow_SDL2>(fullscreen)};
  134. if (!Settings::values.use_multi_core) {
  135. // Single core mode must acquire OpenGL context for entire emulation session
  136. emu_window->MakeCurrent();
  137. }
  138. Core::System& system{Core::System::GetInstance()};
  139. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  140. SCOPE_EXIT({ system.Shutdown(); });
  141. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  142. switch (load_result) {
  143. case Core::System::ResultStatus::ErrorGetLoader:
  144. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
  145. return -1;
  146. case Core::System::ResultStatus::ErrorLoader:
  147. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  148. return -1;
  149. case Core::System::ResultStatus::ErrorNotInitialized:
  150. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  151. return -1;
  152. case Core::System::ResultStatus::ErrorSystemMode:
  153. LOG_CRITICAL(Frontend, "Failed to determine system mode!");
  154. return -1;
  155. case Core::System::ResultStatus::ErrorVideoCore:
  156. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  157. return -1;
  158. case Core::System::ResultStatus::Success:
  159. break; // Expected case
  160. default:
  161. if (static_cast<u32>(load_result) >
  162. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  163. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  164. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  165. LOG_CRITICAL(Frontend,
  166. "While attempting to load the ROM requested, an error occured. Please "
  167. "refer to the yuzu wiki for more information or the yuzu discord for "
  168. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  169. loader_id, error_id, Loader::GetMessageForResultStatus(error_id));
  170. }
  171. }
  172. Core::Telemetry().AddField(Telemetry::FieldType::App, "Frontend", "SDL");
  173. while (emu_window->IsOpen()) {
  174. system.RunLoop();
  175. }
  176. return 0;
  177. }