engine.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*************************************************************************/
  2. /* engine.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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/version.h"
  36. #include "core/version_hash.gen.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_physics_jitter_fix(double p_threshold) {
  45. if (p_threshold < 0) {
  46. p_threshold = 0;
  47. }
  48. physics_jitter_fix = p_threshold;
  49. }
  50. double Engine::get_physics_jitter_fix() const {
  51. return physics_jitter_fix;
  52. }
  53. void Engine::set_target_fps(int p_fps) {
  54. _target_fps = p_fps > 0 ? p_fps : 0;
  55. }
  56. int Engine::get_target_fps() const {
  57. return _target_fps;
  58. }
  59. uint64_t Engine::get_frames_drawn() {
  60. return frames_drawn;
  61. }
  62. void Engine::set_frame_delay(uint32_t p_msec) {
  63. _frame_delay = p_msec;
  64. }
  65. uint32_t Engine::get_frame_delay() const {
  66. return _frame_delay;
  67. }
  68. void Engine::set_time_scale(double p_scale) {
  69. _time_scale = p_scale;
  70. }
  71. double Engine::get_time_scale() const {
  72. return _time_scale;
  73. }
  74. Dictionary Engine::get_version_info() const {
  75. Dictionary dict;
  76. dict["major"] = VERSION_MAJOR;
  77. dict["minor"] = VERSION_MINOR;
  78. dict["patch"] = VERSION_PATCH;
  79. dict["hex"] = VERSION_HEX;
  80. dict["status"] = VERSION_STATUS;
  81. dict["build"] = VERSION_BUILD;
  82. dict["year"] = VERSION_YEAR;
  83. String hash = VERSION_HASH;
  84. dict["hash"] = hash.length() == 0 ? String("unknown") : hash;
  85. String stringver = String(dict["major"]) + "." + String(dict["minor"]);
  86. if ((int)dict["patch"] != 0) {
  87. stringver += "." + String(dict["patch"]);
  88. }
  89. stringver += "-" + String(dict["status"]) + " (" + String(dict["build"]) + ")";
  90. dict["string"] = stringver;
  91. return dict;
  92. }
  93. static Array array_from_info(const char *const *info_list) {
  94. Array arr;
  95. for (int i = 0; info_list[i] != nullptr; i++) {
  96. arr.push_back(String::utf8(info_list[i]));
  97. }
  98. return arr;
  99. }
  100. static Array array_from_info_count(const char *const *info_list, int info_count) {
  101. Array arr;
  102. for (int i = 0; i < info_count; i++) {
  103. arr.push_back(String::utf8(info_list[i]));
  104. }
  105. return arr;
  106. }
  107. Dictionary Engine::get_author_info() const {
  108. Dictionary dict;
  109. dict["lead_developers"] = array_from_info(AUTHORS_LEAD_DEVELOPERS);
  110. dict["project_managers"] = array_from_info(AUTHORS_PROJECT_MANAGERS);
  111. dict["founders"] = array_from_info(AUTHORS_FOUNDERS);
  112. dict["developers"] = array_from_info(AUTHORS_DEVELOPERS);
  113. return dict;
  114. }
  115. Array Engine::get_copyright_info() const {
  116. Array components;
  117. for (int component_index = 0; component_index < COPYRIGHT_INFO_COUNT; component_index++) {
  118. const ComponentCopyright &cp_info = COPYRIGHT_INFO[component_index];
  119. Dictionary component_dict;
  120. component_dict["name"] = String::utf8(cp_info.name);
  121. Array parts;
  122. for (int i = 0; i < cp_info.part_count; i++) {
  123. const ComponentCopyrightPart &cp_part = cp_info.parts[i];
  124. Dictionary part_dict;
  125. part_dict["files"] = array_from_info_count(cp_part.files, cp_part.file_count);
  126. part_dict["copyright"] = array_from_info_count(cp_part.copyright_statements, cp_part.copyright_count);
  127. part_dict["license"] = String::utf8(cp_part.license);
  128. parts.push_back(part_dict);
  129. }
  130. component_dict["parts"] = parts;
  131. components.push_back(component_dict);
  132. }
  133. return components;
  134. }
  135. Dictionary Engine::get_donor_info() const {
  136. Dictionary donors;
  137. donors["platinum_sponsors"] = array_from_info(DONORS_SPONSOR_PLATINUM);
  138. donors["gold_sponsors"] = array_from_info(DONORS_SPONSOR_GOLD);
  139. donors["silver_sponsors"] = array_from_info(DONORS_SPONSOR_SILVER);
  140. donors["bronze_sponsors"] = array_from_info(DONORS_SPONSOR_BRONZE);
  141. donors["mini_sponsors"] = array_from_info(DONORS_SPONSOR_MINI);
  142. donors["gold_donors"] = array_from_info(DONORS_GOLD);
  143. donors["silver_donors"] = array_from_info(DONORS_SILVER);
  144. donors["bronze_donors"] = array_from_info(DONORS_BRONZE);
  145. return donors;
  146. }
  147. Dictionary Engine::get_license_info() const {
  148. Dictionary licenses;
  149. for (int i = 0; i < LICENSE_COUNT; i++) {
  150. licenses[LICENSE_NAMES[i]] = LICENSE_BODIES[i];
  151. }
  152. return licenses;
  153. }
  154. String Engine::get_license_text() const {
  155. return String(GODOT_LICENSE_TEXT);
  156. }
  157. bool Engine::is_abort_on_gpu_errors_enabled() const {
  158. return abort_on_gpu_errors;
  159. }
  160. bool Engine::is_validation_layers_enabled() const {
  161. return use_validation_layers;
  162. }
  163. void Engine::set_print_error_messages(bool p_enabled) {
  164. _print_error_enabled = p_enabled;
  165. }
  166. bool Engine::is_printing_error_messages() const {
  167. return _print_error_enabled;
  168. }
  169. void Engine::add_singleton(const Singleton &p_singleton) {
  170. ERR_FAIL_COND_MSG(singleton_ptrs.has(p_singleton.name), "Can't register singleton that already exists: " + String(p_singleton.name));
  171. singletons.push_back(p_singleton);
  172. singleton_ptrs[p_singleton.name] = p_singleton.ptr;
  173. }
  174. Object *Engine::get_singleton_object(const StringName &p_name) const {
  175. const Map<StringName, Object *>::Element *E = singleton_ptrs.find(p_name);
  176. ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + String(p_name) + "'.");
  177. return E->get();
  178. }
  179. bool Engine::is_singleton_user_created(const StringName &p_name) const {
  180. ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
  181. for (const Singleton &E : singletons) {
  182. if (E.name == p_name && E.user_created) {
  183. return true;
  184. }
  185. }
  186. return false;
  187. }
  188. void Engine::remove_singleton(const StringName &p_name) {
  189. ERR_FAIL_COND(!singleton_ptrs.has(p_name));
  190. for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
  191. if (E->get().name == p_name) {
  192. singletons.erase(E);
  193. singleton_ptrs.erase(p_name);
  194. return;
  195. }
  196. }
  197. }
  198. bool Engine::has_singleton(const StringName &p_name) const {
  199. return singleton_ptrs.has(p_name);
  200. }
  201. void Engine::get_singletons(List<Singleton> *p_singletons) {
  202. for (const Singleton &E : singletons) {
  203. p_singletons->push_back(E);
  204. }
  205. }
  206. void Engine::set_shader_cache_path(const String &p_path) {
  207. shader_cache_path = p_path;
  208. }
  209. String Engine::get_shader_cache_path() const {
  210. return shader_cache_path;
  211. }
  212. Engine *Engine::singleton = nullptr;
  213. Engine *Engine::get_singleton() {
  214. return singleton;
  215. }
  216. Engine::Engine() {
  217. singleton = this;
  218. }
  219. Engine::Singleton::Singleton(const StringName &p_name, Object *p_ptr, const StringName &p_class_name) :
  220. name(p_name),
  221. ptr(p_ptr),
  222. class_name(p_class_name) {
  223. #ifdef DEBUG_ENABLED
  224. RefCounted *rc = Object::cast_to<RefCounted>(p_ptr);
  225. if (rc && !rc->is_referenced()) {
  226. WARN_PRINT("You must use Ref<> to ensure the lifetime of a RefCounted object intended to be used as a singleton.");
  227. }
  228. #endif
  229. }