-Added ability to remap dependencies in tscn (forgot to do it..), fixes #3368

This commit is contained in:
Juan Linietsky 2016-01-23 14:09:55 -03:00
parent d59733bd67
commit 0c6ffcf7b0
2 changed files with 46 additions and 98 deletions

View File

@ -356,7 +356,7 @@ Error ResourceInteractiveLoaderText::poll() {
int type=-1; int type=-1;
int name=-1; int name=-1;
int instance=-1; int instance=-1;
int base_scene=-1; // int base_scene=-1;
if (next_tag.fields.has("name")) { if (next_tag.fields.has("name")) {
name=packed_scene->get_state()->add_name(next_tag.fields["name"]); name=packed_scene->get_state()->add_name(next_tag.fields["name"]);
@ -619,57 +619,63 @@ void ResourceInteractiveLoaderText::get_dependencies(FileAccess *f,List<String>
Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map) { Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map) {
open(p_f,true);
#if 0
open(p_f);
ERR_FAIL_COND_V(error!=OK,error); ERR_FAIL_COND_V(error!=OK,error);
//FileAccess //FileAccess
bool old_format=false;
FileAccess *fw = NULL; FileAccess *fw = NULL;
String base_path=local_path.get_base_dir(); String base_path=local_path.get_base_dir();
uint64_t tag_end = f->get_pos();
while(true) { while(true) {
bool exit;
List<String> order;
Tag *tag = parse_tag(&exit,true,&order);
bool done=false;
if (!tag) { Error err = VariantParser::parse_tag(&stream,lines,error_text,next_tag,&rp);
if (err!=OK) {
if (fw) { if (fw) {
memdelete(fw); memdelete(fw);
} }
error=ERR_FILE_CORRUPT; error=ERR_FILE_CORRUPT;
ERR_FAIL_COND_V(!exit,error); ERR_FAIL_V(error);
error=ERR_FILE_EOF;
return error;
} }
if (tag->name=="ext_resource") { if (next_tag.name!="ext_resource") {
if (!tag->args.has("index") || !tag->args.has("path") || !tag->args.has("type")) { //nothing was done
old_format=true; if (!fw)
break; return OK;
}
break;
} else {
if (!fw) { if (!fw) {
fw=FileAccess::open(p_path+".depren",FileAccess::WRITE); fw=FileAccess::open(p_path+".depren",FileAccess::WRITE);
fw->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); //no escape if (is_scene) {
fw->store_line("<resource_file type=\""+resource_type+"\" subresource_count=\""+itos(resources_total)+"\" version=\""+itos(VERSION_MAJOR)+"."+itos(VERSION_MINOR)+"\" version_name=\""+VERSION_FULL_NAME+"\">"); fw->store_line("[gd_scene load_steps="+itos(resources_total)+" format="+itos(FORMAT_VERSION)+"]\n");
} else {
fw->store_line("[gd_resource type=\""+res_type+"\" load_steps="+itos(resources_total)+" format="+itos(FORMAT_VERSION)+"]\n");
}
} }
String path = tag->args["path"]; if (!next_tag.fields.has("path") || !next_tag.fields.has("id") || !next_tag.fields.has("type")) {
String index = tag->args["index"]; memdelete(fw);
String type = tag->args["type"]; error=ERR_FILE_CORRUPT;
ERR_FAIL_V(error);
}
String path = next_tag.fields["path"];
int index = next_tag.fields["id"];
String type = next_tag.fields["type"];
bool relative=false; bool relative=false;
@ -678,7 +684,6 @@ Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const
relative=true; relative=true;
} }
if (p_map.has(path)) { if (p_map.has(path)) {
String np=p_map[path]; String np=p_map[path];
path=np; path=np;
@ -689,74 +694,15 @@ Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const
path=base_path.path_to_file(path); path=base_path.path_to_file(path);
} }
tag->args["path"]=path; fw->store_line("[ext_resource path=\""+path+"\" type=\""+type+"\" id="+itos(index)+"]");
tag->args["index"]=index;
tag->args["type"]=type;
} else { tag_end = f->get_pos();
done=true;
} }
String tagt="\t<";
if (exit)
tagt+="/";
tagt+=tag->name;
for(List<String>::Element *E=order.front();E;E=E->next()) {
tagt+=" "+E->get()+"=\""+tag->args[E->get()]+"\"";
}
tagt+=">";
fw->store_line(tagt);
if (done)
break;
close_tag("ext_resource");
fw->store_line("\t</ext_resource>");
} }
f->seek(tag_end);
if (old_format) {
if (fw)
memdelete(fw);
DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
da->remove(p_path+".depren");
memdelete(da);
//fuck it, use the old approach;
WARN_PRINT(("This file is old, so it can't refactor dependencies, opening and resaving: "+p_path).utf8().get_data());
Error err;
FileAccess *f2 = FileAccess::open(p_path,FileAccess::READ,&err);
if (err!=OK) {
ERR_FAIL_COND_V(err!=OK,ERR_FILE_CANT_OPEN);
}
Ref<ResourceInteractiveLoaderText> ria = memnew( ResourceInteractiveLoaderText );
ria->local_path=Globals::get_singleton()->localize_path(p_path);
ria->res_path=ria->local_path;
ria->remaps=p_map;
// ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
ria->open(f2);
err = ria->poll();
while(err==OK) {
err=ria->poll();
}
ERR_FAIL_COND_V(err!=ERR_FILE_EOF,ERR_FILE_CORRUPT);
RES res = ria->get_resource();
ERR_FAIL_COND_V(!res.is_valid(),ERR_FILE_CORRUPT);
return ResourceFormatSaverText::singleton->save(p_path,res);
}
if (!fw) {
return OK; //nothing to rename, do nothing
}
uint8_t c=f->get_8(); uint8_t c=f->get_8();
while(!f->eof_reached()) { while(!f->eof_reached()) {
@ -776,13 +722,13 @@ Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const
da->remove(p_path); da->remove(p_path);
da->rename(p_path+".depren",p_path); da->rename(p_path+".depren",p_path);
memdelete(da); memdelete(da);
#endif
return OK; return OK;
} }
void ResourceInteractiveLoaderText::open(FileAccess *p_f) { void ResourceInteractiveLoaderText::open(FileAccess *p_f,bool p_skip_first_tag) {
error=OK; error=OK;
@ -845,13 +791,15 @@ void ResourceInteractiveLoaderText::open(FileAccess *p_f) {
resources_total=0; resources_total=0;
} }
if (!p_skip_first_tag) {
err = VariantParser::parse_tag(&stream,lines,error_text,next_tag,&rp); err = VariantParser::parse_tag(&stream,lines,error_text,next_tag,&rp);
if (err) { if (err) {
error_text="Unexpected end of file"; error_text="Unexpected end of file";
_printerr(); _printerr();
error=ERR_FILE_CORRUPT; error=ERR_FILE_CORRUPT;
}
} }
rp.ext_func=_parse_ext_resources; rp.ext_func=_parse_ext_resources;

View File

@ -72,7 +72,7 @@ public:
virtual int get_stage() const; virtual int get_stage() const;
virtual int get_stage_count() const; virtual int get_stage_count() const;
void open(FileAccess *p_f); void open(FileAccess *p_f, bool p_skip_first_tag=false);
String recognize(FileAccess *p_f); String recognize(FileAccess *p_f);
void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types); void get_dependencies(FileAccess *p_f, List<String> *p_dependencies, bool p_add_types);
Error rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map); Error rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map);