Error ResourceFormatSaverShader::save(const String &p_path, const RES &p_resource, uint32_t p_flags) { Ref<Shader> shader = p_resource; ERR_FAIL_COND_V(shader.is_null(), ERR_INVALID_PARAMETER); String source = shader->get_code(); Error err; FileAccess *file = FileAccess::open(p_path, FileAccess::WRITE, &err); if (err) { ERR_FAIL_COND_V(err, err); } file->store_string(source); if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) { memdelete(file); return ERR_CANT_CREATE; } file->close(); memdelete(file); return OK; }
Error DirAccess::copy(String p_from, String p_to, int p_chmod_flags) { //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data()); Error err; FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ, &err); if (err) { ERR_PRINTS("Failed to open " + p_from); return err; } FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE, &err); if (err) { fsrc->close(); memdelete(fsrc); ERR_PRINTS("Failed to open " + p_to); return err; } fsrc->seek_end(0); int size = fsrc->get_position(); fsrc->seek(0); err = OK; while (size--) { if (fsrc->get_error() != OK) { err = fsrc->get_error(); break; } if (fdst->get_error() != OK) { err = fdst->get_error(); break; } fdst->store_8(fsrc->get_8()); } if (err == OK && p_chmod_flags != -1) { fdst->close(); err = FileAccess::set_unix_permissions(p_to, p_chmod_flags); // If running on a platform with no chmod support (i.e., Windows), don't fail if (err == ERR_UNAVAILABLE) err = OK; } memdelete(fsrc); memdelete(fdst); return err; }
Error DirAccess::copy(String p_from,String p_to) { //printf("copy %s -> %s\n",p_from.ascii().get_data(),p_to.ascii().get_data()); Error err; FileAccess *fsrc = FileAccess::open(p_from, FileAccess::READ,&err); if (err) { ERR_FAIL_COND_V( err, err ); } FileAccess *fdst = FileAccess::open(p_to, FileAccess::WRITE,&err ); if (err) { fsrc->close(); memdelete( fsrc ); ERR_FAIL_COND_V( err, err ); } fsrc->seek_end(0); int size = fsrc->get_pos(); fsrc->seek(0); err = OK; while(size--) { if (fsrc->get_error()!=OK) { err= fsrc->get_error(); break; } if (fdst->get_error()!=OK) { err= fdst->get_error(); break; } fdst->store_8( fsrc->get_8() ); } memdelete(fsrc); memdelete(fdst); return err; }
static int godot_testerror(voidpf opaque, voidpf stream) { FileAccess* f = (FileAccess*)opaque; return f->get_error()!=OK?1:0; };
Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map) { open(p_f,true); ERR_FAIL_COND_V(error!=OK,error); ignore_resource_parsing=true; //FileAccess FileAccess *fw = NULL; String base_path=local_path.get_base_dir(); uint64_t tag_end = f->get_pos(); while(true) { Error err = VariantParser::parse_tag(&stream,lines,error_text,next_tag,&rp); if (err!=OK) { if (fw) { memdelete(fw); } error=ERR_FILE_CORRUPT; ERR_FAIL_V(error); } if (next_tag.name!="ext_resource") { //nothing was done if (!fw) return OK; break; } else { if (!fw) { fw=FileAccess::open(p_path+".depren",FileAccess::WRITE); if (is_scene) { 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"); } } if (!next_tag.fields.has("path") || !next_tag.fields.has("id") || !next_tag.fields.has("type")) { memdelete(fw); 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; if (!path.begins_with("res://")) { path=base_path.plus_file(path).simplify_path(); relative=true; } if (p_map.has(path)) { String np=p_map[path]; path=np; } if (relative) { //restore relative path=base_path.path_to_file(path); } fw->store_line("[ext_resource path=\""+path+"\" type=\""+type+"\" id="+itos(index)+"]"); tag_end = f->get_pos(); } } f->seek(tag_end); uint8_t c=f->get_8(); while(!f->eof_reached()) { fw->store_8(c); c=f->get_8(); } f->close(); bool all_ok = fw->get_error()==OK; memdelete(fw); if (!all_ok) { return ERR_CANT_CREATE; } DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); da->remove(p_path); da->rename(p_path+".depren",p_path); memdelete(da); return OK; }
Error ResourceInteractiveLoaderText::rename_dependencies(FileAccess *p_f, const String &p_path,const Map<String,String>& p_map) { #if 0 open(p_f); ERR_FAIL_COND_V(error!=OK,error); //FileAccess bool old_format=false; FileAccess *fw = NULL; String base_path=local_path.get_base_dir(); while(true) { bool exit; List<String> order; Tag *tag = parse_tag(&exit,true,&order); bool done=false; if (!tag) { if (fw) { memdelete(fw); } error=ERR_FILE_CORRUPT; ERR_FAIL_COND_V(!exit,error); error=ERR_FILE_EOF; return error; } if (tag->name=="ext_resource") { if (!tag->args.has("index") || !tag->args.has("path") || !tag->args.has("type")) { old_format=true; break; } if (!fw) { fw=FileAccess::open(p_path+".depren",FileAccess::WRITE); fw->store_line("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); //no escape 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+"\">"); } String path = tag->args["path"]; String index = tag->args["index"]; String type = tag->args["type"]; bool relative=false; if (!path.begins_with("res://")) { path=base_path.plus_file(path).simplify_path(); relative=true; } if (p_map.has(path)) { String np=p_map[path]; path=np; } if (relative) { //restore relative path=base_path.path_to_file(path); } tag->args["path"]=path; tag->args["index"]=index; tag->args["type"]=type; } else { 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>"); } if (old_format) { if (fw) memdelete(fw); DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); da->remove(p_path+".depren"); memdelete(da); //f**k 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(); while(!f->eof_reached()) { fw->store_8(c); c=f->get_8(); } bool all_ok = fw->get_error()==OK; memdelete(fw); if (!all_ok) { return ERR_CANT_CREATE; } DirAccess *da = DirAccess::create(DirAccess::ACCESS_RESOURCES); da->remove(p_path); da->rename(p_path+".depren",p_path); memdelete(da); #endif return OK; }