yuzu.cpp 6.6 KB

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