os.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /**************************************************************************/
  2. /* os.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "os.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/dir_access.h"
  34. #include "core/io/file_access.h"
  35. #include "core/io/json.h"
  36. #include "core/os/midi_driver.h"
  37. #include "core/version_generated.gen.h"
  38. #include <stdarg.h>
  39. #ifdef MINGW_ENABLED
  40. #define MINGW_STDTHREAD_REDUNDANCY_WARNING
  41. #include "thirdparty/mingw-std-threads/mingw.thread.h"
  42. #define THREADING_NAMESPACE mingw_stdthread
  43. #else
  44. #include <thread>
  45. #define THREADING_NAMESPACE std
  46. #endif
  47. OS *OS::singleton = nullptr;
  48. uint64_t OS::target_ticks = 0;
  49. OS *OS::get_singleton() {
  50. return singleton;
  51. }
  52. uint64_t OS::get_ticks_msec() const {
  53. return get_ticks_usec() / 1000ULL;
  54. }
  55. double OS::get_unix_time() const {
  56. return 0;
  57. }
  58. void OS::_set_logger(CompositeLogger *p_logger) {
  59. if (_logger) {
  60. memdelete(_logger);
  61. }
  62. _logger = p_logger;
  63. }
  64. void OS::add_logger(Logger *p_logger) {
  65. if (!_logger) {
  66. Vector<Logger *> loggers;
  67. loggers.push_back(p_logger);
  68. _logger = memnew(CompositeLogger(loggers));
  69. } else {
  70. _logger->add_logger(p_logger);
  71. }
  72. }
  73. String OS::get_identifier() const {
  74. return get_name().to_lower();
  75. }
  76. void OS::print_error(const char *p_function, const char *p_file, int p_line, const char *p_code, const char *p_rationale, bool p_editor_notify, Logger::ErrorType p_type) {
  77. if (!_stderr_enabled) {
  78. return;
  79. }
  80. if (_logger) {
  81. _logger->log_error(p_function, p_file, p_line, p_code, p_rationale, p_editor_notify, p_type);
  82. }
  83. }
  84. void OS::print(const char *p_format, ...) {
  85. if (!_stdout_enabled) {
  86. return;
  87. }
  88. va_list argp;
  89. va_start(argp, p_format);
  90. if (_logger) {
  91. _logger->logv(p_format, argp, false);
  92. }
  93. va_end(argp);
  94. }
  95. void OS::print_rich(const char *p_format, ...) {
  96. if (!_stdout_enabled) {
  97. return;
  98. }
  99. va_list argp;
  100. va_start(argp, p_format);
  101. if (_logger) {
  102. _logger->logv(p_format, argp, false);
  103. }
  104. va_end(argp);
  105. }
  106. void OS::printerr(const char *p_format, ...) {
  107. if (!_stderr_enabled) {
  108. return;
  109. }
  110. va_list argp;
  111. va_start(argp, p_format);
  112. if (_logger) {
  113. _logger->logv(p_format, argp, true);
  114. }
  115. va_end(argp);
  116. }
  117. void OS::alert(const String &p_alert, const String &p_title) {
  118. fprintf(stderr, "%s: %s\n", p_title.utf8().get_data(), p_alert.utf8().get_data());
  119. }
  120. void OS::set_low_processor_usage_mode(bool p_enabled) {
  121. low_processor_usage_mode = p_enabled;
  122. }
  123. bool OS::is_in_low_processor_usage_mode() const {
  124. return low_processor_usage_mode;
  125. }
  126. void OS::set_low_processor_usage_mode_sleep_usec(int p_usec) {
  127. low_processor_usage_mode_sleep_usec = p_usec;
  128. }
  129. int OS::get_low_processor_usage_mode_sleep_usec() const {
  130. return low_processor_usage_mode_sleep_usec;
  131. }
  132. void OS::set_delta_smoothing(bool p_enabled) {
  133. _delta_smoothing_enabled = p_enabled;
  134. }
  135. bool OS::is_delta_smoothing_enabled() const {
  136. return _delta_smoothing_enabled;
  137. }
  138. String OS::get_executable_path() const {
  139. return _execpath;
  140. }
  141. int OS::get_process_id() const {
  142. return -1;
  143. }
  144. bool OS::is_stdout_verbose() const {
  145. return _verbose_stdout;
  146. }
  147. bool OS::is_stdout_debug_enabled() const {
  148. return _debug_stdout;
  149. }
  150. bool OS::is_stdout_enabled() const {
  151. return _stdout_enabled;
  152. }
  153. bool OS::is_stderr_enabled() const {
  154. return _stderr_enabled;
  155. }
  156. void OS::set_stdout_enabled(bool p_enabled) {
  157. _stdout_enabled = p_enabled;
  158. }
  159. void OS::set_stderr_enabled(bool p_enabled) {
  160. _stderr_enabled = p_enabled;
  161. }
  162. int OS::get_exit_code() const {
  163. return _exit_code;
  164. }
  165. void OS::set_exit_code(int p_code) {
  166. _exit_code = p_code;
  167. }
  168. String OS::get_locale() const {
  169. return "en";
  170. }
  171. // Non-virtual helper to extract the 2 or 3-letter language code from
  172. // `get_locale()` in a way that's consistent for all platforms.
  173. String OS::get_locale_language() const {
  174. return get_locale().left(3).replace("_", "");
  175. }
  176. // Embedded PCK offset.
  177. uint64_t OS::get_embedded_pck_offset() const {
  178. return 0;
  179. }
  180. // Helper function to ensure that a dir name/path will be valid on the OS
  181. String OS::get_safe_dir_name(const String &p_dir_name, bool p_allow_paths) const {
  182. String safe_dir_name = p_dir_name;
  183. Vector<String> invalid_chars = String(": * ? \" < > |").split(" ");
  184. if (p_allow_paths) {
  185. // Dir separators are allowed, but disallow ".." to avoid going up the filesystem
  186. invalid_chars.push_back("..");
  187. safe_dir_name = safe_dir_name.replace("\\", "/").strip_edges();
  188. } else {
  189. invalid_chars.push_back("/");
  190. invalid_chars.push_back("\\");
  191. safe_dir_name = safe_dir_name.strip_edges();
  192. // These directory names are invalid.
  193. if (safe_dir_name == ".") {
  194. safe_dir_name = "dot";
  195. } else if (safe_dir_name == "..") {
  196. safe_dir_name = "twodots";
  197. }
  198. }
  199. for (int i = 0; i < invalid_chars.size(); i++) {
  200. safe_dir_name = safe_dir_name.replace(invalid_chars[i], "-");
  201. }
  202. // Trim trailing periods from the returned value as it's not valid for folder names on Windows.
  203. // This check is still applied on non-Windows platforms so the returned value is consistent across platforms.
  204. return safe_dir_name.rstrip(".");
  205. }
  206. // Path to data, config, cache, etc. OS-specific folders
  207. // Get properly capitalized engine name for system paths
  208. String OS::get_godot_dir_name() const {
  209. // Default to lowercase, so only override when different case is needed
  210. return String(VERSION_SHORT_NAME).to_lower();
  211. }
  212. // OS equivalent of XDG_DATA_HOME
  213. String OS::get_data_path() const {
  214. return ".";
  215. }
  216. // OS equivalent of XDG_CONFIG_HOME
  217. String OS::get_config_path() const {
  218. return ".";
  219. }
  220. // OS equivalent of XDG_CACHE_HOME
  221. String OS::get_cache_path() const {
  222. return ".";
  223. }
  224. // Path to macOS .app bundle resources
  225. String OS::get_bundle_resource_dir() const {
  226. return ".";
  227. }
  228. // Path to macOS .app bundle embedded icon
  229. String OS::get_bundle_icon_path() const {
  230. return String();
  231. }
  232. // OS specific path for user://
  233. String OS::get_user_data_dir() const {
  234. return ".";
  235. }
  236. // Absolute path to res://
  237. String OS::get_resource_dir() const {
  238. return ProjectSettings::get_singleton()->get_resource_path();
  239. }
  240. // Access system-specific dirs like Documents, Downloads, etc.
  241. String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
  242. return ".";
  243. }
  244. Error OS::shell_open(const String &p_uri) {
  245. return ERR_UNAVAILABLE;
  246. }
  247. Error OS::shell_show_in_file_manager(String p_path, bool p_open_folder) {
  248. p_path = p_path.trim_prefix("file://");
  249. if (!DirAccess::dir_exists_absolute(p_path)) {
  250. p_path = p_path.get_base_dir();
  251. }
  252. p_path = String("file://") + p_path;
  253. return shell_open(p_path);
  254. }
  255. // implement these with the canvas?
  256. uint64_t OS::get_static_memory_usage() const {
  257. return Memory::get_mem_usage();
  258. }
  259. uint64_t OS::get_static_memory_peak_usage() const {
  260. return Memory::get_mem_max_usage();
  261. }
  262. Error OS::set_cwd(const String &p_cwd) {
  263. return ERR_CANT_OPEN;
  264. }
  265. Dictionary OS::get_memory_info() const {
  266. Dictionary meminfo;
  267. meminfo["physical"] = -1;
  268. meminfo["free"] = -1;
  269. meminfo["available"] = -1;
  270. meminfo["stack"] = -1;
  271. return meminfo;
  272. }
  273. void OS::yield() {
  274. }
  275. void OS::ensure_user_data_dir() {
  276. String dd = get_user_data_dir();
  277. if (DirAccess::exists(dd)) {
  278. return;
  279. }
  280. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  281. Error err = da->make_dir_recursive(dd);
  282. ERR_FAIL_COND_MSG(err != OK, vformat("Error attempting to create data dir: %s.", dd));
  283. }
  284. String OS::get_model_name() const {
  285. return "GenericDevice";
  286. }
  287. void OS::set_cmdline(const char *p_execpath, const List<String> &p_args, const List<String> &p_user_args) {
  288. _execpath = String::utf8(p_execpath);
  289. _cmdline = p_args;
  290. _user_args = p_user_args;
  291. }
  292. String OS::get_unique_id() const {
  293. return "";
  294. }
  295. int OS::get_processor_count() const {
  296. return THREADING_NAMESPACE::thread::hardware_concurrency();
  297. }
  298. String OS::get_processor_name() const {
  299. return "";
  300. }
  301. void OS::set_has_server_feature_callback(HasServerFeatureCallback p_callback) {
  302. has_server_feature_callback = p_callback;
  303. }
  304. bool OS::has_feature(const String &p_feature) {
  305. // Feature tags are always lowercase for consistency.
  306. if (p_feature == get_identifier()) {
  307. return true;
  308. }
  309. if (p_feature == "movie") {
  310. return _writing_movie;
  311. }
  312. #ifdef DEBUG_ENABLED
  313. if (p_feature == "debug") {
  314. return true;
  315. }
  316. #endif // DEBUG_ENABLED
  317. #ifdef TOOLS_ENABLED
  318. if (p_feature == "editor") {
  319. return true;
  320. }
  321. if (p_feature == "editor_hint") {
  322. return _in_editor;
  323. } else if (p_feature == "editor_runtime") {
  324. return !_in_editor;
  325. }
  326. #else
  327. if (p_feature == "template") {
  328. return true;
  329. }
  330. #ifdef DEBUG_ENABLED
  331. if (p_feature == "template_debug") {
  332. return true;
  333. }
  334. #else
  335. if (p_feature == "template_release" || p_feature == "release") {
  336. return true;
  337. }
  338. #endif // DEBUG_ENABLED
  339. #endif // TOOLS_ENABLED
  340. #ifdef REAL_T_IS_DOUBLE
  341. if (p_feature == "double") {
  342. return true;
  343. }
  344. #else
  345. if (p_feature == "single") {
  346. return true;
  347. }
  348. #endif // REAL_T_IS_DOUBLE
  349. if (sizeof(void *) == 8 && p_feature == "64") {
  350. return true;
  351. }
  352. if (sizeof(void *) == 4 && p_feature == "32") {
  353. return true;
  354. }
  355. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(_M_X64)
  356. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  357. if (p_feature == "x86_64") {
  358. return true;
  359. }
  360. #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
  361. if (p_feature == "x86_32") {
  362. return true;
  363. }
  364. #endif
  365. if (p_feature == "x86") {
  366. return true;
  367. }
  368. #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  369. #if defined(__aarch64__) || defined(_M_ARM64)
  370. if (p_feature == "arm64") {
  371. return true;
  372. }
  373. #elif defined(__arm__) || defined(_M_ARM)
  374. if (p_feature == "arm32") {
  375. return true;
  376. }
  377. #endif
  378. #if defined(__ARM_ARCH_7A__)
  379. if (p_feature == "armv7a" || p_feature == "armv7") {
  380. return true;
  381. }
  382. #endif
  383. #if defined(__ARM_ARCH_7S__)
  384. if (p_feature == "armv7s" || p_feature == "armv7") {
  385. return true;
  386. }
  387. #endif
  388. if (p_feature == "arm") {
  389. return true;
  390. }
  391. #elif defined(__riscv)
  392. #if __riscv_xlen == 8
  393. if (p_feature == "rv64") {
  394. return true;
  395. }
  396. #endif
  397. if (p_feature == "riscv") {
  398. return true;
  399. }
  400. #elif defined(__powerpc__)
  401. #if defined(__powerpc64__)
  402. if (p_feature == "ppc64") {
  403. return true;
  404. }
  405. #endif
  406. if (p_feature == "ppc") {
  407. return true;
  408. }
  409. #elif defined(__wasm__)
  410. #if defined(__wasm64__)
  411. if (p_feature == "wasm64") {
  412. return true;
  413. }
  414. #elif defined(__wasm32__)
  415. if (p_feature == "wasm32") {
  416. return true;
  417. }
  418. #endif
  419. if (p_feature == "wasm") {
  420. return true;
  421. }
  422. #endif
  423. #if defined(IOS_SIMULATOR)
  424. if (p_feature == "simulator") {
  425. return true;
  426. }
  427. #endif
  428. #ifdef THREADS_ENABLED
  429. if (p_feature == "threads") {
  430. return true;
  431. }
  432. #else
  433. if (p_feature == "nothreads") {
  434. return true;
  435. }
  436. #endif
  437. if (_check_internal_feature_support(p_feature)) {
  438. return true;
  439. }
  440. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  441. return true;
  442. }
  443. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature)) {
  444. return true;
  445. }
  446. return false;
  447. }
  448. bool OS::is_sandboxed() const {
  449. return false;
  450. }
  451. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  452. restart_on_exit = p_restart;
  453. restart_commandline = p_restart_arguments;
  454. }
  455. bool OS::is_restart_on_exit_set() const {
  456. return restart_on_exit;
  457. }
  458. List<String> OS::get_restart_on_exit_arguments() const {
  459. return restart_commandline;
  460. }
  461. PackedStringArray OS::get_connected_midi_inputs() {
  462. if (MIDIDriver::get_singleton()) {
  463. return MIDIDriver::get_singleton()->get_connected_inputs();
  464. }
  465. PackedStringArray list;
  466. ERR_FAIL_V_MSG(list, vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  467. }
  468. void OS::open_midi_inputs() {
  469. if (MIDIDriver::get_singleton()) {
  470. MIDIDriver::get_singleton()->open();
  471. } else {
  472. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  473. }
  474. }
  475. void OS::close_midi_inputs() {
  476. if (MIDIDriver::get_singleton()) {
  477. MIDIDriver::get_singleton()->close();
  478. } else {
  479. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  480. }
  481. }
  482. void OS::add_frame_delay(bool p_can_draw) {
  483. const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay();
  484. if (frame_delay) {
  485. // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take
  486. // the actual frame time into account.
  487. // Due to the high fluctuation of the actual sleep duration, it's not recommended
  488. // to use this as a FPS limiter.
  489. delay_usec(frame_delay * 1000);
  490. }
  491. // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the
  492. // previous frame time into account for a smoother result.
  493. uint64_t dynamic_delay = 0;
  494. if (is_in_low_processor_usage_mode() || !p_can_draw) {
  495. dynamic_delay = get_low_processor_usage_mode_sleep_usec();
  496. }
  497. const int max_fps = Engine::get_singleton()->get_max_fps();
  498. if (max_fps > 0 && !Engine::get_singleton()->is_editor_hint()) {
  499. // Override the low processor usage mode sleep delay if the target FPS is lower.
  500. dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / max_fps));
  501. }
  502. if (dynamic_delay > 0) {
  503. target_ticks += dynamic_delay;
  504. uint64_t current_ticks = get_ticks_usec();
  505. if (current_ticks < target_ticks) {
  506. delay_usec(target_ticks - current_ticks);
  507. }
  508. current_ticks = get_ticks_usec();
  509. target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay);
  510. }
  511. }
  512. Error OS::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
  513. return default_rfs.synchronize_with_server(p_server_host, p_port, p_password, r_project_path);
  514. }
  515. OS::PreferredTextureFormat OS::get_preferred_texture_format() const {
  516. #if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  517. return PREFERRED_TEXTURE_FORMAT_ETC2_ASTC; // By rule, ARM hardware uses ETC texture compression.
  518. #elif defined(__x86_64__) || defined(_M_X64) || defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
  519. return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; // By rule, X86 hardware prefers S3TC and derivatives.
  520. #else
  521. return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; // Override in platform if needed.
  522. #endif
  523. }
  524. void OS::set_use_benchmark(bool p_use_benchmark) {
  525. use_benchmark = p_use_benchmark;
  526. }
  527. bool OS::is_use_benchmark_set() {
  528. return use_benchmark;
  529. }
  530. void OS::set_benchmark_file(const String &p_benchmark_file) {
  531. benchmark_file = p_benchmark_file;
  532. }
  533. String OS::get_benchmark_file() {
  534. return benchmark_file;
  535. }
  536. void OS::benchmark_begin_measure(const String &p_context, const String &p_what) {
  537. #ifdef TOOLS_ENABLED
  538. Pair<String, String> mark_key(p_context, p_what);
  539. ERR_FAIL_COND_MSG(benchmark_marks_from.has(mark_key), vformat("Benchmark key '%s:%s' already exists.", p_context, p_what));
  540. benchmark_marks_from[mark_key] = OS::get_singleton()->get_ticks_usec();
  541. #endif
  542. }
  543. void OS::benchmark_end_measure(const String &p_context, const String &p_what) {
  544. #ifdef TOOLS_ENABLED
  545. Pair<String, String> mark_key(p_context, p_what);
  546. ERR_FAIL_COND_MSG(!benchmark_marks_from.has(mark_key), vformat("Benchmark key '%s:%s' doesn't exist.", p_context, p_what));
  547. uint64_t total = OS::get_singleton()->get_ticks_usec() - benchmark_marks_from[mark_key];
  548. double total_f = double(total) / double(1000000);
  549. benchmark_marks_final[mark_key] = total_f;
  550. #endif
  551. }
  552. void OS::benchmark_dump() {
  553. #ifdef TOOLS_ENABLED
  554. if (!use_benchmark) {
  555. return;
  556. }
  557. if (!benchmark_file.is_empty()) {
  558. Ref<FileAccess> f = FileAccess::open(benchmark_file, FileAccess::WRITE);
  559. if (f.is_valid()) {
  560. Dictionary benchmark_marks;
  561. for (const KeyValue<Pair<String, String>, double> &E : benchmark_marks_final) {
  562. const String mark_key = vformat("[%s] %s", E.key.first, E.key.second);
  563. benchmark_marks[mark_key] = E.value;
  564. }
  565. Ref<JSON> json;
  566. json.instantiate();
  567. f->store_string(json->stringify(benchmark_marks, "\t", false, true));
  568. }
  569. } else {
  570. HashMap<String, String> results;
  571. for (const KeyValue<Pair<String, String>, double> &E : benchmark_marks_final) {
  572. if (E.key.first == "Startup" && !results.has(E.key.first)) {
  573. results.insert(E.key.first, "", true); // Hack to make sure "Startup" always comes first.
  574. }
  575. results[E.key.first] += vformat("\t\t- %s: %.3f msec.\n", E.key.second, (E.value * 1000));
  576. }
  577. print_line("BENCHMARK:");
  578. for (const KeyValue<String, String> &E : results) {
  579. print_line(vformat("\t[%s]\n%s", E.key, E.value));
  580. }
  581. }
  582. #endif
  583. }
  584. OS::OS() {
  585. singleton = this;
  586. Vector<Logger *> loggers;
  587. loggers.push_back(memnew(StdLogger));
  588. _set_logger(memnew(CompositeLogger(loggers)));
  589. }
  590. OS::~OS() {
  591. if (_logger) {
  592. memdelete(_logger);
  593. }
  594. singleton = nullptr;
  595. }