diff options
Diffstat (limited to 'scene/resources/scene_format_text.cpp')
| -rw-r--r-- | scene/resources/scene_format_text.cpp | 156 |
1 files changed, 104 insertions, 52 deletions
diff --git a/scene/resources/scene_format_text.cpp b/scene/resources/scene_format_text.cpp index f9f7f7807d..a913687e7f 100644 --- a/scene/resources/scene_format_text.cpp +++ b/scene/resources/scene_format_text.cpp @@ -1,3 +1,31 @@ +/*************************************************************************/ +/* scene_format_text.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* http://www.godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ #include "scene_format_text.h" #include "globals.h" @@ -39,12 +67,17 @@ Error ResourceInteractiveLoaderText::_parse_sub_resource(VariantParser::Stream* String path = local_path+"::"+itos(index); - if (!ResourceCache::has(path)) { - r_err_str="Can't load cached sub-resource: "+path; - return ERR_PARSE_ERROR; - } + if (!ignore_resource_parsing) { + + if (!ResourceCache::has(path)) { + r_err_str="Can't load cached sub-resource: "+path; + return ERR_PARSE_ERROR; + } - r_res=RES(ResourceCache::get(path)); + r_res=RES(ResourceCache::get(path)); + } else { + r_res=RES(); + } VariantParser::get_token(p_stream,token,line,r_err_str); if (token.type!=VariantParser::TK_PARENTHESIS_CLOSE) { @@ -67,25 +100,29 @@ Error ResourceInteractiveLoaderText::_parse_ext_resource(VariantParser::Stream* int id = token.value; + if (!ignore_resource_parsing) { - if (!ext_resources.has(id)) { - r_err_str="Can't load cached ext-resource #"+itos(id); - return ERR_PARSE_ERROR; - } + if (!ext_resources.has(id)) { + r_err_str="Can't load cached ext-resource #"+itos(id); + return ERR_PARSE_ERROR; + } - String path = ext_resources[id].path; - String type = ext_resources[id].type; + String path = ext_resources[id].path; + String type = ext_resources[id].type; - if (path.find("://")==-1 && path.is_rel_path()) { - // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); + if (path.find("://")==-1 && path.is_rel_path()) { + // path is relative to file being loaded, so convert to a resource path + path=GlobalConfig::get_singleton()->localize_path(res_path.get_base_dir().plus_file(path)); - } + } - r_res=ResourceLoader::load(path,type); + r_res=ResourceLoader::load(path,type); - if (r_res.is_null()) { - WARN_PRINT(String("Couldn't load external resource: "+path).utf8().get_data()); + if (r_res.is_null()) { + WARN_PRINT(String("Couldn't load external resource: "+path).utf8().get_data()); + } + } else { + r_res=RES(); } VariantParser::get_token(p_stream,token,line,r_err_str); @@ -135,7 +172,7 @@ Error ResourceInteractiveLoaderText::poll() { if (path.find("://")==-1 && path.is_rel_path()) { // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); + path=GlobalConfig::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); } if (remaps.has(path)) { @@ -203,7 +240,7 @@ Error ResourceInteractiveLoaderText::poll() { if ( !ResourceCache::has(path)) { //only if it doesn't exist - Object *obj = ObjectTypeDB::instance(type); + Object *obj = ClassDB::instance(type); if (!obj) { error_text+="Can't create sub resource of type: "+type; @@ -273,7 +310,7 @@ Error ResourceInteractiveLoaderText::poll() { return error; } - Object *obj = ObjectTypeDB::instance(res_type); + Object *obj = ClassDB::instance(res_type); if (!obj) { error_text+="Can't create sub resource of type: "+res_type; @@ -360,14 +397,16 @@ Error ResourceInteractiveLoaderText::poll() { int type=-1; int name=-1; int instance=-1; -// int base_scene=-1; + //int base_scene=-1; if (next_tag.fields.has("name")) { name=packed_scene->get_state()->add_name(next_tag.fields["name"]); } if (next_tag.fields.has("parent")) { - parent=packed_scene->get_state()->add_node_path(next_tag.fields["parent"]); + NodePath np = next_tag.fields["parent"]; + np.prepend_period(); //compatible to how it manages paths internally + parent=packed_scene->get_state()->add_node_path(np); } @@ -597,6 +636,7 @@ void ResourceInteractiveLoaderText::get_dependencies(FileAccess *f,List<String> open(f); + ignore_resource_parsing=true; ERR_FAIL_COND(error!=OK); while(next_tag.name=="ext_resource") { @@ -621,7 +661,7 @@ void ResourceInteractiveLoaderText::get_dependencies(FileAccess *f,List<String> if (path.find("://")==-1 && path.is_rel_path()) { // path is relative to file being loaded, so convert to a resource path - path=Globals::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); + path=GlobalConfig::get_singleton()->localize_path(local_path.get_base_dir().plus_file(path)); } @@ -634,6 +674,7 @@ void ResourceInteractiveLoaderText::get_dependencies(FileAccess *f,List<String> Error err = VariantParser::parse_tag(&stream,lines,error_text,next_tag,&rp); if (err) { + print_line(error_text+" - "+itos(lines)); error_text="Unexpected end of file"; _printerr(); error=ERR_FILE_CORRUPT; @@ -648,7 +689,7 @@ Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const open(p_f,true); ERR_FAIL_COND_V(error!=OK,error); - + ignore_resource_parsing=true; //FileAccess FileAccess *fw = NULL; @@ -766,7 +807,7 @@ void ResourceInteractiveLoaderText::open(FileAccess *p_f,bool p_skip_first_tag) stream.f=f; is_scene=false; - + ignore_resource_parsing=false; resource_current=0; @@ -851,6 +892,8 @@ String ResourceInteractiveLoaderText::recognize(FileAccess *p_f) { stream.f=f; + ignore_resource_parsing=true; + VariantParser::Tag tag; Error err = VariantParser::parse_tag(&stream,lines,error_text,tag); @@ -905,9 +948,9 @@ Ref<ResourceInteractiveLoader> ResourceFormatLoaderText::load_interactive(const } Ref<ResourceInteractiveLoaderText> ria = memnew( ResourceInteractiveLoaderText ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); + //ria->set_local_path( GlobalConfig::get_singleton()->localize_path(p_path) ); ria->open(f); return ria; @@ -941,7 +984,7 @@ String ResourceFormatLoaderText::get_resource_type(const String &p_path) const{ - String ext=p_path.extension().to_lower(); + String ext=p_path.get_extension().to_lower(); if (ext=="tscn") return "PackedScene"; else if (ext!="tres") @@ -956,9 +999,9 @@ String ResourceFormatLoaderText::get_resource_type(const String &p_path) const{ } Ref<ResourceInteractiveLoaderText> ria = memnew( ResourceInteractiveLoaderText ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); + //ria->set_local_path( GlobalConfig::get_singleton()->localize_path(p_path) ); String r = ria->recognize(f); return r; } @@ -973,9 +1016,9 @@ void ResourceFormatLoaderText::get_dependencies(const String& p_path,List<String } Ref<ResourceInteractiveLoaderText> ria = memnew( ResourceInteractiveLoaderText ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); + //ria->set_local_path( GlobalConfig::get_singleton()->localize_path(p_path) ); ria->get_dependencies(f,p_dependencies,p_add_types); @@ -990,9 +1033,9 @@ Error ResourceFormatLoaderText::rename_dependencies(const String &p_path,const M } Ref<ResourceInteractiveLoaderText> ria = memnew( ResourceInteractiveLoaderText ); - ria->local_path=Globals::get_singleton()->localize_path(p_path); + ria->local_path=GlobalConfig::get_singleton()->localize_path(p_path); ria->res_path=ria->local_path; -// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) ); + //ria->set_local_path( GlobalConfig::get_singleton()->localize_path(p_path) ); return ria->rename_dependencies(f,p_path,p_map); } @@ -1072,7 +1115,7 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant& p_variant,b PropertyInfo pi=I->get(); - if (pi.usage&PROPERTY_USAGE_STORAGE || (bundle_resources && pi.usage&PROPERTY_USAGE_BUNDLE)) { + if (pi.usage&PROPERTY_USAGE_STORAGE) { Variant v=res->get(I->get().name); _find_resources(v); @@ -1112,7 +1155,12 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant& p_variant,b } +static String _valprop(const String& p_name) { + if (p_name.find("\"")!=-1 || p_name.find("=")!=-1 || p_name.find(" ")!=-1) + return "\""+p_name.c_escape()+"\""; + return p_name; +} Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_resource,uint32_t p_flags) { @@ -1125,7 +1173,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re ERR_FAIL_COND_V( err, ERR_CANT_OPEN ); FileAccessRef _fref(f); - local_path = Globals::get_singleton()->localize_path(p_path); + local_path = GlobalConfig::get_singleton()->localize_path(p_path); relative_paths=p_flags&ResourceSaver::FLAG_RELATIVE_PATHS; skip_editor=p_flags&ResourceSaver::FLAG_OMIT_EDITOR_PROPERTIES; @@ -1158,12 +1206,14 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re { String title=packed_scene.is_valid()?"[gd_scene ":"[gd_resource "; if (packed_scene.is_null()) - title+="type=\""+p_resource->get_type()+"\" "; + title+="type=\""+p_resource->get_class()+"\" "; int load_steps=saved_resources.size()+external_resources.size(); - //if (packed_scene.is_valid()) { - // load_steps+=packed_scene->get_node_count(); - //} + /* + 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)+" "; @@ -1187,7 +1237,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re 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 + f->store_string("[ext_resource path=\""+p+"\" type=\""+sorted_er[i]->get_save_class()+"\" id="+itos(i+1)+"]\n"); //bundled } if (external_resources.size()) @@ -1234,27 +1284,31 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re } int idx = res->get_subindex(); - line+="type=\""+res->get_type()+"\" id="+itos(idx); + line+="type=\""+res->get_class()+"\" id="+itos(idx); f->store_line(line+"]\n"); if (takeover_paths) { res->set_path(p_path+"::"+itos(idx),true); } internal_resources[res]=idx; +#ifdef TOOLS_ENABLED + res->set_edited(false); +#endif + } List<PropertyInfo> property_list; res->get_property_list(&property_list); -// property_list.sort(); + //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)) { + if (PE->get().usage&PROPERTY_USAGE_STORAGE) { String name = PE->get().name; Variant value = res->get(name); @@ -1263,12 +1317,12 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re 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()) + 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(name+" = "+vars+"\n"); + f->store_string(_valprop(name)+" = "+vars+"\n"); } @@ -1292,8 +1346,6 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re Vector<StringName> groups = state->get_node_groups(i); - if (instance.is_valid()) - print_line("for path "+String(path)+" instance "+instance->get_path()); String header="[node"; header+=" name=\""+String(name)+"\""; @@ -1344,7 +1396,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re String vars; VariantWriter::write_to_string(state->get_node_property_value(i,j),vars,_write_resources,this); - f->store_string(String(state->get_node_property_name(i,j))+" = "+vars+"\n"); + f->store_string(_valprop(String(state->get_node_property_name(i,j)))+" = "+vars+"\n"); } if (state->get_node_property_count(i)) { @@ -1401,7 +1453,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path,const RES& p_re Error ResourceFormatSaverText::save(const String &p_path,const RES& p_resource,uint32_t p_flags) { - if (p_path.ends_with(".sct") && p_resource->get_type()!="PackedScene") { + if (p_path.ends_with(".sct") && p_resource->get_class()!="PackedScene") { return ERR_FILE_UNRECOGNIZED; } @@ -1417,7 +1469,7 @@ bool ResourceFormatSaverText::recognize(const RES& p_resource) const { } void ResourceFormatSaverText::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const { - if (p_resource->get_type()=="PackedScene") + if (p_resource->get_class()=="PackedScene") p_extensions->push_back("tscn"); //text scene else p_extensions->push_back("tres"); //text resource |