scene_format_object.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*************************************************************************/
  2. /* scene_format_object.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "scene_format_object.h"
  30. #include "print_string.h"
  31. #include "globals.h"
  32. #include "scene/resources/packed_scene.h"
  33. #include "io/resource_loader.h"
  34. #ifdef OLD_SCENE_FORMAT_ENABLED
  35. void SceneFormatSaverObject::save_node(const Node* p_root,const Node* p_node,const Node* p_owner,ObjectFormatSaver *p_saver,String p_base_path,uint32_t p_flags,Map<const Node*,uint32_t>& owner_id) const {
  36. if (p_node!=p_root && p_node->get_owner()==NULL)
  37. return;
  38. if (p_flags&SceneSaver::FLAG_BUNDLE_INSTANCED_SCENES || p_node->get_owner() == p_owner || p_node == p_owner ) {
  39. Dictionary d;
  40. if (p_root!=p_node) {
  41. d["path"]=p_root->get_path_to(p_node->get_parent());
  42. }
  43. d["name"]=p_node->get_name();
  44. /* Connections */
  45. List<MethodInfo> signal_list;
  46. p_node->get_signal_list(&signal_list);
  47. int conn_count=0;
  48. Set<Node::Connection> exclude_connections;
  49. if (!(p_flags&SceneSaver::FLAG_BUNDLE_INSTANCED_SCENES)) {
  50. Vector<Node::Connection> ex = p_node->get_instance_connections();
  51. for(int i=0;i<ex.size();i++) {
  52. exclude_connections.insert(ex[i]);
  53. }
  54. }
  55. for (List<MethodInfo>::Element *S=signal_list.front();S;S=S->next()) {
  56. List<Node::Connection> connections;
  57. p_node->get_signal_connection_list(S->get().name,&connections);
  58. for(List<Node::Connection>::Element *E=connections.front();E;E=E->next()) {
  59. Node::Connection &c=E->get();
  60. if (!(c.flags&Object::CONNECT_PERSIST))
  61. continue;
  62. if (exclude_connections.has(c))
  63. continue;
  64. Node *target = c.target->cast_to<Node>();
  65. if (!target)
  66. continue; //connected to something not a node, ignoring
  67. Dictionary cd;
  68. cd["signal"]=c.signal;
  69. cd["target"]=p_node->get_path_to(target);
  70. cd["method"]=c.method;
  71. cd["realtime"]=!(c.flags&Object::CONNECT_DEFERRED);
  72. if (c.binds.size())
  73. cd["binds"]=c.binds;
  74. d["connection/"+itos(conn_count+1)]=cd;
  75. conn_count++;
  76. }
  77. }
  78. d["connection_count"]=conn_count;
  79. if (owner_id.has(p_node->get_owner())) {
  80. d["owner"]=owner_id[p_node->get_owner()];
  81. }
  82. /* Groups */
  83. DVector<String> group_array;
  84. List<Node::GroupInfo> groups;
  85. p_node->get_groups(&groups);
  86. Set<StringName> exclude_groups;
  87. if (!(p_flags&SceneSaver::FLAG_BUNDLE_INSTANCED_SCENES)) {
  88. //generate groups to exclude (came from instance)
  89. Vector<StringName> eg;
  90. eg=p_node->get_instance_groups();
  91. for(int i=0;i<eg.size();i++)
  92. exclude_groups.insert(eg[i]);
  93. }
  94. for(List<Node::GroupInfo>::Element*E=groups.front();E;E=E->next()) {
  95. if (E->get().persistent && !exclude_groups.has(E->get().name))
  96. group_array.push_back(E->get().name);
  97. }
  98. if (group_array.size())
  99. d["groups"]=group_array;
  100. /* Save */
  101. if (p_owner!=p_node && p_node->get_filename()!="") {
  102. String instance_path;
  103. if (p_flags&SceneSaver::FLAG_RELATIVE_PATHS)
  104. instance_path=p_base_path.path_to_file(Globals::get_singleton()->localize_path(p_node->get_filename()));
  105. else
  106. instance_path=p_node->get_filename();
  107. d["instance"]=instance_path;
  108. if (p_flags&SceneSaver::FLAG_BUNDLE_INSTANCED_SCENES) {
  109. int id = owner_id.size();
  110. d["owner_id"]=id;
  111. owner_id[p_node]=id;
  112. p_saver->save(p_node,d);
  113. //owner change!
  114. for (int i=0;i<p_node->get_child_count();i++) {
  115. save_node(p_root,p_node->get_child(i),p_node,p_saver,p_base_path,p_flags,owner_id);
  116. }
  117. return;
  118. } else {
  119. DVector<String> prop_names;
  120. Array prop_values;
  121. List<PropertyInfo> properties;
  122. p_node->get_property_list(&properties);
  123. //instance state makes sure that only changes to instance are saved
  124. Dictionary instance_state=p_node->get_instance_state();
  125. for(List<PropertyInfo>::Element *E=properties.front();E;E=E->next()) {
  126. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  127. continue;
  128. String name=E->get().name;
  129. Variant value=p_node->get(E->get().name);
  130. if (!instance_state.has(name))
  131. continue; // did not change since it was loaded, not save
  132. if (value==instance_state[name])
  133. continue;
  134. prop_names.push_back( name );
  135. prop_values.push_back( value );
  136. }
  137. d["override_names"]=prop_names;
  138. d["override_values"]=prop_values;
  139. p_saver->save(NULL,d);
  140. }
  141. } else {
  142. p_saver->save(p_node,d);
  143. }
  144. }
  145. for (int i=0;i<p_node->get_child_count();i++) {
  146. save_node(p_root,p_node->get_child(i),p_owner,p_saver,p_base_path,p_flags,owner_id);
  147. }
  148. }
  149. Error SceneFormatSaverObject::save(const String &p_path,const Node* p_scene,uint32_t p_flags,const Ref<OptimizedSaver> &p_optimizer) {
  150. String extension=p_path.extension();
  151. if (extension=="scn")
  152. extension="bin";
  153. if (extension=="xscn")
  154. extension="xml";
  155. String local_path=Globals::get_singleton()->localize_path(p_path);
  156. uint32_t saver_flags=0;
  157. if (p_flags&SceneSaver::FLAG_RELATIVE_PATHS)
  158. saver_flags|=ObjectSaver::FLAG_RELATIVE_PATHS;
  159. if (p_flags&SceneSaver::FLAG_BUNDLE_RESOURCES)
  160. saver_flags|=ObjectSaver::FLAG_BUNDLE_RESOURCES;
  161. if (p_flags&SceneSaver::FLAG_OMIT_EDITOR_PROPERTIES)
  162. saver_flags|=ObjectSaver::FLAG_OMIT_EDITOR_PROPERTIES;
  163. if (p_flags&SceneSaver::FLAG_SAVE_BIG_ENDIAN)
  164. saver_flags|=ObjectSaver::FLAG_SAVE_BIG_ENDIAN;
  165. ObjectFormatSaver *saver = ObjectSaver::instance_format_saver(local_path,"SCENE",extension,saver_flags,p_optimizer);
  166. ERR_FAIL_COND_V(!saver,ERR_FILE_UNRECOGNIZED);
  167. /* SAVE SCENE */
  168. Map<const Node*,uint32_t> node_id_map;
  169. save_node(p_scene,p_scene,p_scene,saver,local_path,p_flags,node_id_map);
  170. memdelete(saver);
  171. return OK;
  172. }
  173. void SceneFormatSaverObject::get_recognized_extensions(List<String> *p_extensions) const {
  174. p_extensions->push_back("xml");
  175. p_extensions->push_back("scn");
  176. p_extensions->push_back("xscn");
  177. // ObjectSaver::get_recognized_extensions(p_extensions);
  178. }
  179. /////////////////////////////////////////////////////////////////
  180. /////////////////////////////////////////////////////////////////
  181. /////////////////////////////////////////////////////////////////
  182. void SceneFormatLoaderObject::_apply_meta(Node *node, const Variant&meta, ObjectFormatLoader *p_loader,List<ConnectionItem>& connections,Error& r_err,Map<uint32_t,Node*>& owner_map) {
  183. r_err = OK;
  184. Dictionary d=meta;
  185. if (!d.has("name")) {
  186. r_err=ERR_WTF;
  187. memdelete(node);
  188. ERR_FAIL_COND(!d.has("name"));
  189. }
  190. node->set_name(d["name"]);
  191. int connection_count=d.has("connection_count")?d["connection_count"].operator int():0;
  192. for (int i=0;i<connection_count;i++) {
  193. Dictionary cd=d["connection/"+itos(i+1)];
  194. ERR_CONTINUE(!cd.has("target"));
  195. ERR_CONTINUE(!cd.has("method"));
  196. ERR_CONTINUE(!cd.has("realtime"));
  197. ERR_CONTINUE(!cd.has("signal"));
  198. ConnectionItem ci;
  199. ci.node=node;
  200. ci.target=cd["target"];
  201. ci.method=cd["method"];
  202. ci.signal=cd["signal"];
  203. ci.realtime=cd["realtime"];
  204. if (cd.has("binds"))
  205. ci.binds=cd["binds"];
  206. connections.push_back(ci);
  207. }
  208. DVector<String> groups=d.has("groups")?d["groups"].operator DVector<String>():DVector<String>();
  209. for (int i=0;i<groups.size();i++) {
  210. node->add_to_group(groups[i],true);
  211. }
  212. }
  213. Ref<SceneInteractiveLoader> SceneFormatLoaderObject::load_interactive(const String &p_path,bool p_save_root_state) {
  214. SceneInteractiveLoaderObject *sil = memnew( SceneInteractiveLoaderObject(p_path,p_save_root_state) );
  215. if (sil->error!=OK) {
  216. memdelete( sil );
  217. return Ref<SceneInteractiveLoader>();
  218. }
  219. return Ref<SceneInteractiveLoader>( sil );
  220. }
  221. Node* SceneFormatLoaderObject::load_node(Object *obj, const Variant& meta, Node *p_root, ObjectFormatLoader *p_loader,List<ConnectionItem>& connections,Error& r_err,bool p_save_instance_state,Map<uint32_t,Node*>& owner_map) {
  222. r_err = OK;
  223. Node *node=obj->cast_to<Node>();
  224. _apply_meta(node,meta,p_loader,connections,r_err,owner_map);
  225. if (r_err!=OK)
  226. return NULL;
  227. Dictionary d=meta;
  228. if (p_root) {
  229. NodePath path=d.has("path")?d["path"].operator NodePath():NodePath(".");
  230. Node *parent=p_root->get_node(path);
  231. if (!parent) {
  232. memdelete(node);
  233. r_err=ERR_FILE_CORRUPT;
  234. ERR_FAIL_COND_V(!parent,NULL);
  235. }
  236. parent->add_child(node);
  237. if (d.has("owner_id")) {
  238. //is owner
  239. owner_map[d["owner_id"]]=node;
  240. if (d.has("instance"))
  241. node->set_filename(d["instance"]);
  242. }
  243. if (d.has("owner")) {
  244. uint32_t owner = d["owner"];
  245. ERR_FAIL_COND_V(!owner_map.has(owner),NULL);
  246. node->set_owner(owner_map[owner]);
  247. } else {
  248. node->set_owner(p_root);
  249. }
  250. }
  251. return node;
  252. }
  253. void SceneFormatLoaderObject::_apply_connections(List<ConnectionItem>& connections) {
  254. int idx=0;
  255. for (List<ConnectionItem>::Element *E=connections.front();E;E=E->next()) {
  256. ConnectionItem &ci=E->get();
  257. Node *target = ci.node->get_node(ci.target);
  258. ERR_CONTINUE(!target);
  259. ci.node->connect(ci.signal,target,ci.method,ci.binds,(ci.realtime?0:Object::CONNECT_DEFERRED)|Object::CONNECT_PERSIST);
  260. idx++;
  261. }
  262. }
  263. Node* SceneFormatLoaderObject::load(const String &p_path,bool p_save_instance_state) {
  264. List<ConnectionItem> connections;
  265. String extension=p_path.extension();
  266. if (extension=="scn")
  267. extension="bin";
  268. if (extension=="xscn")
  269. extension="xml";
  270. String local_path = Globals::get_singleton()->localize_path(p_path);
  271. ObjectFormatLoader *loader = ObjectLoader::instance_format_loader(local_path,"SCENE",extension);
  272. ERR_EXPLAIN("Couldn't load scene: "+p_path);
  273. ERR_FAIL_COND_V(!loader,NULL);
  274. Node *root=NULL;
  275. Map<uint32_t,Node*> owner_map;
  276. while(true) {
  277. Object *obj=NULL;
  278. Variant metav;
  279. Error r_err=loader->load(&obj,metav);
  280. if (r_err == ERR_SKIP) {
  281. continue;
  282. };
  283. if (r_err==ERR_FILE_EOF) {
  284. memdelete(loader);
  285. ERR_FAIL_COND_V(!root,NULL);
  286. _apply_connections(connections);
  287. return root;
  288. }
  289. if (r_err || (!obj && metav.get_type()==Variant::NIL)) {
  290. memdelete(loader);
  291. ERR_EXPLAIN("Object Loader Failed for Scene: "+p_path) ;
  292. ERR_FAIL_COND_V( r_err, NULL);
  293. ERR_EXPLAIN("Object Loader Failed for Scene: "+p_path) ;
  294. ERR_FAIL_COND_V( !obj && metav.get_type()==Variant::NIL,NULL);
  295. }
  296. if (obj) {
  297. if (obj->cast_to<Node>()) {
  298. Error err;
  299. Node* node = load_node(obj, metav, root, loader,connections,err,p_save_instance_state,owner_map);
  300. if (err)
  301. memdelete(loader);
  302. ERR_FAIL_COND_V( err, NULL );
  303. if (!root)
  304. root=node;
  305. } else {
  306. memdelete(loader);
  307. ERR_FAIL_V( NULL );
  308. }
  309. } else {
  310. // check for instance
  311. Dictionary meta=metav;
  312. if (meta.has("instance")) {
  313. if (!root) {
  314. memdelete(loader);
  315. ERR_FAIL_COND_V(!root,NULL);
  316. }
  317. String path = meta["instance"];
  318. if (path.find("://")==-1 && path.is_rel_path()) {
  319. // path is relative to file being loaded, so convert to a resource path
  320. path=Globals::get_singleton()->localize_path(
  321. local_path.get_base_dir()+"/"+path);
  322. }
  323. Node *scene = SceneLoader::load(path);
  324. if (!scene) {
  325. Ref<PackedScene> sd = ResourceLoader::load(path);
  326. if (sd.is_valid()) {
  327. scene=sd->instance();
  328. }
  329. }
  330. if (!scene) {
  331. memdelete(loader);
  332. ERR_FAIL_COND_V(!scene,NULL);
  333. }
  334. if (p_save_instance_state)
  335. scene->generate_instance_state();
  336. Error err;
  337. _apply_meta(scene,metav,loader,connections,err,owner_map);
  338. if (err!=OK) {
  339. memdelete(loader);
  340. ERR_FAIL_COND_V(err!=OK,NULL);
  341. }
  342. Node *parent=root;
  343. if (meta.has("path"))
  344. parent=root->get_node(meta["path"]);
  345. if (!parent) {
  346. memdelete(loader);
  347. ERR_FAIL_COND_V(!parent,NULL);
  348. }
  349. if (meta.has("override_names") && meta.has("override_values")) {
  350. DVector<String> override_names=meta["override_names"];
  351. Array override_values=meta["override_values"];
  352. int len = override_names.size();
  353. if ( len > 0 && len == override_values.size() ) {
  354. DVector<String>::Read names = override_names.read();
  355. for(int i=0;i<len;i++) {
  356. scene->set(names[i],override_values[i]);
  357. }
  358. }
  359. }
  360. scene->set_filename(path);
  361. parent->add_child(scene);
  362. scene->set_owner(root);
  363. }
  364. }
  365. }
  366. return NULL;
  367. }
  368. void SceneFormatLoaderObject::get_recognized_extensions(List<String> *p_extensions) const {
  369. p_extensions->push_back("xml");
  370. p_extensions->push_back("scn");
  371. p_extensions->push_back("xscn");
  372. // ObjectLoader::get_recognized_extensions(p_extensions);
  373. }
  374. ///////////////////////////////////////////////////
  375. void SceneInteractiveLoaderObject::_apply_meta(Node *node, const Variant&meta, ObjectFormatLoader *p_loader,List<ConnectionItem>& connections,Error& r_err,Map<uint32_t,Node*>& owner_map) {
  376. r_err = OK;
  377. Dictionary d=meta;
  378. if (!d.has("name")) {
  379. r_err=ERR_WTF;
  380. memdelete(node);
  381. ERR_FAIL_COND(!d.has("name"));
  382. }
  383. node->set_name(d["name"]);
  384. int connection_count=d.has("connection_count")?d["connection_count"].operator int():0;
  385. for (int i=0;i<connection_count;i++) {
  386. Dictionary cd=d["connection/"+itos(i+1)];
  387. ERR_CONTINUE(!cd.has("target"));
  388. ERR_CONTINUE(!cd.has("method"));
  389. ERR_CONTINUE(!cd.has("realtime"));
  390. ERR_CONTINUE(!cd.has("signal"));
  391. ConnectionItem ci;
  392. ci.node=node;
  393. ci.target=cd["target"];
  394. ci.method=cd["method"];
  395. ci.signal=cd["signal"];
  396. ci.realtime=cd["realtime"];
  397. if (cd.has("binds"))
  398. ci.binds=cd["binds"];
  399. connections.push_back(ci);
  400. }
  401. DVector<String> groups=d.has("groups")?d["groups"].operator DVector<String>():DVector<String>();
  402. for (int i=0;i<groups.size();i++) {
  403. node->add_to_group(groups[i],true);
  404. }
  405. }
  406. Node* SceneInteractiveLoaderObject::load_node(Object *obj, const Variant& meta, Node *p_root, ObjectFormatLoader *p_loader,List<ConnectionItem>& connections,Error& r_err,bool p_save_instance_state,Map<uint32_t,Node*>& owner_map) {
  407. r_err = OK;
  408. Node *node=obj->cast_to<Node>();
  409. _apply_meta(node,meta,p_loader,connections,r_err,owner_map);
  410. if (r_err!=OK)
  411. return NULL;
  412. Dictionary d=meta;
  413. if (p_root) {
  414. NodePath path=d.has("path")?d["path"].operator NodePath():NodePath(".");
  415. Node *parent=p_root->get_node(path);
  416. if (!parent) {
  417. memdelete(node);
  418. r_err=ERR_FILE_CORRUPT;
  419. ERR_FAIL_COND_V(!parent,NULL);
  420. }
  421. parent->add_child(node);
  422. if (d.has("owner_id")) {
  423. //is owner
  424. owner_map[d["owner_id"]]=node;
  425. if (d.has("instance"))
  426. node->set_filename(d["instance"]);
  427. }
  428. if (d.has("owner")) {
  429. uint32_t owner = d["owner"];
  430. ERR_FAIL_COND_V(!owner_map.has(owner),NULL);
  431. node->set_owner(owner_map[owner]);
  432. } else {
  433. node->set_owner(p_root);
  434. }
  435. }
  436. return node;
  437. }
  438. void SceneInteractiveLoaderObject::_apply_connections(List<ConnectionItem>& connections) {
  439. int idx=0;
  440. for (List<ConnectionItem>::Element *E=connections.front();E;E=E->next()) {
  441. ConnectionItem &ci=E->get();
  442. Node *target = ci.node->get_node(ci.target);
  443. ERR_CONTINUE(!target);
  444. ci.node->connect(ci.signal,target,ci.method,ci.binds,(ci.realtime?0:Object::CONNECT_DEFERRED)|Object::CONNECT_PERSIST);
  445. idx++;
  446. }
  447. }
  448. SceneInteractiveLoaderObject::SceneInteractiveLoaderObject(const String &p_path,bool p_save_root_state) {
  449. error=OK;
  450. path=p_path;
  451. save_instance_state=p_save_root_state;
  452. node_path=p_path;
  453. root=NULL;
  454. stage_max=1;
  455. stage=0;
  456. String extension=p_path.extension();
  457. if (extension=="scn")
  458. extension="bin";
  459. if (extension=="xscn")
  460. extension="xml";
  461. local_path = Globals::get_singleton()->localize_path(p_path);
  462. loader = ObjectLoader::instance_format_loader(local_path,"SCENE",extension);
  463. if (!loader) {
  464. error=ERR_CANT_OPEN;
  465. }
  466. ERR_EXPLAIN("Couldn't load scene: "+p_path);
  467. ERR_FAIL_COND(!loader);
  468. }
  469. void SceneInteractiveLoaderObject::set_local_path(const String& p_local_path) {
  470. node_path=p_local_path;
  471. }
  472. Node *SceneInteractiveLoaderObject::get_scene() {
  473. if (error==ERR_FILE_EOF)
  474. return root;
  475. return NULL;
  476. }
  477. Error SceneInteractiveLoaderObject::poll() {
  478. if (error!=OK)
  479. return error;
  480. Object *obj=NULL;
  481. Variant metav;
  482. Error r_err=loader->load(&obj,metav);
  483. if (r_err == ERR_SKIP) {
  484. stage++;
  485. return OK;
  486. };
  487. if (r_err==ERR_FILE_EOF) {
  488. memdelete(loader);
  489. error=ERR_FILE_CORRUPT;
  490. ERR_FAIL_COND_V(!root,ERR_FILE_CORRUPT);
  491. _apply_connections(connections);
  492. error=ERR_FILE_EOF;
  493. if (root)
  494. root->set_filename(node_path);
  495. return error;
  496. }
  497. if (r_err || (!obj && metav.get_type()==Variant::NIL)) {
  498. memdelete(loader);
  499. error=ERR_FILE_CORRUPT;
  500. ERR_EXPLAIN("Object Loader Failed for Scene: "+path);
  501. ERR_FAIL_COND_V( r_err, ERR_FILE_CORRUPT);
  502. ERR_EXPLAIN("Object Loader Failed for Scene: "+path);
  503. ERR_FAIL_COND_V( !obj && metav.get_type()==Variant::NIL,ERR_FILE_CORRUPT);
  504. }
  505. if (obj) {
  506. if (obj->cast_to<Node>()) {
  507. Error err;
  508. Node* node = load_node(obj, metav, root, loader,connections,err,save_instance_state,owner_map);
  509. if (err) {
  510. error=ERR_FILE_CORRUPT;
  511. memdelete(loader);
  512. }
  513. ERR_FAIL_COND_V( err, ERR_FILE_CORRUPT );
  514. if (!root)
  515. root=node;
  516. } else {
  517. error=ERR_FILE_CORRUPT;
  518. memdelete(loader);
  519. ERR_EXPLAIN("Loaded something not a node.. (?)");
  520. ERR_FAIL_V( ERR_FILE_CORRUPT );
  521. }
  522. } else {
  523. // check for instance
  524. Dictionary meta=metav;
  525. if (meta.has("instance")) {
  526. if (!root) {
  527. error=ERR_FILE_CORRUPT;
  528. memdelete(loader);
  529. ERR_FAIL_COND_V(!root,ERR_FILE_CORRUPT);
  530. }
  531. String path = meta["instance"];
  532. if (path.find("://")==-1 && path.is_rel_path()) {
  533. // path is relative to file being loaded, so convert to a resource path
  534. path=Globals::get_singleton()->localize_path(
  535. local_path.get_base_dir()+"/"+path);
  536. }
  537. Node *scene = SceneLoader::load(path);
  538. if (!scene) {
  539. error=ERR_FILE_CORRUPT;
  540. memdelete(loader);
  541. ERR_FAIL_COND_V(!scene,ERR_FILE_CORRUPT);
  542. }
  543. if (save_instance_state)
  544. scene->generate_instance_state();
  545. Error err;
  546. _apply_meta(scene,metav,loader,connections,err,owner_map);
  547. if (err!=OK) {
  548. error=ERR_FILE_CORRUPT;
  549. memdelete(loader);
  550. ERR_FAIL_COND_V(err!=OK,ERR_FILE_CORRUPT);
  551. }
  552. Node *parent=root;
  553. if (meta.has("path"))
  554. parent=root->get_node(meta["path"]);
  555. if (!parent) {
  556. error=ERR_FILE_CORRUPT;
  557. memdelete(loader);
  558. ERR_FAIL_COND_V(!parent,ERR_FILE_CORRUPT);
  559. }
  560. if (meta.has("override_names") && meta.has("override_values")) {
  561. DVector<String> override_names=meta["override_names"];
  562. Array override_values=meta["override_values"];
  563. int len = override_names.size();
  564. if ( len > 0 && len == override_values.size() ) {
  565. DVector<String>::Read names = override_names.read();
  566. for(int i=0;i<len;i++) {
  567. scene->set(names[i],override_values[i]);
  568. }
  569. }
  570. }
  571. scene->set_filename(path);
  572. parent->add_child(scene);
  573. scene->set_owner(root);
  574. }
  575. }
  576. stage++;
  577. error=OK;
  578. return error;
  579. }
  580. int SceneInteractiveLoaderObject::get_stage() const {
  581. return stage;
  582. }
  583. int SceneInteractiveLoaderObject::get_stage_count() const {
  584. return stage_max;
  585. }
  586. #endif