yuzu.cpp 7.4 KB

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