os.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 defined(MACOS_ENABLED)
  358. if (p_feature == "universal") {
  359. return true;
  360. }
  361. #endif
  362. if (p_feature == "x86_64") {
  363. return true;
  364. }
  365. #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
  366. if (p_feature == "x86_32") {
  367. return true;
  368. }
  369. #endif
  370. if (p_feature == "x86") {
  371. return true;
  372. }
  373. #elif defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  374. #if defined(__aarch64__) || defined(_M_ARM64)
  375. #if defined(MACOS_ENABLED)
  376. if (p_feature == "universal") {
  377. return true;
  378. }
  379. #endif
  380. if (p_feature == "arm64") {
  381. return true;
  382. }
  383. #elif defined(__arm__) || defined(_M_ARM)
  384. if (p_feature == "arm32") {
  385. return true;
  386. }
  387. #endif
  388. #if defined(__ARM_ARCH_7A__)
  389. if (p_feature == "armv7a" || p_feature == "armv7") {
  390. return true;
  391. }
  392. #endif
  393. #if defined(__ARM_ARCH_7S__)
  394. if (p_feature == "armv7s" || p_feature == "armv7") {
  395. return true;
  396. }
  397. #endif
  398. if (p_feature == "arm") {
  399. return true;
  400. }
  401. #elif defined(__riscv)
  402. #if __riscv_xlen == 8
  403. if (p_feature == "rv64") {
  404. return true;
  405. }
  406. #endif
  407. if (p_feature == "riscv") {
  408. return true;
  409. }
  410. #elif defined(__powerpc__)
  411. #if defined(__powerpc64__)
  412. if (p_feature == "ppc64") {
  413. return true;
  414. }
  415. #endif
  416. if (p_feature == "ppc") {
  417. return true;
  418. }
  419. #elif defined(__wasm__)
  420. #if defined(__wasm64__)
  421. if (p_feature == "wasm64") {
  422. return true;
  423. }
  424. #elif defined(__wasm32__)
  425. if (p_feature == "wasm32") {
  426. return true;
  427. }
  428. #endif
  429. if (p_feature == "wasm") {
  430. return true;
  431. }
  432. #endif
  433. #if defined(IOS_SIMULATOR)
  434. if (p_feature == "simulator") {
  435. return true;
  436. }
  437. #endif
  438. #ifdef THREADS_ENABLED
  439. if (p_feature == "threads") {
  440. return true;
  441. }
  442. #else
  443. if (p_feature == "nothreads") {
  444. return true;
  445. }
  446. #endif
  447. if (_check_internal_feature_support(p_feature)) {
  448. return true;
  449. }
  450. if (has_server_feature_callback && has_server_feature_callback(p_feature)) {
  451. return true;
  452. }
  453. if (ProjectSettings::get_singleton()->has_custom_feature(p_feature)) {
  454. return true;
  455. }
  456. return false;
  457. }
  458. bool OS::is_sandboxed() const {
  459. return false;
  460. }
  461. void OS::set_restart_on_exit(bool p_restart, const List<String> &p_restart_arguments) {
  462. restart_on_exit = p_restart;
  463. restart_commandline = p_restart_arguments;
  464. }
  465. bool OS::is_restart_on_exit_set() const {
  466. return restart_on_exit;
  467. }
  468. List<String> OS::get_restart_on_exit_arguments() const {
  469. return restart_commandline;
  470. }
  471. PackedStringArray OS::get_connected_midi_inputs() {
  472. if (MIDIDriver::get_singleton()) {
  473. return MIDIDriver::get_singleton()->get_connected_inputs();
  474. }
  475. PackedStringArray list;
  476. ERR_FAIL_V_MSG(list, vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  477. }
  478. void OS::open_midi_inputs() {
  479. if (MIDIDriver::get_singleton()) {
  480. MIDIDriver::get_singleton()->open();
  481. } else {
  482. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  483. }
  484. }
  485. void OS::close_midi_inputs() {
  486. if (MIDIDriver::get_singleton()) {
  487. MIDIDriver::get_singleton()->close();
  488. } else {
  489. ERR_PRINT(vformat("MIDI input isn't supported on %s.", OS::get_singleton()->get_name()));
  490. }
  491. }
  492. void OS::add_frame_delay(bool p_can_draw) {
  493. const uint32_t frame_delay = Engine::get_singleton()->get_frame_delay();
  494. if (frame_delay) {
  495. // Add fixed frame delay to decrease CPU/GPU usage. This doesn't take
  496. // the actual frame time into account.
  497. // Due to the high fluctuation of the actual sleep duration, it's not recommended
  498. // to use this as a FPS limiter.
  499. delay_usec(frame_delay * 1000);
  500. }
  501. // Add a dynamic frame delay to decrease CPU/GPU usage. This takes the
  502. // previous frame time into account for a smoother result.
  503. uint64_t dynamic_delay = 0;
  504. if (is_in_low_processor_usage_mode() || !p_can_draw) {
  505. dynamic_delay = get_low_processor_usage_mode_sleep_usec();
  506. }
  507. const int max_fps = Engine::get_singleton()->get_max_fps();
  508. if (max_fps > 0 && !Engine::get_singleton()->is_editor_hint()) {
  509. // Override the low processor usage mode sleep delay if the target FPS is lower.
  510. dynamic_delay = MAX(dynamic_delay, (uint64_t)(1000000 / max_fps));
  511. }
  512. if (dynamic_delay > 0) {
  513. target_ticks += dynamic_delay;
  514. uint64_t current_ticks = get_ticks_usec();
  515. if (current_ticks < target_ticks) {
  516. delay_usec(target_ticks - current_ticks);
  517. }
  518. current_ticks = get_ticks_usec();
  519. target_ticks = MIN(MAX(target_ticks, current_ticks - dynamic_delay), current_ticks + dynamic_delay);
  520. }
  521. }
  522. Error OS::setup_remote_filesystem(const String &p_server_host, int p_port, const String &p_password, String &r_project_path) {
  523. return default_rfs.synchronize_with_server(p_server_host, p_port, p_password, r_project_path);
  524. }
  525. OS::PreferredTextureFormat OS::get_preferred_texture_format() const {
  526. #if defined(__arm__) || defined(__aarch64__) || defined(_M_ARM) || defined(_M_ARM64)
  527. return PREFERRED_TEXTURE_FORMAT_ETC2_ASTC; // By rule, ARM hardware uses ETC texture compression.
  528. #elif defined(__x86_64__) || defined(_M_X64) || defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
  529. return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; // By rule, X86 hardware prefers S3TC and derivatives.
  530. #else
  531. return PREFERRED_TEXTURE_FORMAT_S3TC_BPTC; // Override in platform if needed.
  532. #endif
  533. }
  534. void OS::set_use_benchmark(bool p_use_benchmark) {
  535. use_benchmark = p_use_benchmark;
  536. }
  537. bool OS::is_use_benchmark_set() {
  538. return use_benchmark;
  539. }
  540. void OS::set_benchmark_file(const String &p_benchmark_file) {
  541. benchmark_file = p_benchmark_file;
  542. }
  543. String OS::get_benchmark_file() {
  544. return benchmark_file;
  545. }
  546. void OS::benchmark_begin_measure(const String &p_context, const String &p_what) {
  547. #ifdef TOOLS_ENABLED
  548. Pair<String, String> mark_key(p_context, p_what);
  549. ERR_FAIL_COND_MSG(benchmark_marks_from.has(mark_key), vformat("Benchmark key '%s:%s' already exists.", p_context, p_what));
  550. benchmark_marks_from[mark_key] = OS::get_singleton()->get_ticks_usec();
  551. #endif
  552. }
  553. void OS::benchmark_end_measure(const String &p_context, const String &p_what) {
  554. #ifdef TOOLS_ENABLED
  555. Pair<String, String> mark_key(p_context, p_what);
  556. ERR_FAIL_COND_MSG(!benchmark_marks_from.has(mark_key), vformat("Benchmark key '%s:%s' doesn't exist.", p_context, p_what));
  557. uint64_t total = OS::get_singleton()->get_ticks_usec() - benchmark_marks_from[mark_key];
  558. double total_f = double(total) / double(1000000);
  559. benchmark_marks_final[mark_key] = total_f;
  560. #endif
  561. }
  562. void OS::benchmark_dump() {
  563. #ifdef TOOLS_ENABLED
  564. if (!use_benchmark) {
  565. return;
  566. }
  567. if (!benchmark_file.is_empty()) {
  568. Ref<FileAccess> f = FileAccess::open(benchmark_file, FileAccess::WRITE);
  569. if (f.is_valid()) {
  570. Dictionary benchmark_marks;
  571. for (const KeyValue<Pair<String, String>, double> &E : benchmark_marks_final) {
  572. const String mark_key = vformat("[%s] %s", E.key.first, E.key.second);
  573. benchmark_marks[mark_key] = E.value;
  574. }
  575. Ref<JSON> json;
  576. json.instantiate();
  577. f->store_string(json->stringify(benchmark_marks, "\t", false, true));
  578. }
  579. } else {
  580. HashMap<String, String> results;
  581. for (const KeyValue<Pair<String, String>, double> &E : benchmark_marks_final) {
  582. if (E.key.first == "Startup" && !results.has(E.key.first)) {
  583. results.insert(E.key.first, "", true); // Hack to make sure "Startup" always comes first.
  584. }
  585. results[E.key.first] += vformat("\t\t- %s: %.3f msec.\n", E.key.second, (E.value * 1000));
  586. }
  587. print_line("BENCHMARK:");
  588. for (const KeyValue<String, String> &E : results) {
  589. print_line(vformat("\t[%s]\n%s", E.key, E.value));
  590. }
  591. }
  592. #endif
  593. }
  594. OS::OS() {
  595. singleton = this;
  596. Vector<Logger *> loggers;
  597. loggers.push_back(memnew(StdLogger));
  598. _set_logger(memnew(CompositeLogger(loggers)));
  599. }
  600. OS::~OS() {
  601. if (_logger) {
  602. memdelete(_logger);
  603. }
  604. singleton = nullptr;
  605. }