yuzu.cpp 6.2 KB

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