engine.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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(__wasm__)
  209. #if defined(__wasm64__)
  210. return "wasm64";
  211. #elif defined(__wasm32__)
  212. return "wasm32";
  213. #endif
  214. #endif
  215. }
  216. bool Engine::is_abort_on_gpu_errors_enabled() const {
  217. return abort_on_gpu_errors;
  218. }
  219. int32_t Engine::get_gpu_index() const {
  220. return gpu_idx;
  221. }
  222. bool Engine::is_validation_layers_enabled() const {
  223. return use_validation_layers;
  224. }
  225. bool Engine::is_generate_spirv_debug_info_enabled() const {
  226. return generate_spirv_debug_info;
  227. }
  228. bool Engine::is_extra_gpu_memory_tracking_enabled() const {
  229. return extra_gpu_memory_tracking;
  230. }
  231. #if defined(DEBUG_ENABLED) || defined(DEV_ENABLED)
  232. bool Engine::is_accurate_breadcrumbs_enabled() const {
  233. return accurate_breadcrumbs;
  234. }
  235. #endif
  236. void Engine::set_print_to_stdout(bool p_enabled) {
  237. CoreGlobals::print_line_enabled = p_enabled;
  238. }
  239. bool Engine::is_printing_to_stdout() const {
  240. return CoreGlobals::print_line_enabled;
  241. }
  242. void Engine::set_print_error_messages(bool p_enabled) {
  243. CoreGlobals::print_error_enabled = p_enabled;
  244. }
  245. bool Engine::is_printing_error_messages() const {
  246. return CoreGlobals::print_error_enabled;
  247. }
  248. void Engine::print_header(const String &p_string) const {
  249. if (_print_header) {
  250. print_line(p_string);
  251. }
  252. }
  253. void Engine::print_header_rich(const String &p_string) const {
  254. if (_print_header) {
  255. print_line_rich(p_string);
  256. }
  257. }
  258. void Engine::add_singleton(const Singleton &p_singleton) {
  259. ERR_FAIL_COND_MSG(singleton_ptrs.has(p_singleton.name), vformat("Can't register singleton '%s' because it already exists.", p_singleton.name));
  260. singletons.push_back(p_singleton);
  261. singleton_ptrs[p_singleton.name] = p_singleton.ptr;
  262. }
  263. Object *Engine::get_singleton_object(const StringName &p_name) const {
  264. HashMap<StringName, Object *>::ConstIterator E = singleton_ptrs.find(p_name);
  265. ERR_FAIL_COND_V_MSG(!E, nullptr, vformat("Failed to retrieve non-existent singleton '%s'.", p_name));
  266. #ifdef TOOLS_ENABLED
  267. if (!is_editor_hint() && is_singleton_editor_only(p_name)) {
  268. ERR_FAIL_V_MSG(nullptr, vformat("Can't retrieve singleton '%s' outside of editor.", p_name));
  269. }
  270. #endif
  271. return E->value;
  272. }
  273. bool Engine::is_singleton_user_created(const StringName &p_name) const {
  274. ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
  275. for (const Singleton &E : singletons) {
  276. if (E.name == p_name && E.user_created) {
  277. return true;
  278. }
  279. }
  280. return false;
  281. }
  282. bool Engine::is_singleton_editor_only(const StringName &p_name) const {
  283. ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
  284. for (const Singleton &E : singletons) {
  285. if (E.name == p_name && E.editor_only) {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. void Engine::remove_singleton(const StringName &p_name) {
  292. ERR_FAIL_COND(!singleton_ptrs.has(p_name));
  293. for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  294. if (E->get().name == p_name) {
  295. singletons.erase(E);
  296. singleton_ptrs.erase(p_name);
  297. return;
  298. }
  299. }
  300. }
  301. bool Engine::has_singleton(const StringName &p_name) const {
  302. return singleton_ptrs.has(p_name);
  303. }
  304. void Engine::get_singletons(List<Singleton> *p_singletons) {
  305. for (const Singleton &E : singletons) {
  306. #ifdef TOOLS_ENABLED
  307. if (!is_editor_hint() && E.editor_only) {
  308. continue;
  309. }
  310. #endif
  311. p_singletons->push_back(E);
  312. }
  313. }
  314. String Engine::get_write_movie_path() const {
  315. return write_movie_path;
  316. }
  317. void Engine::set_write_movie_path(const String &p_path) {
  318. write_movie_path = p_path;
  319. }
  320. void Engine::set_shader_cache_path(const String &p_path) {
  321. shader_cache_path = p_path;
  322. }
  323. String Engine::get_shader_cache_path() const {
  324. return shader_cache_path;
  325. }
  326. Engine *Engine::singleton = nullptr;
  327. Engine *Engine::get_singleton() {
  328. return singleton;
  329. }
  330. bool Engine::notify_frame_server_synced() {
  331. frame_server_synced = true;
  332. return server_syncs > SERVER_SYNC_FRAME_COUNT_WARNING;
  333. }
  334. void Engine::set_freeze_time_scale(bool p_frozen) {
  335. freeze_time_scale = p_frozen;
  336. }
  337. Engine::Engine() {
  338. singleton = this;
  339. }
  340. Engine::~Engine() {
  341. if (singleton == this) {
  342. singleton = nullptr;
  343. }
  344. }
  345. Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) :
  346. name(p_name),
  347. ptr(p_ptr),
  348. class_name(p_class_name) {
  349. #ifdef DEBUG_ENABLED
  350. RefCounted *rc = Object::cast_to<RefCounted>(p_ptr);
  351. if (rc && !rc->is_referenced()) {
  352. WARN_PRINT("You must use Ref<> to ensure the lifetime of a RefCounted object intended to be used as a singleton.");
  353. }
  354. #endif
  355. }