void ResourceFormatSaverTextInstance::_find_resources(const Variant& p_variant,bool p_main) { switch(p_variant.get_type()) { case Variant::OBJECT: { RES res = p_variant.operator RefPtr(); if (res.is_null() || external_resources.has(res)) return; if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) { int index = external_resources.size(); external_resources[res]=index; return; } if (resource_set.has(res)) return; List<PropertyInfo> property_list; res->get_property_list( &property_list ); property_list.sort(); List<PropertyInfo>::Element *I=property_list.front(); while(I) { PropertyInfo pi=I->get(); if (pi.usage&PROPERTY_USAGE_STORAGE || (bundle_resources && pi.usage&PROPERTY_USAGE_BUNDLE)) { Variant v=res->get(I->get().name); _find_resources(v); } I=I->next(); } resource_set.insert( res ); //saved after, so the childs it needs are available when loaded saved_resources.push_back(res); } break; case Variant::ARRAY: { Array varray=p_variant; int len=varray.size(); for(int i=0;i<len;i++) { Variant v=varray.get(i); _find_resources(v); } } break; case Variant::DICTIONARY: { Dictionary d=p_variant; List<Variant> keys; d.get_key_list(&keys); for(List<Variant>::Element *E=keys.front();E;E=E->next()) { Variant v = d[E->get()]; _find_resources(v); } } break; default: {} } }
Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) { if (p_path.ends_with(".tscn")) { packed_scene=p_resource; } Error err; f = FileAccess::open(p_path, FileAccess::WRITE,&err); ERR_FAIL_COND_V( err, ERR_CANT_OPEN ); FileAccessRef _fref(f); local_path = Globals::get_singleton()->localize_path(p_path); relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS; skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES; bundle_resources=p_flags&ResourceSaver::FLAG_BUNDLE_RESOURCES; takeover_paths=p_flags&ResourceSaver::FLAG_REPLACE_SUBRESOURCE_PATHS; if (!p_path.begins_with("res://")) { takeover_paths=false; } // save resources _find_resources(p_resource,true); if (packed_scene.is_valid()) { //add instances to external resources if saving a packed scene for(int i=0;i<packed_scene->get_state()->get_node_count();i++) { if (packed_scene->get_state()->is_node_instance_placeholder(i)) continue; Ref<PackedScene> instance=packed_scene->get_state()->get_node_instance(i); if (instance.is_valid() && !external_resources.has(instance)) { int index = external_resources.size(); external_resources[instance]=index; } } } ERR_FAIL_COND_V(err!=OK,err); { String title=packed_scene.is_valid()?"[gd_scene ":"[gd_resource "; if (packed_scene.is_null()) title+="type=\""+p_resource->get_type()+"\" "; int load_steps=saved_resources.size()+external_resources.size(); //if (packed_scene.is_valid()) { // load_steps+=packed_scene->get_node_count(); //} //no, better to not use load steps from nodes, no point to that if (load_steps>1) { title+="load_steps="+itos(load_steps)+" "; } title+="format="+itos(FORMAT_VERSION)+""; //title+="engine_version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\""; f->store_string(title); f->store_line("]\n"); //one empty line } Vector<RES> sorted_er; sorted_er.resize(external_resources.size()); for(Map<RES,int>::Element *E=external_resources.front();E;E=E->next()) { sorted_er[E->get()]=E->key(); } for(int i=0;i<sorted_er.size();i++) { String p = sorted_er[i]->get_path(); f->store_string("[ext_resource path=\""+p+"\" type=\""+sorted_er[i]->get_save_type()+"\" id="+itos(i+1)+"]\n"); //bundled } if (external_resources.size()) f->store_line(String()); //separate Set<int> used_indices; for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) { RES res = E->get(); if (E->next() && (res->get_path()=="" || res->get_path().find("::") != -1 )) { if (res->get_subindex()!=0) { if (used_indices.has(res->get_subindex())) { res->set_subindex(0); //repeated } else { used_indices.insert(res->get_subindex()); } } } } for(List<RES>::Element *E=saved_resources.front();E;E=E->next()) { RES res = E->get(); ERR_CONTINUE(!resource_set.has(res)); bool main = (E->next()==NULL); if (main && packed_scene.is_valid()) break; //save as a scene if (main) { f->store_line("[resource]\n"); } else { String line="[sub_resource "; if (res->get_subindex()==0) { int new_subindex=1; if (used_indices.size()) { new_subindex=used_indices.back()->get()+1; } res->set_subindex(new_subindex); used_indices.insert(new_subindex); } int idx = res->get_subindex(); line+="type=\""+res->get_type()+"\" id="+itos(idx); f->store_line(line+"]\n"); if (takeover_paths) { res->set_path(p_path+"::"+itos(idx),true); } internal_resources[res]=idx; } List<PropertyInfo> property_list; res->get_property_list(&property_list); // property_list.sort(); for(List<PropertyInfo>::Element *PE = property_list.front();PE;PE=PE->next()) { if (skip_editor && PE->get().name.begins_with("__editor")) continue; if (PE->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && PE->get().usage&PROPERTY_USAGE_BUNDLE)) { String name = PE->get().name; Variant value = res->get(name); if ((PE->get().usage&PROPERTY_USAGE_STORE_IF_NONZERO && value.is_zero())||(PE->get().usage&PROPERTY_USAGE_STORE_IF_NONONE && value.is_one()) ) continue; if (PE->get().type==Variant::OBJECT && value.is_zero() && !(PE->get().usage&PROPERTY_USAGE_STORE_IF_NULL)) continue; String vars; VariantWriter::write_to_string(value,vars,_write_resources,this); f->store_string(_valprop(name)+" = "+vars+"\n"); } } f->store_string("\n"); } if (packed_scene.is_valid()) { //if this is a scene, save nodes and connections! Ref<SceneState> state = packed_scene->get_state(); for(int i=0;i<state->get_node_count();i++) { StringName type = state->get_node_type(i); StringName name = state->get_node_name(i); NodePath path = state->get_node_path(i,true); NodePath owner = state->get_node_owner_path(i); Ref<PackedScene> instance = state->get_node_instance(i); String instance_placeholder = state->get_node_instance_placeholder(i); Vector<StringName> groups = state->get_node_groups(i); String header="[node"; header+=" name=\""+String(name)+"\""; if (type!=StringName()) { header+=" type=\""+String(type)+"\""; } if (path!=NodePath()) { header+=" parent=\""+String(path.simplified())+"\""; } if (owner!=NodePath() && owner!=NodePath(".")) { header+=" owner=\""+String(owner.simplified())+"\""; } if (groups.size()) { String sgroups=" groups=[ "; for(int j=0;j<groups.size();j++) { if (j>0) sgroups+=", "; sgroups+="\""+groups[j].operator String().c_escape()+"\""; } sgroups+=" ]"; header+=sgroups; } f->store_string(header); if (instance_placeholder!=String()) { String vars; f->store_string(" instance_placeholder="); VariantWriter::write_to_string(instance_placeholder,vars,_write_resources,this); f->store_string(vars); } if (instance.is_valid()) { String vars; f->store_string(" instance="); VariantWriter::write_to_string(instance,vars,_write_resources,this); f->store_string(vars); } f->store_line("]\n"); for(int j=0;j<state->get_node_property_count(i);j++) { String vars; VariantWriter::write_to_string(state->get_node_property_value(i,j),vars,_write_resources,this); f->store_string(_valprop(String(state->get_node_property_name(i,j)))+" = "+vars+"\n"); } if (state->get_node_property_count(i)) { //add space f->store_line(String()); } } for(int i=0;i<state->get_connection_count();i++) { String connstr="[connection"; connstr+=" signal=\""+String(state->get_connection_signal(i))+"\""; connstr+=" from=\""+String(state->get_connection_source(i).simplified())+"\""; connstr+=" to=\""+String(state->get_connection_target(i).simplified())+"\""; connstr+=" method=\""+String(state->get_connection_method(i))+"\""; int flags = state->get_connection_flags(i); if (flags!=Object::CONNECT_PERSIST) { connstr+=" flags="+itos(flags); } Array binds=state->get_connection_binds(i); f->store_string(connstr); if (binds.size()) { String vars; VariantWriter::write_to_string(binds,vars,_write_resources,this); f->store_string(" binds= "+vars); } f->store_line("]\n"); } f->store_line(String()); Vector<NodePath> editable_instances = state->get_editable_instances(); for(int i=0;i<editable_instances.size();i++) { f->store_line("[editable path=\""+editable_instances[i].operator String()+"\"]"); } } if (f->get_error()!=OK && f->get_error()!=ERR_FILE_EOF) { f->close(); return ERR_CANT_CREATE; } f->close(); //memdelete(f); return OK; }
void ResourceFormatSaverBinaryInstance::_find_resources(const Variant& p_variant,bool p_main) { switch(p_variant.get_type()) { case Variant::OBJECT: { RES res = p_variant.operator RefPtr(); if (res.is_null()) return; if (!p_main && (!bundle_resources ) && res->get_path().length() && res->get_path().find("::") == -1 ) { external_resources.insert(res); return; } if (resource_map.has(res)) return; List<PropertyInfo> property_list; res->get_property_list(&property_list); for(List<PropertyInfo>::Element *E=property_list.front();E;E=E->next()) { if (E->get().usage&PROPERTY_USAGE_STORAGE || (bundle_resources && E->get().usage&PROPERTY_USAGE_BUNDLE)) { _find_resources(res->get(E->get().name)); } } resource_map[ res ] = saved_resources.size(); saved_resources.push_back(res); } break; case Variant::ARRAY: { Array varray=p_variant; int len=varray.size(); for(int i=0;i<len;i++) { Variant v=varray.get(i); _find_resources(v); } } break; case Variant::DICTIONARY: { Dictionary d=p_variant; List<Variant> keys; d.get_key_list(&keys); for(List<Variant>::Element *E=keys.front();E;E=E->next()) { _find_resources(E->get()); Variant v = d[E->get()]; _find_resources(v); } } break; case Variant::NODE_PATH: { //take the chance and save node path strings NodePath np = p_variant; for(int i=0;i<np.get_name_count();i++) get_string_index(np.get_name(i)); for(int i=0;i<np.get_subname_count();i++) get_string_index(np.get_subname(i)); get_string_index(np.get_property()); } break; default: {} } }