citra.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // This needs to be included before getopt.h because the latter #defines symbols used by it
  9. #include "common/microprofile.h"
  10. #ifdef _MSC_VER
  11. #include <getopt.h>
  12. #else
  13. #include <getopt.h>
  14. #include <unistd.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <windows.h>
  18. #endif
  19. #include "citra/config.h"
  20. #include "citra/emu_window/emu_window_sdl2.h"
  21. #include "common/logging/backend.h"
  22. #include "common/logging/filter.h"
  23. #include "common/logging/log.h"
  24. #include "common/scm_rev.h"
  25. #include "common/scope_exit.h"
  26. #include "common/string_util.h"
  27. #include "core/core.h"
  28. #include "core/gdbstub/gdbstub.h"
  29. #include "core/loader/loader.h"
  30. #include "core/settings.h"
  31. #include "video_core/video_core.h"
  32. static void PrintHelp(const char* argv0) {
  33. std::cout << "Usage: " << argv0
  34. << " [options] <filename>\n"
  35. "-g, --gdbport=NUMBER Enable gdb stub on port NUMBER\n"
  36. "-h, --help Display this help and exit\n"
  37. "-v, --version Output version information and exit\n";
  38. }
  39. static void PrintVersion() {
  40. std::cout << "Citra " << Common::g_scm_branch << " " << Common::g_scm_desc << std::endl;
  41. }
  42. /// Application entry point
  43. int main(int argc, char** argv) {
  44. Config config;
  45. int option_index = 0;
  46. bool use_gdbstub = Settings::values.use_gdbstub;
  47. u32 gdb_port = static_cast<u32>(Settings::values.gdbstub_port);
  48. char* endarg;
  49. #ifdef _WIN32
  50. int argc_w;
  51. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  52. if (argv_w == nullptr) {
  53. LOG_CRITICAL(Frontend, "Failed to get command line arguments");
  54. return -1;
  55. }
  56. #endif
  57. std::string filepath;
  58. static struct option long_options[] = {
  59. {"gdbport", required_argument, 0, 'g'},
  60. {"help", no_argument, 0, 'h'},
  61. {"version", no_argument, 0, 'v'},
  62. {0, 0, 0, 0},
  63. };
  64. while (optind < argc) {
  65. char arg = getopt_long(argc, argv, "g:hv", long_options, &option_index);
  66. if (arg != -1) {
  67. switch (arg) {
  68. case 'g':
  69. errno = 0;
  70. gdb_port = strtoul(optarg, &endarg, 0);
  71. use_gdbstub = true;
  72. if (endarg == optarg)
  73. errno = EINVAL;
  74. if (errno != 0) {
  75. perror("--gdbport");
  76. exit(1);
  77. }
  78. break;
  79. case 'h':
  80. PrintHelp(argv[0]);
  81. return 0;
  82. case 'v':
  83. PrintVersion();
  84. return 0;
  85. }
  86. } else {
  87. #ifdef _WIN32
  88. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  89. #else
  90. filepath = argv[optind];
  91. #endif
  92. optind++;
  93. }
  94. }
  95. #ifdef _WIN32
  96. LocalFree(argv_w);
  97. #endif
  98. Log::Filter log_filter(Log::Level::Debug);
  99. Log::SetFilter(&log_filter);
  100. MicroProfileOnThreadCreate("EmuThread");
  101. SCOPE_EXIT({ MicroProfileShutdown(); });
  102. if (filepath.empty()) {
  103. LOG_CRITICAL(Frontend, "Failed to load ROM: No ROM specified");
  104. return -1;
  105. }
  106. log_filter.ParseFilterString(Settings::values.log_filter);
  107. // Apply the command line arguments
  108. Settings::values.gdbstub_port = gdb_port;
  109. Settings::values.use_gdbstub = use_gdbstub;
  110. Settings::Apply();
  111. std::unique_ptr<EmuWindow_SDL2> emu_window{std::make_unique<EmuWindow_SDL2>()};
  112. Core::System& system{Core::System::GetInstance()};
  113. SCOPE_EXIT({ system.Shutdown(); });
  114. const Core::System::ResultStatus load_result{system.Load(emu_window.get(), filepath)};
  115. switch (load_result) {
  116. case Core::System::ResultStatus::ErrorGetLoader:
  117. LOG_CRITICAL(Frontend, "Failed to obtain loader for %s!", filepath.c_str());
  118. return -1;
  119. case Core::System::ResultStatus::ErrorLoader:
  120. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  121. return -1;
  122. case Core::System::ResultStatus::ErrorLoader_ErrorEncrypted:
  123. LOG_CRITICAL(Frontend, "The game that you are trying to load must be decrypted before "
  124. "being used with Citra. \n\n For more information on dumping and "
  125. "decrypting games, please refer to: "
  126. "https://citra-emu.org/wiki/Dumping-Game-Cartridges");
  127. return -1;
  128. case Core::System::ResultStatus::ErrorLoader_ErrorInvalidFormat:
  129. LOG_CRITICAL(Frontend, "Error while loading ROM: The ROM format is not supported.");
  130. return -1;
  131. case Core::System::ResultStatus::ErrorNotInitialized:
  132. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  133. return -1;
  134. case Core::System::ResultStatus::ErrorSystemMode:
  135. LOG_CRITICAL(Frontend, "Failed to determine system mode!");
  136. return -1;
  137. case Core::System::ResultStatus::ErrorVideoCore:
  138. LOG_CRITICAL(Frontend, "VideoCore not initialized");
  139. return -1;
  140. case Core::System::ResultStatus::Success:
  141. break; // Expected case
  142. }
  143. while (emu_window->IsOpen()) {
  144. system.RunLoop();
  145. }
  146. return 0;
  147. }