engine.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. void Engine::set_physics_ticks_per_second(int p_ips) {
  38. ERR_FAIL_COND_MSG(p_ips <= 0, "Engine iterations per second must be greater than 0.");
  39. ips = p_ips;
  40. }
  41. int Engine::get_physics_ticks_per_second() const {
  42. return ips;
  43. }
  44. void Engine::set_max_physics_steps_per_frame(int p_max_physics_steps) {
  45. ERR_FAIL_COND_MSG(p_max_physics_steps <= 0, "Maximum number of physics steps per frame must be greater than 0.");
  46. max_physics_steps_per_frame = p_max_physics_steps;
  47. }
  48. int Engine::get_max_physics_steps_per_frame() const {
  49. return max_physics_steps_per_frame;
  50. }
  51. void Engine::set_physics_jitter_fix(double p_threshold) {
  52. if (p_threshold < 0) {
  53. p_threshold = 0;
  54. }
  55. physics_jitter_fix = p_threshold;
  56. }
  57. double Engine::get_physics_jitter_fix() const {
  58. return physics_jitter_fix;
  59. }
  60. void Engine::set_max_fps(int p_fps) {
  61. _max_fps = p_fps > 0 ? p_fps : 0;
  62. }
  63. int Engine::get_max_fps() const {
  64. return _max_fps;
  65. }
  66. uint64_t Engine::get_frames_drawn() {
  67. return frames_drawn;
  68. }
  69. void Engine::set_frame_delay(uint32_t p_msec) {
  70. _frame_delay = p_msec;
  71. }
  72. uint32_t Engine::get_frame_delay() const {
  73. return _frame_delay;
  74. }
  75. void Engine::set_time_scale(double p_scale) {
  76. _time_scale = p_scale;
  77. }
  78. double Engine::get_time_scale() const {
  79. return _time_scale;
  80. }
  81. Dictionary Engine::get_version_info() const {
  82. Dictionary dict;
  83. dict["major"] = VERSION_MAJOR;
  84. dict["minor"] = VERSION_MINOR;
  85. dict["patch"] = VERSION_PATCH;
  86. dict["hex"] = VERSION_HEX;
  87. dict["status"] = VERSION_STATUS;
  88. dict["build"] = VERSION_BUILD;
  89. dict["year"] = VERSION_YEAR;
  90. String hash = String(VERSION_HASH);
  91. dict["hash"] = hash.is_empty() ? String("unknown") : hash;
  92. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  93. if ((int)dict["patch"] != 0) {
  94. stringver += "." + String(dict["patch"]);
  95. }
  96. stringver += "-" + String(dict["status"]) + " (" + String(dict["build"]) + ")";
  97. dict["string"] = stringver;
  98. return dict;
  99. }
  100. static Array array_from_info(const char *const *info_list) {
  101. Array arr;
  102. for (int i = 0; info_list[i] != nullptr; i++) {
  103. arr.push_back(String::utf8(info_list[i]));
  104. }
  105. return arr;
  106. }
  107. static Array array_from_info_count(const char *const *info_list, int info_count) {
  108. Array arr;
  109. for (int i = 0; i < info_count; i++) {
  110. arr.push_back(String::utf8(info_list[i]));
  111. }
  112. return arr;
  113. }
  114. Dictionary Engine::get_author_info() const {
  115. Dictionary dict;
  116. dict["lead_developers"] = array_from_info(AUTHORS_LEAD_DEVELOPERS);
  117. dict["project_managers"] = array_from_info(AUTHORS_PROJECT_MANAGERS);
  118. dict["founders"] = array_from_info(AUTHORS_FOUNDERS);
  119. dict["developers"] = array_from_info(AUTHORS_DEVELOPERS);
  120. return dict;
  121. }
  122. TypedArray<Dictionary> Engine::get_copyright_info() const {
  123. TypedArray<Dictionary> components;
  124. for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) {
  125. const ComponentCopyright &cp_info = COPYRIGHT_INFO[component_index];
  126. Dictionary component_dict;
  127. component_dict["name"] = String::utf8(cp_info.name);
  128. Array parts;
  129. for (int i = 0; i < cp_info.part_count; i++) {
  130. const ComponentCopyrightPart &cp_part = cp_info.parts[i];
  131. Dictionary part_dict;
  132. part_dict["files"] = array_from_info_count(cp_part.files, cp_part.file_count);
  133. part_dict["copyright"] = array_from_info_count(cp_part.copyright_statements, cp_part.copyright_count);
  134. part_dict["license"] = String::utf8(cp_part.license);
  135. parts.push_back(part_dict);
  136. }
  137. component_dict["parts"] = parts;
  138. components.push_back(component_dict);
  139. }
  140. return components;
  141. }
  142. Dictionary Engine::get_donor_info() const {
  143. Dictionary donors;
  144. donors["platinum_sponsors"] = array_from_info(DONORS_SPONSOR_PLATINUM);
  145. donors["gold_sponsors"] = array_from_info(DONORS_SPONSOR_GOLD);
  146. donors["silver_sponsors"] = array_from_info(DONORS_SPONSOR_SILVER);
  147. donors["bronze_sponsors"] = array_from_info(DONORS_SPONSOR_BRONZE);
  148. donors["mini_sponsors"] = array_from_info(DONORS_SPONSOR_MINI);
  149. donors["gold_donors"] = array_from_info(DONORS_GOLD);
  150. donors["silver_donors"] = array_from_info(DONORS_SILVER);
  151. donors["bronze_donors"] = array_from_info(DONORS_BRONZE);
  152. return donors;
  153. }
  154. Dictionary Engine::get_license_info() const {
  155. Dictionary licenses;
  156. for (int i = 0; i < LICENSE_COUNT; i++) {
  157. licenses[LICENSE_NAMES[i]] = LICENSE_BODIES[i];
  158. }
  159. return licenses;
  160. }
  161. String Engine::get_license_text() const {
  162. return String(GODOT_LICENSE_TEXT);
  163. }
  164. String Engine::get_architecture_name() const {
  165. #if defined(__x86_64) || defined(__x86_64__) || defined(__amd64__) || defined(_M_X64)
  166. return "x86_64";
  167. #elif defined(__i386) || defined(__i386__) || defined(_M_IX86)
  168. return "x86_32";
  169. #elif defined(__aarch64__) || defined(_M_ARM64) || defined(_M_ARM64EC)
  170. return "arm64";
  171. #elif defined(__arm__) || defined(_M_ARM)
  172. return "arm32";
  173. #elif defined(__riscv)
  174. #if __riscv_xlen == 8
  175. return "rv64";
  176. #else
  177. return "riscv";
  178. #endif
  179. #elif defined(__powerpc__)
  180. #if defined(__powerpc64__)
  181. return "ppc64";
  182. #else
  183. return "ppc";
  184. #endif
  185. #elif defined(__wasm__)
  186. #if defined(__wasm64__)
  187. return "wasm64";
  188. #elif defined(__wasm32__)
  189. return "wasm32";
  190. #endif
  191. #endif
  192. }
  193. bool Engine::is_abort_on_gpu_errors_enabled() const {
  194. return abort_on_gpu_errors;
  195. }
  196. int32_t Engine::get_gpu_index() const {
  197. return gpu_idx;
  198. }
  199. bool Engine::is_validation_layers_enabled() const {
  200. return use_validation_layers;
  201. }
  202. void Engine::set_print_error_messages(bool p_enabled) {
  203. CoreGlobals::print_error_enabled = p_enabled;
  204. }
  205. bool Engine::is_printing_error_messages() const {
  206. return CoreGlobals::print_error_enabled;
  207. }
  208. void Engine::add_singleton(const Singleton &p_singleton) {
  209. ERR_FAIL_COND_MSG(singleton_ptrs.has(p_singleton.name), "Can't register singleton that already exists: " + String(p_singleton.name));
  210. singletons.push_back(p_singleton);
  211. singleton_ptrs[p_singleton.name] = p_singleton.ptr;
  212. }
  213. Object *Engine::get_singleton_object(const StringName &p_name) const {
  214. HashMap<StringName, Object *>::ConstIterator E = singleton_ptrs.find(p_name);
  215. ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + String(p_name) + "'.");
  216. return E->value;
  217. }
  218. bool Engine::is_singleton_user_created(const StringName &p_name) const {
  219. ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
  220. for (const Singleton &E : singletons) {
  221. if (E.name == p_name && E.user_created) {
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. void Engine::remove_singleton(const StringName &p_name) {
  228. ERR_FAIL_COND(!singleton_ptrs.has(p_name));
  229. for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  230. if (E->get().name == p_name) {
  231. singletons.erase(E);
  232. singleton_ptrs.erase(p_name);
  233. return;
  234. }
  235. }
  236. }
  237. bool Engine::has_singleton(const StringName &p_name) const {
  238. return singleton_ptrs.has(p_name);
  239. }
  240. void Engine::get_singletons(List<Singleton> *p_singletons) {
  241. for (const Singleton &E : singletons) {
  242. p_singletons->push_back(E);
  243. }
  244. }
  245. String Engine::get_write_movie_path() const {
  246. return write_movie_path;
  247. }
  248. void Engine::set_write_movie_path(const String &p_path) {
  249. write_movie_path = p_path;
  250. }
  251. void Engine::set_shader_cache_path(const String &p_path) {
  252. shader_cache_path = p_path;
  253. }
  254. String Engine::get_shader_cache_path() const {
  255. return shader_cache_path;
  256. }
  257. Engine *Engine::singleton = nullptr;
  258. Engine *Engine::get_singleton() {
  259. return singleton;
  260. }
  261. Engine::Engine() {
  262. singleton = this;
  263. }
  264. Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) :
  265. name(p_name),
  266. ptr(p_ptr),
  267. class_name(p_class_name) {
  268. #ifdef DEBUG_ENABLED
  269. RefCounted *rc = Object::cast_to<RefCounted>(p_ptr);
  270. if (rc && !rc->is_referenced()) {
  271. WARN_PRINT("You must use Ref<> to ensure the lifetime of a RefCounted object intended to be used as a singleton.");
  272. }
  273. #endif
  274. }