yuzu.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <chrono>
  5. #include <iostream>
  6. #include <memory>
  7. #include <string>
  8. #include <thread>
  9. #include <fmt/ostream.h>
  10. #include "common/common_paths.h"
  11. #include "common/detached_tasks.h"
  12. #include "common/file_util.h"
  13. #include "common/logging/backend.h"
  14. #include "common/logging/filter.h"
  15. #include "common/logging/log.h"
  16. #include "common/microprofile.h"
  17. #include "common/scm_rev.h"
  18. #include "common/scope_exit.h"
  19. #include "common/string_util.h"
  20. #include "common/telemetry.h"
  21. #include "core/core.h"
  22. #include "core/crypto/key_manager.h"
  23. #include "core/file_sys/registered_cache.h"
  24. #include "core/file_sys/vfs_real.h"
  25. #include "core/hle/service/filesystem/filesystem.h"
  26. #include "core/loader/loader.h"
  27. #include "core/settings.h"
  28. #include "core/telemetry_session.h"
  29. #include "video_core/renderer_base.h"
  30. #include "yuzu_tester/config.h"
  31. #include "yuzu_tester/emu_window/emu_window_sdl2_hide.h"
  32. #include "yuzu_tester/service/yuzutest.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. "-h, --help Display this help and exit\n"
  55. "-v, --version Output version information and exit\n"
  56. "-d, --datastring Pass following string as data to test service command #2\n"
  57. "-l, --log Log to console in addition to file (will log to file only "
  58. "by default)\n";
  59. }
  60. static void PrintVersion() {
  61. std::cout << "yuzu [Test Utility] " << Common::g_scm_branch << " " << Common::g_scm_desc
  62. << std::endl;
  63. }
  64. static void InitializeLogging(bool console) {
  65. Log::Filter log_filter(Log::Level::Debug);
  66. log_filter.ParseFilterString(Settings::values.log_filter);
  67. Log::SetGlobalFilter(log_filter);
  68. if (console)
  69. Log::AddBackend(std::make_unique<Log::ColorConsoleBackend>());
  70. const std::string& log_dir = Common::FS::GetUserPath(Common::FS::UserPath::LogDir);
  71. Common::FS::CreateFullPath(log_dir);
  72. Log::AddBackend(std::make_unique<Log::FileBackend>(log_dir + LOG_FILE));
  73. #ifdef _WIN32
  74. Log::AddBackend(std::make_unique<Log::DebuggerBackend>());
  75. #endif
  76. }
  77. /// Application entry point
  78. int main(int argc, char** argv) {
  79. Common::DetachedTasks detached_tasks;
  80. Config config;
  81. int option_index = 0;
  82. #ifdef _WIN32
  83. int argc_w;
  84. auto argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
  85. if (argv_w == nullptr) {
  86. std::cout << "Failed to get command line arguments" << std::endl;
  87. return -1;
  88. }
  89. #endif
  90. std::string filepath;
  91. static struct option long_options[] = {
  92. {"help", no_argument, 0, 'h'},
  93. {"version", no_argument, 0, 'v'},
  94. {"datastring", optional_argument, 0, 'd'},
  95. {"log", no_argument, 0, 'l'},
  96. {0, 0, 0, 0},
  97. };
  98. bool console_log = false;
  99. std::string datastring;
  100. while (optind < argc) {
  101. int arg = getopt_long(argc, argv, "hvdl::", long_options, &option_index);
  102. if (arg != -1) {
  103. switch (static_cast<char>(arg)) {
  104. case 'h':
  105. PrintHelp(argv[0]);
  106. return 0;
  107. case 'v':
  108. PrintVersion();
  109. return 0;
  110. case 'd':
  111. datastring = argv[optind];
  112. ++optind;
  113. break;
  114. case 'l':
  115. console_log = true;
  116. break;
  117. }
  118. } else {
  119. #ifdef _WIN32
  120. filepath = Common::UTF16ToUTF8(argv_w[optind]);
  121. #else
  122. filepath = argv[optind];
  123. #endif
  124. optind++;
  125. }
  126. }
  127. InitializeLogging(console_log);
  128. #ifdef _WIN32
  129. LocalFree(argv_w);
  130. #endif
  131. MicroProfileOnThreadCreate("EmuThread");
  132. SCOPE_EXIT({ MicroProfileShutdown(); });
  133. if (filepath.empty()) {
  134. LOG_CRITICAL(Frontend, "Failed to load application: No application specified");
  135. std::cout << "Failed to load application: No application specified" << std::endl;
  136. PrintHelp(argv[0]);
  137. return -1;
  138. }
  139. Settings::values.use_gdbstub = false;
  140. Settings::Apply();
  141. std::unique_ptr<EmuWindow_SDL2_Hide> emu_window{std::make_unique<EmuWindow_SDL2_Hide>()};
  142. bool finished = false;
  143. int return_value = 0;
  144. const auto callback = [&finished,
  145. &return_value](std::vector<Service::Yuzu::TestResult> results) {
  146. finished = true;
  147. return_value = 0;
  148. // Find the minimum length needed to fully enclose all test names (and the header field) in
  149. // the fmt::format column by first finding the maximum size of any test name and comparing
  150. // that to 9, the string length of 'Test Name'
  151. const auto needed_length_name =
  152. std::max<u64>(std::max_element(results.begin(), results.end(),
  153. [](const auto& lhs, const auto& rhs) {
  154. return lhs.name.size() < rhs.name.size();
  155. })
  156. ->name.size(),
  157. 9ull);
  158. std::size_t passed = 0;
  159. std::size_t failed = 0;
  160. std::cout << fmt::format("Result [Res Code] | {:<{}} | Extra Data", "Test Name",
  161. needed_length_name)
  162. << std::endl;
  163. for (const auto& res : results) {
  164. const auto main_res = res.code == 0 ? "PASSED" : "FAILED";
  165. if (res.code == 0)
  166. ++passed;
  167. else
  168. ++failed;
  169. std::cout << fmt::format("{} [{:08X}] | {:<{}} | {}", main_res, res.code, res.name,
  170. needed_length_name, res.data)
  171. << std::endl;
  172. }
  173. std::cout << std::endl
  174. << fmt::format("{:4d} Passed | {:4d} Failed | {:4d} Total | {:2.2f} Passed Ratio",
  175. passed, failed, passed + failed,
  176. static_cast<float>(passed) / (passed + failed))
  177. << std::endl
  178. << (failed == 0 ? "PASSED" : "FAILED") << std::endl;
  179. if (failed > 0)
  180. return_value = -1;
  181. };
  182. Core::System& system{Core::System::GetInstance()};
  183. system.SetContentProvider(std::make_unique<FileSys::ContentProviderUnion>());
  184. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  185. system.GetFileSystemController().CreateFactories(*system.GetFilesystem());
  186. SCOPE_EXIT({ system.Shutdown(); });
  187. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  188. switch (load_result) {
  189. case Core::System::ResultStatus::ErrorGetLoader:
  190. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  191. return -1;
  192. case Core::System::ResultStatus::ErrorLoader:
  193. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  194. return -1;
  195. case Core::System::ResultStatus::ErrorNotInitialized:
  196. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  197. return -1;
  198. case Core::System::ResultStatus::ErrorVideoCore:
  199. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  200. return -1;
  201. case Core::System::ResultStatus::Success:
  202. break; // Expected case
  203. default:
  204. if (static_cast<u32>(load_result) >
  205. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  206. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  207. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  208. LOG_CRITICAL(Frontend,
  209. "While attempting to load the ROM requested, an error occured. Please "
  210. "refer to the yuzu wiki for more information or the yuzu discord for "
  211. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  212. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  213. }
  214. }
  215. Service::Yuzu::InstallInterfaces(system.ServiceManager(), datastring, callback);
  216. system.TelemetrySession().AddField(Common::Telemetry::FieldType::App, "Frontend",
  217. "SDLHideTester");
  218. system.GPU().Start();
  219. system.Run();
  220. while (!finished) {
  221. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  222. }
  223. system.Pause();
  224. detached_tasks.WaitForAllTasks();
  225. return return_value;
  226. }