engine.cpp 11 KB

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