engine.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**************************************************************************/
  2. /* engine.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 "engine.h"
  31. #include "core/authors.gen.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/donors.gen.h"
  34. #include "core/license.gen.h"
  35. #include "core/variant/typed_array.h"
  36. #include "core/version.h"
  37. #include "servers/rendering/rendering_device.h"
  38. void Engine::_update_time_scale() {
  39. _time_scale = _user_time_scale * _game_time_scale;
  40. user_ips = MAX(1, ips * _user_time_scale);
  41. max_user_physics_steps_per_frame = MAX(max_physics_steps_per_frame, max_physics_steps_per_frame * _user_time_scale);
  42. }
  43. void Engine::set_physics_ticks_per_second(int p_ips) {
  44. ERR_FAIL_COND_MSG(p_ips <= 0, "Engine iterations per second must be greater than 0.");
  45. ips = p_ips;
  46. _update_time_scale();
  47. }
  48. int Engine::get_physics_ticks_per_second() const {
  49. return ips;
  50. }
  51. int Engine::get_user_physics_ticks_per_second() const {
  52. return user_ips;
  53. }
  54. void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) {
  55. ERR_FAIL_COND_MSG(p_max_physics_steps <= 0, "Maximum number of physics steps per frame must be greater than 0.");
  56. max_physics_steps_per_frame = p_max_physics_steps;
  57. _update_time_scale();
  58. }
  59. int Engine::get_max_physics_steps_per_frame() const {
  60. return max_physics_steps_per_frame;
  61. }
  62. int Engine::get_user_max_physics_steps_per_frame() const {
  63. return max_user_physics_steps_per_frame;
  64. }
  65. void Engine::set_physics_jitter_fix(double p_threshold) {
  66. if (p_threshold < 0) {
  67. p_threshold = 0;
  68. }
  69. physics_jitter_fix = p_threshold;
  70. }
  71. double Engine::get_physics_jitter_fix() const {
  72. return physics_jitter_fix;
  73. }
  74. void Engine::set_max_fps(int p_fps) {
  75. _max_fps = p_fps > 0 ? p_fps : 0;
  76. RenderingDevice *rd = RenderingDevice::get_singleton();
  77. if (rd) {
  78. rd->_set_max_fps(_max_fps);
  79. }
  80. }
  81. int Engine::get_max_fps() const {
  82. return _max_fps;
  83. }
  84. void Engine::set_audio_output_latency(int p_msec) {
  85. _audio_output_latency = p_msec > 1 ? p_msec : 1;
  86. }
  87. int Engine::get_audio_output_latency() const {
  88. return _audio_output_latency;
  89. }
  90. void Engine::increment_frames_drawn() {
  91. if (frame_server_synced) {
  92. server_syncs++;
  93. } else {
  94. server_syncs = 0;
  95. }
  96. frame_server_synced = false;
  97. frames_drawn++;
  98. }
  99. uint64_t Engine::get_frames_drawn() {
  100. return frames_drawn;
  101. }
  102. void Engine::set_frame_delay(uint32_t p_msec) {
  103. _frame_delay = p_msec;
  104. }
  105. uint32_t Engine::get_frame_delay() const {
  106. return _frame_delay;
  107. }
  108. void Engine::set_time_scale(double p_scale) {
  109. _game_time_scale = p_scale;
  110. _update_time_scale();
  111. }
  112. double Engine::get_time_scale() const {
  113. return freeze_time_scale ? 0.0 : _game_time_scale;
  114. }
  115. void Engine::set_user_time_scale(double p_scale) {
  116. _user_time_scale = p_scale;
  117. _update_time_scale();
  118. }
  119. double Engine::get_effective_time_scale() const {
  120. return freeze_time_scale ? 0.0 : _time_scale;
  121. }
  122. double Engine::get_unfrozen_time_scale() const {
  123. return _time_scale;
  124. }
  125. Dictionary Engine::get_version_info() const {
  126. Dictionary dict;
  127. dict["major"] = GODOT_VERSION_MAJOR;
  128. dict["minor"] = GODOT_VERSION_MINOR;
  129. dict["patch"] = GODOT_VERSION_PATCH;
  130. dict["hex"] = GODOT_VERSION_HEX;
  131. dict["status"] = GODOT_VERSION_STATUS;
  132. dict["build"] = GODOT_VERSION_BUILD;
  133. String hash = String(GODOT_VERSION_HASH);
  134. dict["hash"] = hash.is_empty() ? String("unknown") : hash;
  135. dict["timestamp"] = GODOT_VERSION_TIMESTAMP;
  136. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  137. if ((int)dict["patch"] != 0) {
  138. stringver += "." + String(dict["patch"]);
  139. }
  140. stringver += "-" + String(dict["status"]) + " (" + String(dict["build"]) + ")";
  141. dict["string"] = stringver;
  142. return dict;
  143. }
  144. static Array array_from_info(const char *const *info_list) {
  145. Array arr;
  146. for (int i = 0; info_list[i] != nullptr; i++) {
  147. arr.push_back(String::utf8(info_list[i]));
  148. }
  149. return arr;
  150. }
  151. static Array array_from_info_count(const char *const *info_list, int info_count) {
  152. Array arr;
  153. for (int i = 0; i < info_count; i++) {
  154. arr.push_back(String::utf8(info_list[i]));
  155. }
  156. return arr;
  157. }
  158. Dictionary Engine::get_author_info() const {
  159. Dictionary dict;
  160. dict["lead_developers"] = array_from_info(AUTHORS_LEAD_DEVELOPERS);
  161. dict["project_managers"] = array_from_info(AUTHORS_PROJECT_MANAGERS);
  162. dict["founders"] = array_from_info(AUTHORS_FOUNDERS);
  163. dict["developers"] = array_from_info(AUTHORS_DEVELOPERS);
  164. return dict;
  165. }
  166. TypedArray<Dictionary> Engine::get_copyright_info() const {
  167. TypedArray<Dictionary> components;
  168. for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) {
  169. const ComponentCopyright &cp_info = COPYRIGHT_INFO[component_index];
  170. Dictionary component_dict;
  171. component_dict["name"] = String::utf8(cp_info.name);
  172. Array parts;
  173. for (int i = 0; i < cp_info.part_count; i++) {
  174. const ComponentCopyrightPart &cp_part = cp_info.parts[i];
  175. Dictionary part_dict;
  176. part_dict["files"] = array_from_info_count(cp_part.files, cp_part.file_count);
  177. part_dict["copyright"] = array_from_info_count(cp_part.copyright_statements, cp_part.copyright_count);
  178. part_dict["license"] = String::utf8(cp_part.license);
  179. parts.push_back(part_dict);
  180. }
  181. component_dict["parts"] = parts;
  182. components.push_back(component_dict);
  183. }
  184. return components;
  185. }
  186. Dictionary Engine::get_donor_info() const {
  187. Dictionary donors;
  188. donors["patrons"] = array_from_info(DONORS_PATRONS);
  189. donors["platinum_sponsors"] = array_from_info(DONORS_SPONSORS_PLATINUM);
  190. donors["gold_sponsors"] = array_from_info(DONORS_SPONSORS_GOLD);
  191. donors["silver_sponsors"] = array_from_info(DONORS_SPONSORS_SILVER);
  192. donors["diamond_members"] = array_from_info(DONORS_MEMBERS_DIAMOND);
  193. donors["titanium_members"] = array_from_info(DONORS_MEMBERS_TITANIUM);
  194. donors["platinum_members"] = array_from_info(DONORS_MEMBERS_PLATINUM);
  195. donors["gold_members"] = array_from_info(DONORS_MEMBERS_GOLD);
  196. return donors;
  197. }
  198. Dictionary Engine::get_license_info() const {
  199. Dictionary licenses;
  200. for (int i = 0; i < LICENSE_COUNT; i++) {
  201. licenses[LICENSE_NAMES[i]] = LICENSE_BODIES[i];
  202. }
  203. return licenses;
  204. }
  205. String Engine::get_license_text() const {
  206. return String(GODOT_LICENSE_TEXT);
  207. }
  208. String Engine::get_architecture_name() const {
  209. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  210. return "x86_64";
  211. #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
  212. return "x86_32";
  213. #elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
  214. return "arm64";
  215. #elif defined(__arm__) || defined(_M_ARM)
  216. return "arm32";
  217. #elif defined(__riscv)
  218. return "rv64";
  219. #elif defined(__powerpc64__)
  220. return "ppc64";
  221. #elif defined(__loongarch64)
  222. return "loongarch64";
  223. #elif defined(__wasm64__)
  224. return "wasm64";
  225. #elif defined(__wasm32__)
  226. return "wasm32";
  227. #endif
  228. }
  229. bool Engine::is_abort_on_gpu_errors_enabled() const {
  230. return abort_on_gpu_errors;
  231. }
  232. int32_t Engine::get_gpu_index() const {
  233. return gpu_idx;
  234. }
  235. bool Engine::is_validation_layers_enabled() const {
  236. return use_validation_layers;
  237. }
  238. bool Engine::is_generate_spirv_debug_info_enabled() const {
  239. return generate_spirv_debug_info;
  240. }
  241. bool Engine::is_extra_gpu_memory_tracking_enabled() const {
  242. return extra_gpu_memory_tracking;
  243. }
  244. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  245. bool Engine::is_accurate_breadcrumbs_enabled() const {
  246. return accurate_breadcrumbs;
  247. }
  248. #endif
  249. void Engine::set_print_to_stdout(bool p_enabled) {
  250. CoreGlobals::print_line_enabled = p_enabled;
  251. }
  252. bool Engine::is_printing_to_stdout() const {
  253. return CoreGlobals::print_line_enabled;
  254. }
  255. void Engine::set_print_error_messages(bool p_enabled) {
  256. CoreGlobals::print_error_enabled = p_enabled;
  257. }
  258. bool Engine::is_printing_error_messages() const {
  259. return CoreGlobals::print_error_enabled;
  260. }
  261. void Engine::print_header(const String &p_string) const {
  262. if (_print_header) {
  263. print_line(p_string);
  264. }
  265. }
  266. void Engine::print_header_rich(const String &p_string) const {
  267. if (_print_header) {
  268. print_line_rich(p_string);
  269. }
  270. }
  271. void Engine::add_singleton(const Singleton &p_singleton) {
  272. ERR_FAIL_COND_MSG(singleton_ptrs.has(p_singleton.name), vformat("Can't register singleton '%s' because it already exists.", p_singleton.name));
  273. singletons.push_back(p_singleton);
  274. singleton_ptrs[p_singleton.name] = p_singleton.ptr;
  275. }
  276. Object *Engine::get_singleton_object(const StringName &p_name) const {
  277. HashMap<StringName, Object *>::ConstIterator E = singleton_ptrs.find(p_name);
  278. ERR_FAIL_COND_V_MSG(!E, nullptr, vformat("Failed to retrieve non-existent singleton '%s'.", p_name));
  279. #ifdef TOOLS_ENABLED
  280. if (!is_editor_hint() && is_singleton_editor_only(p_name)) {
  281. ERR_FAIL_V_MSG(nullptr, vformat("Can't retrieve singleton '%s' outside of editor.", p_name));
  282. }
  283. #endif
  284. return E->value;
  285. }
  286. bool Engine::is_singleton_user_created(const StringName &p_name) const {
  287. ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
  288. for (const Singleton &E : singletons) {
  289. if (E.name == p_name && E.user_created) {
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. bool Engine::is_singleton_editor_only(const StringName &p_name) const {
  296. ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
  297. for (const Singleton &E : singletons) {
  298. if (E.name == p_name && E.editor_only) {
  299. return true;
  300. }
  301. }
  302. return false;
  303. }
  304. void Engine::remove_singleton(const StringName &p_name) {
  305. ERR_FAIL_COND(!singleton_ptrs.has(p_name));
  306. for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  307. if (E->get().name == p_name) {
  308. singletons.erase(E);
  309. singleton_ptrs.erase(p_name);
  310. return;
  311. }
  312. }
  313. }
  314. bool Engine::has_singleton(const StringName &p_name) const {
  315. return singleton_ptrs.has(p_name);
  316. }
  317. void Engine::get_singletons(List<Singleton> *p_singletons) {
  318. for (const Singleton &E : singletons) {
  319. #ifdef TOOLS_ENABLED
  320. if (!is_editor_hint() && E.editor_only) {
  321. continue;
  322. }
  323. #endif
  324. p_singletons->push_back(E);
  325. }
  326. }
  327. String Engine::get_write_movie_path() const {
  328. return write_movie_path;
  329. }
  330. void Engine::set_write_movie_path(const String &p_path) {
  331. write_movie_path = p_path;
  332. }
  333. void Engine::set_shader_cache_path(const String &p_path) {
  334. shader_cache_path = p_path;
  335. }
  336. String Engine::get_shader_cache_path() const {
  337. return shader_cache_path;
  338. }
  339. Engine *Engine::get_singleton() {
  340. return singleton;
  341. }
  342. bool Engine::notify_frame_server_synced() {
  343. frame_server_synced = true;
  344. return server_syncs > SERVER_SYNC_FRAME_COUNT_WARNING;
  345. }
  346. void Engine::set_freeze_time_scale(bool p_frozen) {
  347. freeze_time_scale = p_frozen;
  348. }
  349. void Engine::set_embedded_in_editor(bool p_enabled) {
  350. embedded_in_editor = p_enabled;
  351. }
  352. bool Engine::is_embedded_in_editor() const {
  353. return embedded_in_editor;
  354. }
  355. Engine::Engine() {
  356. singleton = this;
  357. }
  358. Engine::~Engine() {
  359. if (singleton == this) {
  360. singleton = nullptr;
  361. }
  362. }
  363. Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) :
  364. name(p_name),
  365. ptr(p_ptr),
  366. class_name(p_class_name) {
  367. #ifdef DEBUG_ENABLED
  368. RefCounted *rc = Object::cast_to<RefCounted>(p_ptr);
  369. if (rc && !rc->is_referenced()) {
  370. WARN_PRINT("You must use Ref<> to ensure the lifetime of a RefCounted object intended to be used as a singleton.");
  371. }
  372. #endif
  373. }