コード例 #1
0
ファイル: godotsharp_export.cpp プロジェクト: Paulloz/godot
bool GodotSharpExport::_add_file(const String &p_src_path, const String &p_dst_path, bool p_remap) {

	FileAccessRef f = FileAccess::open(p_src_path, FileAccess::READ);
	ERR_FAIL_COND_V(!f, false);

	Vector<uint8_t> data;
	data.resize(f->get_len());
	f->get_buffer(data.ptrw(), data.size());

	add_file(p_dst_path, data, p_remap);

	return true;
}
コード例 #2
0
ファイル: power_x11.cpp プロジェクト: 93i/godot
bool PowerX11::read_power_file(const char *base, const char *node, const char *key, char *buf, size_t buflen) {
	ssize_t br = 0;
	FileAccessRef fd = open_power_file(base, node, key);
	if (!fd) {
		return false;
	}
	br = fd->get_buffer(reinterpret_cast<uint8_t *>(buf), buflen - 1);
	fd->close();
	if (br < 0) {
		return false;
	}
	buf[br] = '\0'; // null-terminate the string
	return true;
}
コード例 #3
0
ファイル: power_x11.cpp プロジェクト: 93i/godot
/* http://lxr.linux.no/linux+v2.6.29/drivers/char/apm-emulation.c */
bool PowerX11::GetPowerInfo_Linux_proc_apm() {
	bool need_details = false;
	int ac_status = 0;
	int battery_status = 0;
	int battery_flag = 0;
	int battery_percent = 0;
	int battery_time = 0;
	FileAccessRef fd = FileAccess::open(proc_apm_path, FileAccess::READ);
	char buf[128];
	char *ptr = &buf[0];
	char *str = NULL;
	ssize_t br;

	if (!fd) {
		return false; /* can't use this interface. */
	}

	br = fd->get_buffer(reinterpret_cast<uint8_t *>(buf), sizeof(buf) - 1);
	fd->close();

	if (br < 0) {
		return false;
	}

	buf[br] = '\0'; /* null-terminate the string. */
	if (!next_string(&ptr, &str)) { /* driver version */
		return false;
	}
	if (!next_string(&ptr, &str)) { /* BIOS version */
		return false;
	}
	if (!next_string(&ptr, &str)) { /* APM flags */
		return false;
	}

	if (!next_string(&ptr, &str)) { /* AC line status */
		return false;
	} else if (!int_string(str, &ac_status)) {
		return false;
	}

	if (!next_string(&ptr, &str)) { /* battery status */
		return false;
	} else if (!int_string(str, &battery_status)) {
		return false;
	}
	if (!next_string(&ptr, &str)) { /* battery flag */
		return false;
	} else if (!int_string(str, &battery_flag)) {
		return false;
	}
	if (!next_string(&ptr, &str)) { /* remaining battery life percent */
		return false;
	}
	String sstr = str;
	if (sstr[sstr.length() - 1] == '%') {
		sstr[sstr.length() - 1] = '\0';
	}
	if (!int_string(str, &battery_percent)) {
		return false;
	}

	if (!next_string(&ptr, &str)) { /* remaining battery life time */
		return false;
	} else if (!int_string(str, &battery_time)) {
		return false;
	}

	if (!next_string(&ptr, &str)) { /* remaining battery life time units */
		return false;
	} else if (String(str) == "min") {
		battery_time *= 60;
	}

	if (battery_flag == 0xFF) { /* unknown state */
		this->power_state = OS::POWERSTATE_UNKNOWN;
	} else if (battery_flag & (1 << 7)) { /* no battery */
		this->power_state = OS::POWERSTATE_NO_BATTERY;
	} else if (battery_flag & (1 << 3)) { /* charging */
		this->power_state = OS::POWERSTATE_CHARGING;
		need_details = true;
	} else if (ac_status == 1) {
		this->power_state = OS::POWERSTATE_CHARGED; /* on AC, not charging. */
		need_details = true;
	} else {
		this->power_state = OS::POWERSTATE_ON_BATTERY;
		need_details = true;
	}

	this->percent_left = -1;
	this->nsecs_left = -1;
	if (need_details) {
		const int pct = battery_percent;
		const int secs = battery_time;

		if (pct >= 0) { /* -1 == unknown */
			this->percent_left = (pct > 100) ? 100 : pct; /* clamp between 0%, 100% */
		}
		if (secs >= 0) { /* -1 == unknown */
			this->nsecs_left = secs;
		}
	}

	return true;
}
コード例 #4
0
Vector<uint8_t> EditorTextureImportPlugin::custom_export(const String& p_path, const Ref<EditorExportPlatform> &p_platform) {


	Ref<ResourceImportMetadata> rimd = ResourceLoader::load_import_metadata(p_path);

	if (rimd.is_null()) {

		StringName group = EditorImportExport::get_singleton()->image_get_export_group(p_path);

		if (group!=StringName()) {
			//handled by export group
			rimd = Ref<ResourceImportMetadata>( memnew( ResourceImportMetadata ) );

			int group_format=0;
			float group_lossy_quality=EditorImportExport::get_singleton()->image_export_group_get_lossy_quality(group);
			int group_shrink=EditorImportExport::get_singleton()->image_export_group_get_shrink(group);
			group_shrink*=EditorImportExport::get_singleton()->get_export_image_shrink();

			switch(EditorImportExport::get_singleton()->image_export_group_get_image_action(group)) {
				case EditorImportExport::IMAGE_ACTION_NONE: {

					switch(EditorImportExport::get_singleton()->get_export_image_action()) {
						case EditorImportExport::IMAGE_ACTION_NONE: {

							group_format=EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSLESS; //?

						} break; //use default
						case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: {
							group_format=EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY;
						} break; //use default
						case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: {
							group_format=EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM;
						} break; //use default
					}

					group_lossy_quality=EditorImportExport::get_singleton()->get_export_image_quality();

				} break; //use default
				case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: {
					group_format=EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_DISK_LOSSY;
				} break; //use default
				case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: {
					group_format=EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM;
				} break; //use default
			}


			int flags=0;

			if (Globals::get_singleton()->get("texture_import/filter"))
				flags|=IMAGE_FLAG_FILTER;
			if (!Globals::get_singleton()->get("texture_import/gen_mipmaps"))
				flags|=IMAGE_FLAG_NO_MIPMAPS;
			if (!Globals::get_singleton()->get("texture_import/repeat"))
				flags|=IMAGE_FLAG_REPEAT;

			flags|=IMAGE_FLAG_FIX_BORDER_ALPHA;

			print_line("group format"+itos(group_format));
			rimd->set_option("format",group_format);
			rimd->set_option("flags",flags);
			rimd->set_option("quality",group_lossy_quality);
			rimd->set_option("atlas",false);
			rimd->set_option("shrink",group_shrink);
			rimd->add_source(EditorImportPlugin::validate_source_path(p_path));

		} else if (EditorImportExport::get_singleton()->get_image_formats().has(p_path.extension().to_lower()) && EditorImportExport::get_singleton()->get_export_image_action()!=EditorImportExport::IMAGE_ACTION_NONE) {
			//handled by general image export settings

			rimd = Ref<ResourceImportMetadata>( memnew( ResourceImportMetadata ) );

			switch(EditorImportExport::get_singleton()->get_export_image_action()) {
				case EditorImportExport::IMAGE_ACTION_COMPRESS_DISK: rimd->set_option("format",IMAGE_FORMAT_COMPRESS_DISK_LOSSY); break;
				case EditorImportExport::IMAGE_ACTION_COMPRESS_RAM: rimd->set_option("format",IMAGE_FORMAT_COMPRESS_RAM); break;
			}

			int flags=0;

			if (Globals::get_singleton()->get("texture_import/filter"))
				flags|=IMAGE_FLAG_FILTER;
			if (!Globals::get_singleton()->get("texture_import/gen_mipmaps"))
				flags|=IMAGE_FLAG_NO_MIPMAPS;
			if (!Globals::get_singleton()->get("texture_import/repeat"))
				flags|=IMAGE_FLAG_REPEAT;

			flags|=IMAGE_FLAG_FIX_BORDER_ALPHA;

			rimd->set_option("shrink",EditorImportExport::get_singleton()->get_export_image_shrink());
			rimd->set_option("flags",flags);
			rimd->set_option("quality",EditorImportExport::get_singleton()->get_export_image_quality());
			rimd->set_option("atlas",false);
			rimd->add_source(EditorImportPlugin::validate_source_path(p_path));

		} else {
			return Vector<uint8_t>();
		}
	}

	int fmt = rimd->get_option("format");

	if (fmt!=IMAGE_FORMAT_COMPRESS_RAM && fmt!=IMAGE_FORMAT_COMPRESS_DISK_LOSSY)  {
		print_line("no compress ram or lossy");
		return Vector<uint8_t>(); //pointless to do anything, since no need to reconvert
	}

	uint32_t flags = rimd->get_option("flags");
	uint8_t shrink = rimd->has_option("shrink") ? rimd->get_option("shrink"): Variant(1);
	uint8_t format = rimd->get_option("format");
	uint8_t comp = (format==EditorTextureImportPlugin::IMAGE_FORMAT_COMPRESS_RAM)?uint8_t(p_platform->get_image_compression()):uint8_t(255);

	MD5_CTX ctx;
	uint8_t f4[4];
	encode_uint32(flags,&f4[0]);
	MD5Init(&ctx);
	String gp = Globals::get_singleton()->globalize_path(p_path);
	CharString cs = gp.utf8();
	MD5Update(&ctx,(unsigned char*)cs.get_data(),cs.length());
	MD5Update(&ctx,f4,4);
	MD5Update(&ctx,&format,1);
	MD5Update(&ctx,&comp,1);
	MD5Update(&ctx,&shrink,1);
	MD5Final(&ctx);

	uint64_t sd=0;
	String smd5;

	String md5 = String::md5(ctx.digest);

	String tmp_path = EditorSettings::get_singleton()->get_settings_path().plus_file("tmp/");

	bool valid=false;
	{
		//if existing, make sure it's valid
		FileAccessRef f = FileAccess::open(tmp_path+"imgexp-"+md5+".txt",FileAccess::READ);
		if (f) {

			uint64_t d = f->get_line().strip_edges().to_int64();
			sd = FileAccess::get_modified_time(p_path);

			if (d==sd) {
				valid=true;
			} else {
				String cmd5 = f->get_line().strip_edges();
				smd5 = FileAccess::get_md5(p_path);
				if (cmd5==smd5) {
					valid=true;
				}
			}


		}
	}

	if (!valid) {
		//cache failed, convert
		Error err = import2(tmp_path+"imgexp-"+md5+".tex",rimd,p_platform->get_image_compression(),true);
		ERR_FAIL_COND_V(err!=OK,Vector<uint8_t>());
		FileAccessRef f = FileAccess::open(tmp_path+"imgexp-"+md5+".txt",FileAccess::WRITE);

		if (sd==0)
			sd = FileAccess::get_modified_time(p_path);
		if (smd5==String())
			smd5 = FileAccess::get_md5(p_path);

		f->store_line(String::num(sd));
		f->store_line(smd5);
		f->store_line(gp); //source path for reference
	}


	Vector<uint8_t> ret;
	FileAccessRef f = FileAccess::open(tmp_path+"imgexp-"+md5+".tex",FileAccess::READ);
	ERR_FAIL_COND_V(!f,ret);

	ret.resize(f->get_len());
	f->get_buffer(ret.ptr(),ret.size());

	return ret;
}