示例#1
0
int BIFImporter::OpenArchive(const char* path)
{
	if (stream) {
		delete( stream );
		stream = NULL;
	}

	char filename[_MAX_PATH];
	ExtractFileFromPath(filename, path);

	char cachePath[_MAX_PATH];
	PathJoin(cachePath, core->CachePath, filename, NULL);
	stream = FileStream::OpenFile(cachePath);

	char Signature[8];
	if (!stream) {
		FileStream* file = FileStream::OpenFile(path);
		if (!file) {
			return GEM_ERROR;
		}
		if (file->Read(Signature, 8) == GEM_ERROR) {
			delete file;
			return GEM_ERROR;
		}

		if (strncmp(Signature, "BIF V1.0", 8) == 0) {
			stream = DecompressBIF(file, cachePath);
			delete file;
		} else if (strncmp(Signature, "BIFCV1.0", 8) == 0) {
			stream = DecompressBIFC(file, cachePath);
			delete file;
		} else if (strncmp( Signature, "BIFFV1  ", 8 ) == 0) {
			file->Seek(0, GEM_STREAM_START);
			stream = file;
		} else {
			delete file;
			return GEM_ERROR;
		}
	}

	if (!stream)
		return GEM_ERROR;

	stream->Read( Signature, 8 );

	if (strncmp( Signature, "BIFFV1  ", 8 ) != 0) {
		return GEM_ERROR;
	}

	ReadBIF();
	return GEM_OK;
}
int BIFImporter::OpenArchive(const char* filename)
{
	if (stream) {
		delete( stream );
		stream = NULL;
	}
	FILE* in_cache = fopen( filename, "rb" );
	if( !in_cache) {
		return GEM_ERROR;
	}
	char Signature[8];
	if (fread( &Signature, 1, 8, in_cache ) != 8) {
		fclose ( in_cache );
		return GEM_ERROR;
	}
	fclose( in_cache );
	//normal bif, not in cache
	if (strncmp( Signature, "BIFFV1  ", 8 ) == 0) {
		stream = new CachedFileStream( filename );
		stream->Read( Signature, 8 );
		strcpy( path, filename );
		ReadBIF();
		return GEM_OK;
	}
	//not found as normal bif
	//checking compression type
	FileStream* compressed = new FileStream();
	compressed->Open( filename, true );
	compressed->Read( Signature, 8 );
	if (strncmp( Signature, "BIF V1.0", 8 ) == 0) {
		ieDword fnlen, complen, declen;
		compressed->ReadDword( &fnlen );
		char* fname = ( char* ) malloc( fnlen );
		compressed->Read( fname, fnlen );
		strlwr(fname);
		compressed->ReadDword( &declen );
		compressed->ReadDword( &complen );
		PathJoin( path, core->CachePath, fname, NULL );
		free( fname );
		in_cache = fopen( path, "rb" );
		if (in_cache) {
			//printf("Found in Cache\n");
			fclose( in_cache );
			delete( compressed );
			stream = new CachedFileStream( path );
			stream->Read( Signature, 8 );
			if (strncmp( Signature, "BIFFV1  ", 8 ) == 0)
				ReadBIF();
			else
				return GEM_ERROR;
			return GEM_OK;
		}
		printf( "Decompressing\n" );
		if (!core->IsAvailable( PLUGIN_COMPRESSION_ZLIB )) {
			printMessage("BIFImporter", "No Compression Manager Available.", RED);
			return GEM_ERROR;
		}
		in_cache = fopen( path, "wb" );
		if (!in_cache) {
			printMessage("BIFImporter", " ", RED);
			printf( "Cannot write %s.\n", path );
			return GEM_ERROR;
		}
		PluginHolder<Compressor> comp(PLUGIN_COMPRESSION_ZLIB);
		if (comp->Decompress( in_cache, compressed, complen ) != GEM_OK) {
			return GEM_ERROR;
		}
		fclose( in_cache );
		delete( compressed );
		stream = new CachedFileStream( path );
		stream->Read( Signature, 8 );
		if (strncmp( Signature, "BIFFV1  ", 8 ) == 0)
			ReadBIF();
		else
			return GEM_ERROR;
		return GEM_OK;
	}

	if (strncmp( Signature, "BIFCV1.0", 8 ) == 0) {
		//printf("'BIFCV1.0' Compressed File Found\n");
		PathJoin( path, core->CachePath, compressed->filename, NULL );
		in_cache = fopen( path, "rb" );
		if (in_cache) {
			//printf("Found in Cache\n");
			fclose( in_cache );
			delete( compressed );
			stream = new CachedFileStream( path );
			stream->Read( Signature, 8 );
			if (strncmp( Signature, "BIFFV1  ", 8 ) == 0) {
				ReadBIF();				
			} else
				return GEM_ERROR;
			return GEM_OK;
		}
		printf( "Decompressing\n" );
		if (!core->IsAvailable( PLUGIN_COMPRESSION_ZLIB ))
			return GEM_ERROR;
		PluginHolder<Compressor> comp(PLUGIN_COMPRESSION_ZLIB);
		ieDword unCompBifSize;
		compressed->ReadDword( &unCompBifSize );
		printf( "\nDecompressing file: [..........]" );
		fflush(stdout);
		in_cache = fopen( path, "wb" );
		if (!in_cache) {
			printMessage("BIFImporter", " ", RED);
			printf( "Cannot write %s.\n", path );	
			return GEM_ERROR;
		}
		ieDword finalsize = 0;
		int laststep = 0;
		while (finalsize < unCompBifSize) {
			ieDword complen, declen;
			compressed->ReadDword( &declen );
			compressed->ReadDword( &complen );
			if (comp->Decompress( in_cache, compressed, complen ) != GEM_OK) {
				return GEM_ERROR;
			}
			finalsize = ftell( in_cache );
			if (( int ) ( finalsize * ( 10.0 / unCompBifSize ) ) != laststep) {
				laststep++;
				printf( "\b\b\b\b\b\b\b\b\b\b\b" );
				int l;

				for (l = 0; l < laststep; l++)
					printf( "|" );
				for (; l < 10; l++)//l starts from laststep
					printf( "." );
				printf( "]" );
				fflush(stdout);
			}
		}
		printf( "\n" );
		fclose( in_cache );
		delete( compressed );
		stream = new CachedFileStream( path );
		stream->Read( Signature, 8 );
		if (strncmp( Signature, "BIFFV1  ", 8 ) == 0)
			ReadBIF();
		else
			return GEM_ERROR;
		return GEM_OK;
	}
	delete (compressed);
	return GEM_ERROR;
}