engine.cpp 11 KB

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