yuzu.cpp 7.1 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. "-p, --program Pass following string as arguments to executable\n";
  55. }
  56. static void PrintVersion() {
  57. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  58. }
  59. static void InitializeLogging() {
  60. Log::Filter log_filter(Log::Level::Debug);
  61. log_filter.ParseFilterString(Settings::values.log_filter);
  62. Log::SetGlobalFilter(log_filter);
  63. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  64. const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  65. FileUtil::CreateFullPath(log_dir);
  66. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  67. }
  68. /// Application entry point
  69. int main(int argc, char** argv) {
  70. Common::DetachedTasks detached_tasks;
  71. Config config;
  72. int option_index = 0;
  73. bool use_gdbstub = Settings::values.use_gdbstub;
  74. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  75. InitializeLogging();
  76. char* endarg;
  77. #ifdef _WIN32
  78. int argc_w;
  79. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  80. if (argv_w == nullptr) {
  81. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  82. return -1;
  83. }
  84. #endif
  85. std::string filepath;
  86. bool fullscreen = false;
  87. static struct option long_options[] = {
  88. {"gdbport", required_argument, 0, 'g'}, {"fullscreen", no_argument, 0, 'f'},
  89. {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'},
  90. {"program", optional_argument, 0, 'p'}, {0, 0, 0, 0},
  91. };
  92. while (optind < argc) {
  93. char arg = getopt_long(argc, argv, "g:fhvp::", long_options, &option_index);
  94. if (arg != -1) {
  95. switch (arg) {
  96. case 'g':
  97. errno = 0;
  98. gdb_port = strtoul(optarg, &endarg, 0);
  99. use_gdbstub = true;
  100. if (endarg == optarg)
  101. errno = EINVAL;
  102. if (errno != 0) {
  103. perror("--gdbport");
  104. exit(1);
  105. }
  106. break;
  107. case 'f':
  108. fullscreen = true;
  109. LOG_INFO(Frontend, "Starting in fullscreen mode...");
  110. break;
  111. case 'h':
  112. PrintHelp(argv[0]);
  113. return 0;
  114. case 'v':
  115. PrintVersion();
  116. return 0;
  117. case 'p':
  118. Settings::values.program_args = argv[optind];
  119. ++optind;
  120. break;
  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. }