int AudioStreamPlaybackOGGVorbis::_ov_seek_func(void *_f, ogg_int64_t offs, int whence) {

	//printf("seek to %p, offs %i, whence %i\n",_f,(int)offs,whence);

#ifdef SEEK_SET
	//printf("seek set defined\n");
	FileAccess *fa = (FileAccess *)_f;

	if (whence == SEEK_SET) {

		fa->seek(offs);
	} else if (whence == SEEK_CUR) {

		fa->seek(fa->get_position() + offs);
	} else if (whence == SEEK_END) {

		fa->seek_end(offs);
	} else {

		ERR_PRINT("Vorbis seek function failure: Unexpected value in _whence\n");
	}
	int ret = fa->eof_reached() ? -1 : 0;
	//printf("returning %i\n",ret);
	return ret;

#else
	return -1; // no seeking
#endif
}
	//! position the source pointer to byte_index from the start of the source
	virtual void seek(unsigned long byte_index) {

		if (!fa)
			return;

		fa->seek(byte_index);
	};
int64_t GDAPI godot_videodecoder_file_seek(void *ptr, int64_t pos, int whence) {
	// file
	FileAccess *file = reinterpret_cast<FileAccess *>(ptr);

	if (file) {
		size_t len = file->get_len();
		switch (whence) {
			case SEEK_SET: {
				// Just for explicitness
				size_t new_pos = static_cast<size_t>(pos);
				if (new_pos > len) {
					return -1;
				}
				file->seek(new_pos);
				pos = static_cast<int64_t>(file->get_position());
				return pos;
			} break;
			case SEEK_CUR: {
				// Just in case it doesn't exist
				if (pos < 0 && (size_t)-pos > file->get_position()) {
					return -1;
				}
				pos = pos + static_cast<int>(file->get_position());
				file->seek(pos);
				pos = static_cast<int64_t>(file->get_position());
				return pos;
			} break;
			case SEEK_END: {
				// Just in case something goes wrong
				if ((size_t)-pos > len) {
					return -1;
				}
				file->seek_end(pos);
				pos = static_cast<int64_t>(file->get_position());
				return pos;
			} break;
			default: {
				// Only 4 possible options, hence default = AVSEEK_SIZE
				// Asks to return the length of file
				return static_cast<int64_t>(len);
			} break;
		}
	}
	// In case nothing works out.
	return -1;
}
Exemple #4
0
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 ResourceFormatLoaderBinary::load_import_metadata(const String &p_path, Ref<ResourceImportMetadata>& r_var) const {


	FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
	if (!f) {
		return ERR_FILE_CANT_OPEN;
	}

	Ref<ResourceInteractiveLoaderBinary> ria = memnew( ResourceInteractiveLoaderBinary );
	ria->local_path=Globals::get_singleton()->localize_path(p_path);
	ria->res_path=ria->local_path;
//	ria->set_local_path( Globals::get_singleton()->localize_path(p_path) );
	ria->recognize(f);
	if(ria->error!=OK)
		return ERR_FILE_UNRECOGNIZED;
	f=ria->f;
	uint64_t imp_ofs = f->get_64();

	if (imp_ofs==0)
		return ERR_UNAVAILABLE;

	f->seek(imp_ofs);
	Ref<ResourceImportMetadata> imd = memnew( ResourceImportMetadata );
	imd->set_editor(ria->get_unicode_string());
	int sc = f->get_32();
	for(int i=0;i<sc;i++) {

		String src = ria->get_unicode_string();
		String md5 = ria->get_unicode_string();
		imd->add_source(src,md5);
	}
	int pc = f->get_32();

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

		String name = ria->get_unicode_string();
		Variant val;
		ria->parse_variant(val);
		imd->set_option(name,val);
	}

	r_var=imd;

	return OK;

}
Exemple #6
0
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;
}
Exemple #7
0
static long godot_seek(voidpf opaque, voidpf stream, uLong offset, int origin) {

	FileAccess* f = (FileAccess*)opaque;

	int pos = offset;
	switch (origin) {

	case ZLIB_FILEFUNC_SEEK_CUR:
		pos = f->get_pos() + offset;
		break;
	case ZLIB_FILEFUNC_SEEK_END:
		pos = f->get_len() + offset;
		break;
	default:
		break;
	};

	f->seek(pos);
	return 0;
};
Exemple #8
0
Error StreamTexture::_load_data(const String &p_path, int &tw, int &th, int &flags, Image &image, int p_size_limit) {

	FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
	ERR_FAIL_COND_V(!f, ERR_CANT_OPEN);

	uint8_t header[4];
	f->get_buffer(header, 4);
	if (header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T') {
		memdelete(f);
		ERR_FAIL_COND_V(header[0] != 'G' || header[1] != 'D' || header[2] != 'S' || header[3] != 'T', ERR_FILE_CORRUPT);
	}

	tw = f->get_32();
	th = f->get_32();
	flags = f->get_32(); //texture flags!
	uint32_t df = f->get_32(); //data format

	print_line("width: " + itos(tw));
	print_line("height: " + itos(th));
	print_line("flags: " + itos(flags));
	print_line("df: " + itos(df));

	if (request_3d_callback && df & FORMAT_BIT_DETECT_3D) {
		print_line("request detect 3D at " + p_path);
		VS::get_singleton()->texture_set_detect_3d_callback(texture, _requested_3d, this);
	} else {
		print_line("not requesting detect 3D at " + p_path);
		VS::get_singleton()->texture_set_detect_3d_callback(texture, NULL, NULL);
	}

	if (request_srgb_callback && df & FORMAT_BIT_DETECT_SRGB) {
		print_line("request detect srgb at " + p_path);
		VS::get_singleton()->texture_set_detect_srgb_callback(texture, _requested_srgb, this);
	} else {
		VS::get_singleton()->texture_set_detect_srgb_callback(texture, NULL, NULL);
		print_line("not requesting detect srgb at " + p_path);
	}

	if (!(df & FORMAT_BIT_STREAM)) {
		p_size_limit = 0;
	}

	if (df & FORMAT_BIT_LOSSLESS || df & FORMAT_BIT_LOSSY) {
		//look for a PNG or WEBP file inside

		int sw = tw;
		int sh = th;

		uint32_t mipmaps = f->get_32();
		uint32_t size = f->get_32();

		print_line("mipmaps: " + itos(mipmaps));

		while (mipmaps > 1 && p_size_limit > 0 && (sw > p_size_limit || sh > p_size_limit)) {

			f->seek(f->get_pos() + size);
			mipmaps = f->get_32();
			size = f->get_32();

			sw = MAX(sw >> 1, 1);
			sh = MAX(sh >> 1, 1);
			mipmaps--;
		}

		//mipmaps need to be read independently, they will be later combined
		Vector<Image> mipmap_images;
		int total_size = 0;

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

			if (i > 0) {
				size = f->get_32();
			}

			PoolVector<uint8_t> pv;
			pv.resize(size);
			{
				PoolVector<uint8_t>::Write w = pv.write();
				f->get_buffer(w.ptr(), size);
			}

			Image img;
			if (df & FORMAT_BIT_LOSSLESS) {
				img = Image::lossless_unpacker(pv);
			} else {
				img = Image::lossy_unpacker(pv);
			}

			if (img.empty()) {
				memdelete(f);
				ERR_FAIL_COND_V(img.empty(), ERR_FILE_CORRUPT);
			}
			total_size += img.get_data().size();

			mipmap_images.push_back(img);
		}

		print_line("mipmap read total: " + itos(mipmap_images.size()));

		memdelete(f); //no longer needed

		if (mipmap_images.size() == 1) {

			image = mipmap_images[0];
			return OK;

		} else {
			PoolVector<uint8_t> img_data;
			img_data.resize(total_size);

			{
				PoolVector<uint8_t>::Write w = img_data.write();

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

					PoolVector<uint8_t> id = mipmap_images[i].get_data();
					int len = id.size();
					PoolVector<uint8_t>::Read r = id.read();
					copymem(&w[ofs], r.ptr(), len);
					ofs += len;
				}
			}

			image = Image(sw, sh, true, mipmap_images[0].get_format(), img_data);
			return OK;
		}

	} else {
bool PackedSourcePCK::try_open_pack(const String& p_path) {

	FileAccess *f = FileAccess::open(p_path,FileAccess::READ);
	if (!f)
		return false;

	uint32_t magic= f->get_32();

	if (magic != 0x4b435047) {
		//maybe at he end.... self contained exe
		f->seek_end();
		f->seek( f->get_pos() -4 );
		magic = f->get_32();
		if (magic != 0x4b435047) {

			memdelete(f);
			return false;
		}
		f->seek( f->get_pos() -12 );


		uint64_t ds = f->get_64();
		f->seek( f->get_pos() -ds-8 );

		magic = f->get_32();
		if (magic != 0x4b435047) {

			memdelete(f);
			return false;
		}

	}

	uint32_t ver_major = f->get_32();
	uint32_t ver_minor = f->get_32();
	uint32_t ver_rev = f->get_32();

	ERR_EXPLAIN("Pack created with a newer version of the engine: "+itos(ver_major)+"."+itos(ver_minor)+"."+itos(ver_rev));
	ERR_FAIL_COND_V( ver_major > VERSION_MAJOR || (ver_major == VERSION_MAJOR && ver_minor > VERSION_MINOR), ERR_INVALID_DATA);

	for(int i=0;i<16;i++) {
		//reserved
		f->get_32();
	}

	int file_count = f->get_32();

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

		uint32_t sl = f->get_32();
		CharString cs;
		cs.resize(sl+1);
		f->get_buffer((uint8_t*)cs.ptr(),sl);
		cs[sl]=0;

		String path;
		path.parse_utf8(cs.ptr());

		uint64_t ofs = f->get_64();
		uint64_t size = f->get_64();
		uint8_t md5[16];
		f->get_buffer(md5,16);

		PackedData::get_singleton()->add_path(p_path, path, ofs, size, md5,this);
	};

	return true;
};