yuzu.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 "video_core/renderer_base.h"
  29. #include "yuzu_cmd/config.h"
  30. #include "yuzu_cmd/emu_window/emu_window_sdl2.h"
  31. #include <getopt.h>
  32. #ifndef _MSC_VER
  33. #include <unistd.h>
  34. #endif
  35. #ifdef _WIN32
  36. // windows.h needs to be included before shellapi.h
  37. #include <windows.h>
  38. #include <shellapi.h>
  39. #endif
  40. #ifdef _WIN32
  41. extern "C" {
  42. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  43. // graphics
  44. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  45. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  46. }
  47. #endif
  48. static void PrintHelp(const char* argv0) {
  49. std::cout << "Usage: " << argv0
  50. << " [options] <filename>\n"
  51. "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
  52. "-f, --fullscreen Start in fullscreen mode\n"
  53. "-h, --help Display this help and exit\n"
  54. "-v, --version Output version information and exit\n"
  55. "-p, --program Pass following string as arguments to executable\n";
  56. }
  57. static void PrintVersion() {
  58. std::cout << "yuzu " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  59. }
  60. static void InitializeLogging() {
  61. Log::Filter log_filter(Log::Level::Debug);
  62. log_filter.ParseFilterString(Settings::values.log_filter);
  63. Log::SetGlobalFilter(log_filter);
  64. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  65. const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  66. FileUtil::CreateFullPath(log_dir);
  67. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  68. #ifdef _WIN32
  69. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  70. #endif
  71. }
  72. /// Application entry point
  73. int main(int argc, char** argv) {
  74. Common::DetachedTasks detached_tasks;
  75. Config config;
  76. int option_index = 0;
  77. bool use_gdbstub = Settings::values.use_gdbstub;
  78. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  79. InitializeLogging();
  80. char* endarg;
  81. #ifdef _WIN32
  82. int argc_w;
  83. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  84. if (argv_w == nullptr) {
  85. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  86. return -1;
  87. }
  88. #endif
  89. std::string filepath;
  90. bool fullscreen = false;
  91. static struct option long_options[] = {
  92. {"gdbport", required_argument, 0, 'g'}, {"fullscreen", no_argument, 0, 'f'},
  93. {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'v'},
  94. {"program", optional_argument, 0, 'p'}, {0, 0, 0, 0},
  95. };
  96. while (optind < argc) {
  97. char arg = getopt_long(argc, argv, "g:fhvp::", 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. case 'p':
  122. Settings::values.program_args = argv[optind];
  123. ++optind;
  124. break;
  125. }
  126. } else {
  127. #ifdef _WIN32
  128. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  129. #else
  130. filepath = argv[optind];
  131. #endif
  132. optind++;
  133. }
  134. }
  135. #ifdef _WIN32
  136. LocalFree(argv_w);
  137. #endif
  138. MicroProfileOnThreadCreate("EmuThread");
  139. SCOPE_EXIT({ MicroProfileShutdown(); });
  140. if (filepath.empty()) {
  141. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  142. return -1;
  143. }
  144. // Apply the command line arguments
  145. Settings::values.gdbstub_port = gdb_port;
  146. Settings::values.use_gdbstub = use_gdbstub;
  147. Settings::Apply();
  148. std::unique_ptr<EmuWindow_SDL2> emu_window{std::make_unique<EmuWindow_SDL2>(fullscreen)};
  149. if (!Settings::values.use_multi_core) {
  150. // Single core mode must acquire OpenGL context for entire emulation session
  151. emu_window->MakeCurrent();
  152. }
  153. Core::System& system{Core::System::GetInstance()};
  154. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  155. Service::FileSystem::CreateFactories(*system.GetFilesystem());
  156. SCOPE_EXIT({ system.Shutdown(); });
  157. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  158. switch (load_result) {
  159. case Core::System::ResultStatus::ErrorGetLoader:
  160. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
  161. return -1;
  162. case Core::System::ResultStatus::ErrorLoader:
  163. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  164. return -1;
  165. case Core::System::ResultStatus::ErrorNotInitialized:
  166. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  167. return -1;
  168. case Core::System::ResultStatus::ErrorSystemMode:
  169. LOG_CRITICAL(Frontend, "Failed to determine system mode!");
  170. return -1;
  171. case Core::System::ResultStatus::ErrorVideoCore:
  172. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  173. return -1;
  174. case Core::System::ResultStatus::Success:
  175. break; // Expected case
  176. default:
  177. if (static_cast<u32>(load_result) >
  178. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  179. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  180. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  181. LOG_CRITICAL(Frontend,
  182. "While attempting to load the ROM requested, an error occured. Please "
  183. "refer to the yuzu wiki for more information or the yuzu discord for "
  184. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  185. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  186. }
  187. }
  188. system.TelemetrySession().AddField(Telemetry::FieldType::App, "Frontend", "SDL");
  189. system.Renderer().Rasterizer().LoadDiskResources();
  190. while (emu_window->IsOpen()) {
  191. system.RunLoop();
  192. }
  193. detached_tasks.WaitForAllTasks();
  194. return 0;
  195. }