engine.cpp 12 KB

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