os.cpp 21 KB

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