yuzu.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/detached_tasks.h"
  11. #include "common/file_util.h"
  12. #include "common/logging/backend.h"
  13. #include "common/logging/filter.h"
  14. #include "common/logging/log.h"
  15. #include "common/microprofile.h"
  16. #include "common/scm_rev.h"
  17. #include "common/scope_exit.h"
  18. #include "common/string_util.h"
  19. #include "common/telemetry.h"
  20. #include "core/core.h"
  21. #include "core/crypto/key_manager.h"
  22. #include "core/file_sys/vfs_real.h"
  23. #include "core/gdbstub/gdbstub.h"
  24. #include "core/hle/service/filesystem/filesystem.h"
  25. #include "core/loader/loader.h"
  26. #include "core/settings.h"
  27. #include "core/telemetry_session.h"
  28. #include "yuzu_cmd/config.h"
  29. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  30. #include <getopt.h>
  31. #ifndef _MSC_VER
  32. #include <unistd.h>
  33. #endif
  34. #ifdef _WIN32
  35. // windows.h needs to be included before shellapi.h
  36. #include <windows.h>
  37. #include <shellapi.h>
  38. #endif
  39. #ifdef _WIN32
  40. extern "C" {
  41. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  42. // graphics
  43. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  44. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  45. }
  46. #endif
  47. static void PrintHelp(const char* argv0) {
  48. std::cout << "Usage: " << argv0
  49. << " [options] <filename>\n"
  50. "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
  51. "-f, --fullscreen Start in fullscreen mode\n"
  52. "-h, --help Display this help and exit\n"
  53. "-v, --version Output version information and exit\n";
  54. }
  55. static void PrintVersion() {
  56. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  57. }
  58. static void InitializeLogging() {
  59. Log::Filter log_filter(Log::Level::Debug);
  60. log_filter.ParseFilterString(Settings::values.log_filter);
  61. Log::SetGlobalFilter(log_filter);
  62. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  63. const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  64. FileUtil::CreateFullPath(log_dir);
  65. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  66. #ifdef _WIN32
  67. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  68. #endif
  69. }
  70. /// Application entry point
  71. int main(int argc, char** argv) {
  72. Common::DetachedTasks detached_tasks;
  73. Config config;
  74. int option_index = 0;
  75. bool use_gdbstub = Settings::values.use_gdbstub;
  76. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  77. InitializeLogging();
  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. MicroProfileOnThreadCreate("EmuThread");
  135. SCOPE_EXIT({ MicroProfileShutdown(); });
  136. if (filepath.empty()) {
  137. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  138. return -1;
  139. }
  140. // Apply the command line arguments
  141. Settings::values.gdbstub_port = gdb_port;
  142. Settings::values.use_gdbstub = use_gdbstub;
  143. Settings::Apply();
  144. std::unique_ptr<EmuWindow_SDL2> emu_window{std::make_unique<EmuWindow_SDL2>(fullscreen)};
  145. if (!Settings::values.use_multi_core) {
  146. // Single core mode must acquire OpenGL context for entire emulation session
  147. emu_window->MakeCurrent();
  148. }
  149. Core::System& system{Core::System::GetInstance()};
  150. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  151. Service::FileSystem::CreateFactories(system.GetFilesystem());
  152. SCOPE_EXIT({ system.Shutdown(); });
  153. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  154. switch (load_result) {
  155. case Core::System::ResultStatus::ErrorGetLoader:
  156. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
  157. return -1;
  158. case Core::System::ResultStatus::ErrorLoader:
  159. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  160. return -1;
  161. case Core::System::ResultStatus::ErrorNotInitialized:
  162. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  163. return -1;
  164. case Core::System::ResultStatus::ErrorSystemMode:
  165. LOG_CRITICAL(Frontend, "Failed to determine system mode!");
  166. return -1;
  167. case Core::System::ResultStatus::ErrorVideoCore:
  168. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  169. return -1;
  170. case Core::System::ResultStatus::Success:
  171. break; // Expected case
  172. default:
  173. if (static_cast<u32>(load_result) >
  174. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  175. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  176. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  177. LOG_CRITICAL(Frontend,
  178. "While attempting to load the ROM requested, an error occured. Please "
  179. "refer to the yuzu wiki for more information or the yuzu discord for "
  180. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  181. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  182. }
  183. }
  184. Core::Telemetry().AddField(Telemetry::FieldType::App, "Frontend", "SDL");
  185. while (emu_window->IsOpen()) {
  186. system.RunLoop();
  187. }
  188. detached_tasks.WaitForAllTasks();
  189. return 0;
  190. }