yuzu.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. bool finished = false;
  142. int return_value = 0;
  143. const auto callback = [&finished,
  144. &return_value](std::vector<Service::Yuzu::TestResult> results) {
  145. finished = true;
  146. return_value = 0;
  147. // Find the minimum length needed to fully enclose all test names (and the header field) in
  148. // the fmt::format column by first finding the maximum size of any test name and comparing
  149. // that to 9, the string length of 'Test Name'
  150. const auto needed_length_name =
  151. std::max<u64>(std::max_element(results.begin(), results.end(),
  152. [](const auto& lhs, const auto& rhs) {
  153. return lhs.name.size() < rhs.name.size();
  154. })
  155. ->name.size(),
  156. 9ull);
  157. std::size_t passed = 0;
  158. std::size_t failed = 0;
  159. std::cout << fmt::format("Result [Res Code] | {:<{}} | Extra Data", "Test Name",
  160. needed_length_name)
  161. << std::endl;
  162. for (const auto& res : results) {
  163. const auto main_res = res.code == 0 ? "PASSED" : "FAILED";
  164. if (res.code == 0)
  165. ++passed;
  166. else
  167. ++failed;
  168. std::cout << fmt::format("{} [{:08X}] | {:<{}} | {}", main_res, res.code, res.name,
  169. needed_length_name, res.data)
  170. << std::endl;
  171. }
  172. std::cout << std::endl
  173. << fmt::format("{:4d} Passed | {:4d} Failed | {:4d} Total | {:2.2f} Passed Ratio",
  174. passed, failed, passed + failed,
  175. static_cast<float>(passed) / (passed + failed))
  176. << std::endl
  177. << (failed == 0 ? "PASSED" : "FAILED") << std::endl;
  178. if (failed > 0)
  179. return_value = -1;
  180. };
  181. Core::System& system{Core::System::GetInstance()};
  182. system.SetContentProvider(std::make_unique<FileSys::ContentProviderUnion>());
  183. system.SetFilesystem(std::make_shared<FileSys::RealVfsFilesystem>());
  184. system.GetFileSystemController().CreateFactories(*system.GetFilesystem());
  185. SCOPE_EXIT({ system.Shutdown(); });
  186. const Core::System::ResultStatus load_result{system.Load(*emu_window, filepath)};
  187. switch (load_result) {
  188. case Core::System::ResultStatus::ErrorGetLoader:
  189. LOG_CRITICAL(Frontend, "Failed to obtain loader for {}!", filepath);
  190. return -1;
  191. case Core::System::ResultStatus::ErrorLoader:
  192. LOG_CRITICAL(Frontend, "Failed to load ROM!");
  193. return -1;
  194. case Core::System::ResultStatus::ErrorNotInitialized:
  195. LOG_CRITICAL(Frontend, "CPUCore not initialized");
  196. return -1;
  197. case Core::System::ResultStatus::ErrorVideoCore:
  198. LOG_CRITICAL(Frontend, "Failed to initialize VideoCore!");
  199. return -1;
  200. case Core::System::ResultStatus::Success:
  201. break; // Expected case
  202. default:
  203. if (static_cast<u32>(load_result) >
  204. static_cast<u32>(Core::System::ResultStatus::ErrorLoader)) {
  205. const u16 loader_id = static_cast<u16>(Core::System::ResultStatus::ErrorLoader);
  206. const u16 error_id = static_cast<u16>(load_result) - loader_id;
  207. LOG_CRITICAL(Frontend,
  208. "While attempting to load the ROM requested, an error occured. Please "
  209. "refer to the yuzu wiki for more information or the yuzu discord for "
  210. "additional help.\n\nError Code: {:04X}-{:04X}\nError Description: {}",
  211. loader_id, error_id, static_cast<Loader::ResultStatus>(error_id));
  212. }
  213. }
  214. Service::Yuzu::InstallInterfaces(system.ServiceManager(), datastring, callback);
  215. system.TelemetrySession().AddField(Telemetry::FieldType::App, "Frontend", "SDLHideTester");
  216. system.GPU().Start();
  217. system.Renderer().Rasterizer().LoadDiskResources();
  218. while (!finished) {
  219. system.RunLoop();
  220. }
  221. detached_tasks.WaitForAllTasks();
  222. return return_value;
  223. }