2
0

engine.cpp 12 KB

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