yuzu.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // Copyright 2019 yuzu 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/registered_cache.h"
  23. #include "core/file_sys/vfs_real.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_tester/config.h"
  30. #include "yuzu_tester/emu_window/emu_window_sdl2_hide.h"
  31. #include "yuzu_tester/service/yuzutest.h"
  32. #ifdef _WIN32
  33. // windows.h needs to be included before shellapi.h
  34. #include <windows.h>
  35. #include <shellapi.h>
  36. #endif
  37. #undef _UNICODE
  38. #include <getopt.h>
  39. #ifndef _MSC_VER
  40. #include <unistd.h>
  41. #endif
  42. #ifdef _WIN32
  43. extern "C" {
  44. // tells Nvidia and AMD drivers to use the dedicated GPU by default on laptops with switchable
  45. // graphics
  46. __declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
  47. __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
  48. }
  49. #endif
  50. static void PrintHelp(const char* argv0) {
  51. std::cout << "Usage: " << argv0
  52. << " [options] <filename>\n"
  53. "-h, --help Display this help and exit\n"
  54. "-v, --version Output version information and exit\n"
  55. "-d, --datastring Pass following string as data to test service command #2\n"
  56. "-l, --log Log to console in addition to file (will log to file only "
  57. "by default)\n";
  58. }
  59. static void PrintVersion() {
  60. std::cout << "yuzu [Test Utility] " << Common::g_scm_branch << " " << Common::g_scm_desc
  61. << std::endl;
  62. }
  63. static void InitializeLogging(bool console) {
  64. Log::Filter log_filter(Log::Level::Debug);
  65. log_filter.ParseFilterString(Settings::values.log_filter);
  66. Log::SetGlobalFilter(log_filter);
  67. if (console)
  68. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  69. const std::string& log_dir = FileUtil::GetUserPath(FileUtil::UserPath::LogDir);
  70. FileUtil::CreateFullPath(log_dir);
  71. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  72. #ifdef _WIN32
  73. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  74. #endif
  75. }
  76. /// Application entry point
  77. int main(int argc, char** argv) {
  78. Common::DetachedTasks detached_tasks;
  79. Config config;
  80. int option_index = 0;
  81. #ifdef _WIN32
  82. int argc_w;
  83. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  84. if (argv_w == nullptr) {
  85. std::cout << "Failed to get command line arguments" << std::endl;
  86. return -1;
  87. }
  88. #endif
  89. std::string filepath;
  90. static struct option long_options[] = {
  91. {"help", no_argument, 0, 'h'},
  92. {"version", no_argument, 0, 'v'},
  93. {"datastring", optional_argument, 0, 'd'},
  94. {"log", no_argument, 0, 'l'},
  95. {0, 0, 0, 0},
  96. };
  97. bool console_log = false;
  98. std::string datastring;
  99. while (optind < argc) {
  100. int arg = getopt_long(argc, argv, "hvdl::", long_options, &option_index);
  101. if (arg != -1) {
  102. switch (static_cast<char>(arg)) {
  103. case 'h':
  104. PrintHelp(argv[0]);
  105. return 0;
  106. case 'v':
  107. PrintVersion();
  108. return 0;
  109. case 'd':
  110. datastring = argv[optind];
  111. ++optind;
  112. break;
  113. case 'l':
  114. console_log = true;
  115. break;
  116. }
  117. } else {
  118. #ifdef _WIN32
  119. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  120. #else
  121. filepath = argv[optind];
  122. #endif
  123. optind++;
  124. }
  125. }
  126. InitializeLogging(console_log);
  127. #ifdef _WIN32
  128. LocalFree(argv_w);
  129. #endif
  130. MicroProfileOnThreadCreate("EmuThread");
  131. SCOPE_EXIT({ MicroProfileShutdown(); });
  132. if (filepath.empty()) {
  133. LOG_CRITICAL(Frontend, "Failed to load application: No application specified");
  134. std::cout << "Failed to load application: No application specified" << std::endl;
  135. PrintHelp(argv[0]);
  136. return -1;
  137. }
  138. Settings::values.use_gdbstub = false;
  139. Settings::Apply();
  140. std::unique_ptr<EmuWindow_SDL2_Hide> emu_window{std::make_unique<EmuWindow_SDL2_Hide>()};
  141. if (!Settings::values.use_multi_core) {
  142. // Single core mode must acquire OpenGL context for entire emulation session
  143. emu_window->MakeCurrent();
  144. }
  145. bool finished = false;
  146. int return_value = 0;
  147. const auto callback = [&finished,
  148. &return_value](std::vector<Service::Yuzu::TestResult> results) {
  149. finished = true;
  150. return_value = 0;
  151. // Find the minimum length needed to fully enclose all test names (and the header field) in
  152. // the fmt::format column by first finding the maximum size of any test name and comparing
  153. // that to 9, the string length of 'Test Name'
  154. const auto needed_length_name =
  155. std::max<u64>(std::max_element(results.begin(), results.end(),
  156. [](const auto& lhs, const auto& rhs) {
  157. return lhs.name.size() < rhs.name.size();
  158. })
  159. ->name.size(),
  160. 9ull);
  161. std::size_t passed = 0;
  162. std::size_t failed = 0;
  163. std::cout << fmt::format("Result [Res Code] | {:<{}} | Extra Data", "Test Name",
  164. needed_length_name)
  165. << std::endl;
  166. for (const auto& res : results) {
  167. const auto main_res = res.code == 0 ? "PASSED" : "FAILED";
  168. if (res.code == 0)
  169. ++passed;
  170. else
  171. ++failed;
  172. std::cout << fmt::format("{} [{:08X}] | {:<{}} | {}", main_res, res.code, res.name,
  173. needed_length_name, res.data)
  174. << std::endl;
  175. }
  176. std::cout << std::endl
  177. << fmt::format("{:4d} Passed | {:4d} Failed | {:4d} Total | {:2.2f} Passed Ratio",
  178. passed, failed, passed + failed,
  179. static_cast<float>(passed) / (passed + failed))
  180. << std::endl
  181. << (failed == 0 ? "PASSED" : "FAILED") << std::endl;
  182. if (failed > 0)
  183. return_value = -1;
  184. };
  185. Core::System& system{Core::System::GetInstance()};
  186. system.SetContentProvider(std::make_unique<FileSys::ContentProviderUnion>());
  187. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  188. system.GetFileSystemController().CreateFactories(*system.GetFilesystem());
  189. SCOPE_EXIT({ system.Shutdown(); });
  190. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  191. switch (load_result) {
  192. case Core::System::ResultStatus::ErrorGetLoader:
  193. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  194. return -1;
  195. case Core::System::ResultStatus::ErrorLoader:
  196. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  197. return -1;
  198. case Core::System::ResultStatus::ErrorNotInitialized:
  199. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  200. return -1;
  201. case Core::System::ResultStatus::ErrorVideoCore:
  202. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  203. return -1;
  204. case Core::System::ResultStatus::Success:
  205. break; // Expected case
  206. default:
  207. if (static_cast<u32>(load_result) >
  208. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  209. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  210. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  211. LOG_CRITICAL(Frontend,
  212. "While attempting to load the ROM requested, an error occured. Please "
  213. "refer to the yuzu wiki for more information or the yuzu discord for "
  214. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  215. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  216. }
  217. }
  218. Service::Yuzu::InstallInterfaces(system.ServiceManager(), datastring, callback);
  219. system.TelemetrySession().AddField(Telemetry::FieldType::App, "Frontend", "SDLHideTester");
  220. system.Renderer().Rasterizer().LoadDiskResources();
  221. while (!finished) {
  222. system.RunLoop();
  223. }
  224. detached_tasks.WaitForAllTasks();
  225. return return_value;
  226. }