scene_preloader.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*************************************************************************/
  2. /* scene_preloader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "scene_preloader.h"
  31. #include "globals.h"
  32. bool ScenePreloader::can_instance() const {
  33. return nodes.size() > 0;
  34. }
  35. Node *ScenePreloader::instance() const {
  36. int nc = nodes.size();
  37. ERR_FAIL_COND_V(nc == 0, NULL);
  38. const StringName *snames = NULL;
  39. int sname_count = names.size();
  40. if (sname_count)
  41. snames = &names[0];
  42. const Variant *props = NULL;
  43. int prop_count = variants.size();
  44. if (prop_count)
  45. props = &variants[0];
  46. Vector<Variant> properties;
  47. const NodeData *nd = &nodes[0];
  48. Node **ret_nodes = (Node **)alloca(sizeof(Node *) * nc);
  49. for (int i = 0; i < nc; i++) {
  50. const NodeData &n = nd[i];
  51. if (!ObjectTypeDB::is_type_enabled(snames[n.type])) {
  52. ret_nodes[i] = NULL;
  53. continue;
  54. }
  55. Object *obj = ObjectTypeDB::instance(snames[n.type]);
  56. ERR_FAIL_COND_V(!obj, NULL);
  57. Node *node = obj->cast_to<Node>();
  58. ERR_FAIL_COND_V(!node, NULL);
  59. int nprop_count = n.properties.size();
  60. if (nprop_count) {
  61. const NodeData::Property *nprops = &n.properties[0];
  62. for (int j = 0; j < nprop_count; j++) {
  63. bool valid;
  64. node->set(snames[nprops[j].name], props[nprops[j].value], &valid);
  65. }
  66. }
  67. node->set_name(snames[n.name]);
  68. ret_nodes[i] = node;
  69. if (i > 0) {
  70. ERR_FAIL_INDEX_V(n.parent, i, NULL);
  71. ERR_FAIL_COND_V(!ret_nodes[n.parent], NULL);
  72. ret_nodes[n.parent]->add_child(node);
  73. }
  74. }
  75. //do connections
  76. int cc = connections.size();
  77. const ConnectionData *cdata = connections.ptr();
  78. for (int i = 0; i < cc; i++) {
  79. const ConnectionData &c = cdata[i];
  80. ERR_FAIL_INDEX_V(c.from, nc, NULL);
  81. ERR_FAIL_INDEX_V(c.to, nc, NULL);
  82. Vector<Variant> binds;
  83. if (c.binds.size()) {
  84. binds.resize(c.binds.size());
  85. for (int j = 0; j < c.binds.size(); j++)
  86. binds[j] = props[c.binds[j]];
  87. }
  88. if (!ret_nodes[c.from] || !ret_nodes[c.to])
  89. continue;
  90. ret_nodes[c.from]->connect(snames[c.signal], ret_nodes[c.to], snames[c.method], binds, CONNECT_PERSIST);
  91. }
  92. return ret_nodes[0];
  93. }
  94. static int _nm_get_string(const String &p_string, Map<StringName, int> &name_map) {
  95. if (name_map.has(p_string))
  96. return name_map[p_string];
  97. int idx = name_map.size();
  98. name_map[p_string] = idx;
  99. return idx;
  100. }
  101. static int _vm_get_variant(const Variant &p_variant, HashMap<Variant, int, VariantHasher> &variant_map) {
  102. if (variant_map.has(p_variant))
  103. return variant_map[p_variant];
  104. int idx = variant_map.size();
  105. variant_map[p_variant] = idx;
  106. return idx;
  107. }
  108. void ScenePreloader::_parse_node(Node *p_owner, Node *p_node, int p_parent_idx, Map<StringName, int> &name_map, HashMap<Variant, int, VariantHasher> &variant_map, Map<Node *, int> &node_map) {
  109. if (p_node != p_owner && !p_node->get_owner())
  110. return;
  111. NodeData nd;
  112. nd.name = _nm_get_string(p_node->get_name(), name_map);
  113. nd.type = _nm_get_string(p_node->get_type(), name_map);
  114. nd.parent = p_parent_idx;
  115. List<PropertyInfo> plist;
  116. p_node->get_property_list(&plist);
  117. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  118. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  119. continue;
  120. NodeData::Property prop;
  121. prop.name = _nm_get_string(E->get().name, name_map);
  122. prop.value = _vm_get_variant(p_node->get(E->get().name), variant_map);
  123. nd.properties.push_back(prop);
  124. }
  125. int idx = nodes.size();
  126. node_map[p_node] = idx;
  127. nodes.push_back(nd);
  128. for (int i = 0; i < p_node->get_child_count(); i++) {
  129. Node *c = p_node->get_child(i);
  130. _parse_node(p_owner, c, idx, name_map, variant_map, node_map);
  131. }
  132. }
  133. void ScenePreloader::_parse_connections(Node *p_node, Map<StringName, int> &name_map, HashMap<Variant, int, VariantHasher> &variant_map, Map<Node *, int> &node_map, bool p_instance) {
  134. List<MethodInfo> signals;
  135. p_node->get_signal_list(&signals);
  136. for (List<MethodInfo>::Element *E = signals.front(); E; E = E->next()) {
  137. List<Node::Connection> conns;
  138. p_node->get_signal_connection_list(E->get().name, &conns);
  139. for (List<Node::Connection>::Element *F = conns.front(); F; F = F->next()) {
  140. const Node::Connection &c = F->get();
  141. if (!(c.flags & CONNECT_PERSIST))
  142. continue;
  143. Node *n = c.target->cast_to<Node>();
  144. if (!n)
  145. continue;
  146. if (!node_map.has(n))
  147. continue;
  148. ConnectionData cd;
  149. cd.from = node_map[p_node];
  150. cd.to = node_map[n];
  151. cd.method = _nm_get_string(c.method, name_map);
  152. cd.signal = _nm_get_string(c.signal, name_map);
  153. for (int i = 0; i < c.binds.size(); i++) {
  154. cd.binds.push_back(_vm_get_variant(c.binds[i], variant_map));
  155. }
  156. connections.push_back(cd);
  157. }
  158. }
  159. }
  160. Error ScenePreloader::load_scene(const String &p_path) {
  161. return ERR_CANT_OPEN;
  162. #if 0
  163. if (path==p_path)
  164. return OK;
  165. String p=Globals::get_singleton()->localize_path(p_path);
  166. clear();
  167. Node *scene = SceneLoader::load(p);
  168. ERR_FAIL_COND_V(!scene,ERR_CANT_OPEN);
  169. path=p;
  170. Map<StringName,int> name_map;
  171. HashMap<Variant,int,VariantHasher> variant_map;
  172. Map<Node*,int> node_map;
  173. _parse_node(scene,scene,-1,name_map,variant_map,node_map);
  174. names.resize(name_map.size());
  175. for(Map<StringName,int>::Element *E=name_map.front();E;E=E->next()) {
  176. names[E->get()]=E->key();
  177. }
  178. variants.resize(variant_map.size());
  179. const Variant *K=NULL;
  180. while((K=variant_map.next(K))) {
  181. int idx = variant_map[*K];
  182. variants[idx]=*K;
  183. }
  184. memdelete(scene); // <- me falto esto :(
  185. return OK;
  186. #endif
  187. }
  188. String ScenePreloader::get_scene_path() const {
  189. return path;
  190. }
  191. void ScenePreloader::clear() {
  192. names.clear();
  193. variants.clear();
  194. nodes.clear();
  195. connections.clear();
  196. }
  197. void ScenePreloader::_set_bundled_scene(const Dictionary &d) {
  198. ERR_FAIL_COND(!d.has("names"));
  199. ERR_FAIL_COND(!d.has("variants"));
  200. ERR_FAIL_COND(!d.has("node_count"));
  201. ERR_FAIL_COND(!d.has("nodes"));
  202. ERR_FAIL_COND(!d.has("conn_count"));
  203. ERR_FAIL_COND(!d.has("conns"));
  204. ERR_FAIL_COND(!d.has("path"));
  205. DVector<String> snames = d["names"];
  206. if (snames.size()) {
  207. int namecount = snames.size();
  208. names.resize(namecount);
  209. DVector<String>::Read r = snames.read();
  210. for (int i = 0; i < names.size(); i++)
  211. names[i] = r[i];
  212. }
  213. Array svariants = d["variants"];
  214. if (svariants.size()) {
  215. int varcount = svariants.size();
  216. variants.resize(varcount);
  217. for (int i = 0; i < varcount; i++) {
  218. variants[i] = svariants[i];
  219. }
  220. } else {
  221. variants.clear();
  222. }
  223. nodes.resize(d["node_count"]);
  224. int nc = nodes.size();
  225. if (nc) {
  226. DVector<int> snodes = d["nodes"];
  227. DVector<int>::Read r = snodes.read();
  228. int idx = 0;
  229. for (int i = 0; i < nc; i++) {
  230. NodeData &nd = nodes[i];
  231. nd.parent = r[idx++];
  232. nd.type = r[idx++];
  233. nd.name = r[idx++];
  234. nd.properties.resize(r[idx++]);
  235. for (int j = 0; j < nd.properties.size(); j++) {
  236. nd.properties[j].name = r[idx++];
  237. nd.properties[j].value = r[idx++];
  238. }
  239. }
  240. }
  241. connections.resize(d["conn_count"]);
  242. int cc = connections.size();
  243. if (cc) {
  244. DVector<int> sconns = d["conns"];
  245. DVector<int>::Read r = sconns.read();
  246. int idx = 0;
  247. for (int i = 0; i < nc; i++) {
  248. ConnectionData &cd = connections[nc];
  249. cd.from = r[idx++];
  250. cd.to = r[idx++];
  251. cd.signal = r[idx++];
  252. cd.method = r[idx++];
  253. cd.binds.resize(r[idx++]);
  254. for (int j = 0; j < cd.binds.size(); j++) {
  255. cd.binds[j] = r[idx++];
  256. }
  257. }
  258. }
  259. path = d["path"];
  260. }
  261. Dictionary ScenePreloader::_get_bundled_scene() const {
  262. DVector<String> rnames;
  263. rnames.resize(names.size());
  264. if (names.size()) {
  265. DVector<String>::Write r = rnames.write();
  266. for (int i = 0; i < names.size(); i++)
  267. r[i] = names[i];
  268. }
  269. Dictionary d;
  270. d["names"] = rnames;
  271. d["variants"] = variants;
  272. Vector<int> rnodes;
  273. d["node_count"] = nodes.size();
  274. for (int i = 0; i < nodes.size(); i++) {
  275. const NodeData &nd = nodes[i];
  276. rnodes.push_back(nd.parent);
  277. rnodes.push_back(nd.type);
  278. rnodes.push_back(nd.name);
  279. rnodes.push_back(nd.properties.size());
  280. for (int j = 0; j < nd.properties.size(); j++) {
  281. rnodes.push_back(nd.properties[j].name);
  282. rnodes.push_back(nd.properties[j].value);
  283. }
  284. }
  285. d["nodes"] = rnodes;
  286. Vector<int> rconns;
  287. d["conn_count"] = connections.size();
  288. for (int i = 0; i < connections.size(); i++) {
  289. const ConnectionData &cd = connections[i];
  290. rconns.push_back(cd.from);
  291. rconns.push_back(cd.to);
  292. rconns.push_back(cd.signal);
  293. rconns.push_back(cd.method);
  294. rconns.push_back(cd.binds.size());
  295. for (int j = 0; j < cd.binds.size(); j++)
  296. rconns.push_back(cd.binds[j]);
  297. }
  298. d["conns"] = rconns;
  299. d["path"] = path;
  300. return d;
  301. }
  302. void ScenePreloader::_bind_methods() {
  303. ObjectTypeDB::bind_method(_MD("load_scene", "path"), &ScenePreloader::load_scene);
  304. ObjectTypeDB::bind_method(_MD("get_scene_path"), &ScenePreloader::get_scene_path);
  305. ObjectTypeDB::bind_method(_MD("instance:Node"), &ScenePreloader::instance);
  306. ObjectTypeDB::bind_method(_MD("can_instance"), &ScenePreloader::can_instance);
  307. ObjectTypeDB::bind_method(_MD("_set_bundled_scene"), &ScenePreloader::_set_bundled_scene);
  308. ObjectTypeDB::bind_method(_MD("_get_bundled_scene"), &ScenePreloader::_get_bundled_scene);
  309. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_bundled", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_BUNDLE), _SCS("_set_bundled_scene"), _SCS("_get_bundled_scene"));
  310. #if 0
  311. List<String> extensions;
  312. SceneLoader::get_recognized_extensions(&extensions);
  313. String exthint;
  314. for (List<String>::Element*E=extensions.front();E;E=E->next()) {
  315. if (exthint!="")
  316. exthint+=",";
  317. exthint+="*."+E->get();
  318. }
  319. exthint+="; Scenes";
  320. ADD_PROPERTY( PropertyInfo(Variant::STRING,"scene",PROPERTY_HINT_FILE,exthint),_SCS("load_scene"),_SCS("get_scene_path"));
  321. #endif
  322. }
  323. ScenePreloader::ScenePreloader() {
  324. }