Example #1
0
RES ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {

	bool found = false;

	// Try all loaders and pick the first match for the type hint
	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(p_path, p_type_hint)) {
			continue;
		}
		found = true;
		RES res = loader[i]->load(p_path, p_original_path != String() ? p_original_path : p_path, r_error);
		if (res.is_null()) {
			continue;
		}

		return res;
	}

	if (found) {
		ERR_EXPLAIN("Failed loading resource: " + p_path);
	} else {
		ERR_EXPLAIN("No loader found for resource: " + p_path);
	}
	ERR_FAIL_V(RES());
	return RES();
}
Example #2
0
void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource> > &remap_cache) {

	print_line("configure for local: " + get_class());
	List<PropertyInfo> plist;
	get_property_list(&plist);

	local_scene = p_for_scene;

	for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {

		if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
			continue;
		Variant p = get(E->get().name);
		if (p.get_type() == Variant::OBJECT) {

			RES sr = p;
			if (sr.is_valid()) {

				if (sr->is_local_to_scene()) {
					if (!remap_cache.has(sr)) {
						sr->configure_for_local_scene(p_for_scene, remap_cache);
						remap_cache[sr] = sr;
					}
				}
			}
		}
	}
}
Example #3
0
Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String& p_path) {

	RES res = ResourceLoader::load(p_path);
	if (!res.is_valid())
		return res;
	return generate(res);
}
bool recursive_receive(zmq::socket_t& receiving_socket, zmq::message_t& message,
                       const REQ& req, RES& response, bool& succeed) {
  bool rc = receiving_socket.recv(&message);

  if (rc) {
    auto serialized_resp = kZmqUtil->message_to_string(message);
    response.ParseFromString(serialized_resp);

    if (req.request_id() == response.response_id()) {
      succeed = true;
      return false;
    } else {
      return true;
    }
  } else {
    // timeout
    if (errno == EAGAIN) {
      succeed = false;
    } else {
      succeed = false;
    }

    return false;
  }
}
void ResourcePreloaderEditor::_paste_pressed() {

	RES r = EditorSettings::get_singleton()->get_resource_clipboard();
	if (!r.is_valid()) {
		dialog->set_text(TTR("Resource clipboard is empty!"));
		dialog->set_title(TTR("Error!"));
		dialog->get_ok()->set_text(TTR("Close"));
		dialog->popup_centered_minsize();
		return; ///beh should show an error i guess
	}

	String name = r->get_name();
	if (name == "")
		name = r->get_path().get_file();
	if (name == "")
		name = r->get_class();

	String basename = name;
	int counter = 1;
	while (preloader->has_resource(name)) {
		counter++;
		name = basename + " " + itos(counter);
	}

	undo_redo->create_action(TTR("Paste Resource"));
	undo_redo->add_do_method(preloader, "add_resource", name, r);
	undo_redo->add_undo_method(preloader, "remove_resource", name);
	undo_redo->add_do_method(this, "_update_library");
	undo_redo->add_undo_method(this, "_update_library");
	undo_redo->commit_action();
}
Example #6
0
Ref<Resource> Resource::duplicate(bool p_subresources) {

	List<PropertyInfo> plist;
	get_property_list(&plist);

	Resource *r = (Resource *)ObjectTypeDB::instance(get_type());
	ERR_FAIL_COND_V(!r, Ref<Resource>());

	for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {

		if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
			continue;
		Variant p = get(E->get().name);
		if (p.get_type() == Variant::OBJECT && p_subresources) {

			RES sr = p;
			if (sr.is_valid())
				p = sr->duplicate(true);
		}

		r->set(E->get().name, p);
	}

	return Ref<Resource>(r);
}
Example #7
0
Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
	RES res = ResourceLoader::load(p_path);
	ERR_EXPLAIN("Can't autoload: " + p_path);
	ERR_FAIL_COND_V(res.is_null(), NULL);
	Node *n = NULL;
	if (res->is_class("PackedScene")) {
		Ref<PackedScene> ps = res;
		n = ps->instance();
	} else if (res->is_class("Script")) {
		Ref<Script> s = res;
		StringName ibt = s->get_instance_base_type();
		bool valid_type = ClassDB::is_parent_class(ibt, "Node");
		ERR_EXPLAIN("Script does not inherit a Node: " + p_path);
		ERR_FAIL_COND_V(!valid_type, NULL);

		Object *obj = ClassDB::instance(ibt);

		ERR_EXPLAIN("Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt));
		ERR_FAIL_COND_V(obj == NULL, NULL);

		n = Object::cast_to<Node>(obj);
		n->set_script(s.get_ref_ptr());
	}

	ERR_EXPLAIN("Path in autoload not a node or script: " + p_path);
	ERR_FAIL_COND_V(!n, NULL);

	return n;
}
void ResourcePreloaderEditor::_files_load_request(const Vector<String> &p_paths) {

	for (int i = 0; i < p_paths.size(); i++) {

		String path = p_paths[i];

		RES resource;
		resource = ResourceLoader::load(path);

		if (resource.is_null()) {
			dialog->set_text(TTR("ERROR: Couldn't load resource!"));
			dialog->set_title(TTR("Error!"));
			//dialog->get_cancel()->set_text("Close");
			dialog->get_ok()->set_text(TTR("Close"));
			dialog->popup_centered_minsize();
			return; ///beh should show an error i guess
		}

		String basename = path.get_file().get_basename();
		String name = basename;
		int counter = 1;
		while (preloader->has_resource(name)) {
			counter++;
			name = basename + " " + itos(counter);
		}

		undo_redo->create_action(TTR("Add Resource"));
		undo_redo->add_do_method(preloader, "add_resource", name, resource);
		undo_redo->add_undo_method(preloader, "remove_resource", name);
		undo_redo->add_do_method(this, "_update_library");
		undo_redo->add_undo_method(this, "_update_library");
		undo_redo->commit_action();
	}
}
bool ResourcePreloaderEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {

	Dictionary d = p_data;

	if (!d.has("type"))
		return false;

	if (d.has("from") && (Object *)(d["from"]) == tree)
		return false;

	if (String(d["type"]) == "resource" && d.has("resource")) {
		RES r = d["resource"];

		return r.is_valid();
	}

	if (String(d["type"]) == "files") {

		Vector<String> files = d["files"];

		if (files.size() == 0)
			return false;

		return true;
	}
	return false;
}
void ResourcePreloaderEditor::_file_load_request(const String& p_path) {


	RES resource;

	resource = ResourceLoader::load(p_path);

	if (resource.is_null()) {
		dialog->set_text("ERROR: Couldn't load resource!");
		dialog->set_title("Error!");
		//dialog->get_cancel()->set_text("Close");
		dialog->get_ok()->set_text("Close");
		dialog->popup_centered(Size2(300,60));
		return; ///beh should show an error i guess
	}

	String basename = p_path.get_file().basename();
	String name=basename;
	int counter=1;
	while(preloader->has_resource(name)) {
		counter++;
		name=basename+" "+itos(counter);
	}

	undo_redo->create_action("Add Resource");
	undo_redo->add_do_method(preloader,"add_resource",name,resource);
	undo_redo->add_undo_method(preloader,"remove_resource",name);
	undo_redo->add_do_method(this,"_update_library");
	undo_redo->add_undo_method(this,"_update_library");
	undo_redo->commit_action();
}
Example #11
0
Object *CreateDialog::instance_selected() {

	TreeItem *selected = search_options->get_selected();

	if (selected) {

		Variant md = selected->get_metadata(0);

		String custom;
		if (md.get_type() != Variant::NIL)
			custom = md;

		if (custom != String()) {

			if (ScriptServer::is_global_class(custom)) {
				RES script = ResourceLoader::load(ScriptServer::get_global_class_path(custom));
				ERR_FAIL_COND_V(!script.is_valid(), NULL);

				Object *obj = ClassDB::instance(ScriptServer::get_global_class_base(custom));
				ERR_FAIL_COND_V(!obj, NULL);

				obj->set_script(script.get_ref_ptr());
				return obj;
			}
			return EditorNode::get_editor_data().instance_custom_type(selected->get_text(0), custom);
		} else {
			return ClassDB::instance(selected->get_text(0));
		}
	}

	return NULL;
}
Example #12
0
RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {

	if (r_error)
		*r_error = ERR_CANT_OPEN;

	String local_path;
	if (p_path.is_rel_path())
		local_path = "res://" + p_path;
	else
		local_path = GlobalConfig::get_singleton()->localize_path(p_path);

	ERR_FAIL_COND_V(local_path == "", RES());

	if (!p_no_cache && ResourceCache::has(local_path)) {

		if (OS::get_singleton()->is_stdout_verbose())
			print_line("load resource: " + local_path + " (cached)");

		return RES(ResourceCache::get(local_path));
	}

	if (OS::get_singleton()->is_stdout_verbose())
		print_line("load resource: " + local_path);
	bool found = false;

	for (int i = 0; i < loader_count; i++) {

		if (!loader[i]->recognize_path(local_path, p_type_hint)) {
			print_line("path not recognized");
			continue;
		}
		found = true;
		RES res = loader[i]->load(local_path, local_path, r_error);
		if (res.is_null()) {
			continue;
		}
		if (!p_no_cache)
			res->set_path(local_path);
#ifdef TOOLS_ENABLED

		res->set_edited(false);
		if (timestamp_on_load) {
			uint64_t mt = FileAccess::get_modified_time(local_path);
			//printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
			res->set_last_modified_time(mt);
		}
#endif

		return res;
	}

	if (found) {
		ERR_EXPLAIN("Failed loading resource: " + p_path);
	} else {
		ERR_EXPLAIN("No loader found for resource: " + p_path);
	}
	ERR_FAIL_V(RES());
	return RES();
}
Example #13
0
RES ResourceLoader::load(const String &p_path,const String& p_type_hint,bool p_no_cache) {

	String local_path = Globals::get_singleton()->localize_path(p_path);

	local_path=find_complete_path(p_path,p_type_hint);
	ERR_FAIL_COND_V(local_path=="",RES());

	if (!p_no_cache && ResourceCache::has(local_path)) {

		if (OS::get_singleton()->is_stdout_verbose())
			print_line("load resource: "+local_path+" (cached)");

		return RES( ResourceCache::get(local_path ) );
	}

	String remapped_path = PathRemap::get_singleton()->get_remap(local_path);

	if (OS::get_singleton()->is_stdout_verbose())
		print_line("load resource: "+remapped_path);

	String extension=remapped_path.extension();
	bool found=false;
	
	for (int i=0;i<loader_count;i++) {
		
		if (!loader[i]->recognize(extension))
			continue;
		if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
			continue;
		found=true;
		RES res = loader[i]->load(remapped_path,local_path);
		if (res.is_null())
			continue;
		if (!p_no_cache)
			res->set_path(local_path);
#ifdef TOOLS_ENABLED

		res->set_edited(false);
		if (timestamp_on_load) {
			uint64_t mt = FileAccess::get_modified_time(remapped_path);
			//printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
			res->set_last_modified_time(mt);
		}
#endif

		return res;
	}

	if (found) {
		ERR_EXPLAIN("Failed loading resource: "+p_path);
	} else {
		ERR_EXPLAIN("No loader found for resource: "+p_path);
	}
	ERR_FAIL_V(RES());
	return RES();
}
Example #14
0
void InspectorDock::_resource_file_selected(String p_file) {
	RES res = ResourceLoader::load(p_file);

	if (res.is_null()) {
		warning_dialog->set_text(TTR("Failed to load resource."));
		return;
	};

	editor->push_item(res.operator->());
}
Ref<Texture> EditorResourcePreviewGenerator::generate_from_path(const String &p_path, const Size2 p_size) const {

	if (get_script_instance() && get_script_instance()->has_method("generate_from_path")) {
		return get_script_instance()->call("generate_from_path", p_path, p_size);
	}

	RES res = ResourceLoader::load(p_path);
	if (!res.is_valid())
		return res;
	return generate(res, p_size);
}
Example #16
0
Object *EditorData::script_class_instance(const String &p_class) {
	if (ScriptServer::is_global_class(p_class)) {
		Object *obj = ClassDB::instance(ScriptServer::get_global_class_base(p_class));
		if (obj) {
			RES script = ResourceLoader::load(ScriptServer::get_global_class_path(p_class));
			if (script.is_valid())
				obj->set_script(script.get_ref_ptr());
			return obj;
		}
	}
	return NULL;
}
Variant ResourcePreloaderEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {

	TreeItem *ti = tree->get_item_at_position(p_point);
	if (!ti)
		return Variant();

	String name = ti->get_metadata(0);

	RES res = preloader->get_resource(name);
	if (!res.is_valid())
		return Variant();

	return EditorNode::get_singleton()->drag_resource(res, p_from);
}
Example #18
0
void ResourceFormatSaverText::get_recognized_extensions(const RES& p_resource,List<String> *p_extensions) const {

	p_extensions->push_back("tres"); //text resource
	if (p_resource->get_type()=="PackedScene")
		p_extensions->push_back("tscn"); //text scene

}
Example #19
0
void InspectorDock::_resource_selected(const RES &p_res, const String &p_property) const {
	if (p_res.is_null())
		return;

	RES r = p_res;
	editor->push_item(r.operator->(), p_property);
}
Example #20
0
void ResourcePreloader::add_resource(const StringName& p_name,const RES& p_resource) {


	ERR_FAIL_COND(p_resource.is_null());
	if (resources.has(p_name)) {


		StringName new_name;
		int idx=2;

		while(true) {

			new_name=p_name.operator String()+" "+itos(idx);
			if (resources.has(new_name)) {
				idx++;
				continue;
			}

			break;

		}

		add_resource(new_name,p_resource);
	} else {

		resources[p_name]=p_resource;
	}



}
void ResourcePreloaderEditor::_update_library() {

	tree->clear();
	tree->set_hide_root(true);
	TreeItem *root = tree->create_item(NULL);

	List<StringName> rnames;
	preloader->get_resource_list(&rnames);

	List<String> names;
	for (List<StringName>::Element *E = rnames.front(); E; E = E->next()) {
		names.push_back(E->get());
	}

	names.sort();

	for (List<String>::Element *E = names.front(); E; E = E->next()) {

		TreeItem *ti = tree->create_item(root);
		ti->set_cell_mode(0, TreeItem::CELL_MODE_STRING);
		ti->set_editable(0, true);
		ti->set_selectable(0, true);
		ti->set_text(0, E->get());
		ti->set_metadata(0, E->get());

		RES r = preloader->get_resource(E->get());

		ERR_CONTINUE(r.is_null());

		String type = r->get_class();
		ti->set_icon(0, EditorNode::get_singleton()->get_class_icon(type, "Object"));
		ti->set_tooltip(0, TTR("Instance:") + " " + r->get_path() + "\n" + TTR("Type:") + " " + type);

		ti->set_text(1, r->get_path());
		ti->set_editable(1, false);
		ti->set_selectable(1, false);

		if (type == "PackedScene") {
			ti->add_button(1, get_icon("InstanceOptions", "EditorIcons"), BUTTON_OPEN_SCENE, false, TTR("Open in Editor"));
		} else {
			ti->add_button(1, get_icon("Load", "EditorIcons"), BUTTON_EDIT_RESOURCE, false, TTR("Open in Editor"));
		}
		ti->add_button(1, get_icon("Remove", "EditorIcons"), BUTTON_REMOVE, false, TTR("Remove"));
	}

	//player->add_resource("default",resource);
}
Example #22
0
MainLoop *test() {

	print_line("this is test io");
	DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
	da->change_dir(".");
	print_line("Opening current dir " + da->get_current_dir());
	String entry;
	da->list_dir_begin();
	while ((entry = da->get_next()) != "") {

		print_line("entry " + entry + " is dir: " + Variant(da->current_is_dir()));
	};
	da->list_dir_end();

	RES texture = ResourceLoader::load("test_data/rock.png");
	ERR_FAIL_COND_V(texture.is_null(), NULL);

	ResourceSaver::save("test_data/rock.xml", texture);

	print_line("localize paths");
	print_line(ProjectSettings::get_singleton()->localize_path("algo.xml"));
	print_line(ProjectSettings::get_singleton()->localize_path("c:\\windows\\algo.xml"));
	print_line(ProjectSettings::get_singleton()->localize_path(ProjectSettings::get_singleton()->get_resource_path() + "/something/something.xml"));
	print_line(ProjectSettings::get_singleton()->localize_path("somedir/algo.xml"));

	{

		FileAccess *z = FileAccess::open("test_data/archive.zip", FileAccess::READ);
		int len = z->get_len();
		Vector<uint8_t> zip;
		zip.resize(len);
		z->get_buffer(&zip[0], len);
		z->close();
		memdelete(z);

		FileAccessMemory::register_file("a_package", zip);
		FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_RESOURCES);
		FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_FILESYSTEM);
		FileAccess::make_default<FileAccessMemory>(FileAccess::ACCESS_USERDATA);

		print_line("archive test");
	};

	print_line("test done");

	return memnew(TestMainLoop);
}
Variant SpriteFramesEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {

	if (!frames->has_animation(edited_anim))
		return false;

	int idx = tree->get_item_at_pos(p_point, true);

	if (idx < 0 || idx >= frames->get_frame_count(edited_anim))
		return Variant();

	RES frame = frames->get_frame(edited_anim, idx);

	if (frame.is_null())
		return Variant();

	return EditorNode::get_singleton()->drag_resource(frame, p_from);
}
Example #24
0
Array Node::_get_node_and_resource(const NodePath& p_path) {

	Node *node;
	RES res;
	node = get_node_and_resource(p_path,res);
	Array result;

	if (node)
		result.push_back(node);
	else
		result.push_back(Variant());

	if (res.is_valid())
		result.push_back(res);
	else
		result.push_back(Variant());

	return result;
}
void ResourcePreloaderEditor::_update_library() {

	tree->clear();
	tree->set_hide_root(true);
	TreeItem *root = tree->create_item(NULL);

	List<StringName> rnames;
	preloader->get_resource_list(&rnames);

	List<String> names;
	for(List<StringName>::Element *E=rnames.front();E;E=E->next()) {
		names.push_back(E->get());
	}

	names.sort();

	for(List<String>::Element *E=names.front();E;E=E->next()) {

		TreeItem *ti = tree->create_item(root);
		ti->set_cell_mode(0,TreeItem::CELL_MODE_STRING);
		ti->set_editable(0,true);
		ti->set_selectable(0,true);
		ti->set_text(0,E->get());
		ti->set_metadata(0,E->get());



		RES r = preloader->get_resource(E->get());

		ERR_CONTINUE(r.is_null());

		ti->set_tooltip(0,r->get_path());
		String type = r->get_type();
		ti->set_text(1,type);
		ti->set_selectable(1,false);

		if (has_icon(type,"EditorIcons"))
			ti->set_icon( 1, get_icon(type,"EditorIcons") );

	}

	//player->add_resource("default",resource);
}
Example #26
0
uint32_t Resource::hash_edited_version() const {

	uint32_t hash = hash_djb2_one_32(get_edited_version());

	List<PropertyInfo> plist;
	get_property_list(&plist);

	for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {

		if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
			RES res = get(E->get().name);
			if (res.is_valid()) {
				hash = hash_djb2_one_32(res->hash_edited_version(), hash);
			}
		}
	}

	return hash;
}
Example #27
0
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") {
		return ERR_FILE_UNRECOGNIZED;
	}

	ResourceFormatSaverTextInstance saver;
	return saver.save(p_path,p_resource,p_flags);

}
Example #28
0
bool Node::has_node_and_resource(const NodePath& p_path) const {

	if (!has_node(p_path))
		return false;
	Node *node = get_node(p_path);

	if (p_path.get_subname_count()) {

		RES r;
		for(int j=0;j<p_path.get_subname_count();j++) {
			r = j==0 ? node->get(p_path.get_subname(j)) : r->get(p_path.get_subname(j));
			if (r.is_null())
				return false;
		}
	}


	return true;
}
Example #29
0
DVector<String> _ResourceSaver::get_recognized_extensions(const RES& p_resource) {

    ERR_FAIL_COND_V(p_resource.is_null(),DVector<String>());
    List<String> exts;
    ResourceSaver::get_recognized_extensions(p_resource,&exts);
    DVector<String> ret;
    for(List<String>::Element *E=exts.front(); E; E=E->next()) {

        ret.push_back(E->get());
    }
    return ret;
}
Example #30
0
void ResourcePreloader::_set_resources(const Array& p_data) {

	resources.clear();

	ERR_FAIL_COND(p_data.size()!=2);
	DVector<String> names=p_data[0];
	Array resdata=p_data[1];

	ERR_FAIL_COND(names.size()!=resdata.size());

	for(int i=0;i<resdata.size();i++) {

		String name=names[i];
		RES resource = resdata[i];
		ERR_CONTINUE( !resource.is_valid() );
		resources[name]=resource;

		//add_resource(name,resource);
	}

}