Esempio n. 1
0
//----------------------------------------------------------------------
DWORD MC_TextureManager::loadTexture (const char *textureFullPathName, gos_TextureFormat key, DWORD hints, DWORD uniqueInstance, DWORD nFlush)
{
	long i=0;

	//--------------------------------------
	// Is this texture already Loaded?
	for (i=0;i<MC_MAXTEXTURES;i++)
	{
		if (masterTextureNodes[i].nodeName && (stricmp(masterTextureNodes[i].nodeName,textureFullPathName) == 0))
		{
			if (uniqueInstance == masterTextureNodes[i].uniqueInstance)
			{
				masterTextureNodes[i].numUsers++;
				return(i);							//Return the texture Node Id Now.
			}
			else
			{
				//------------------------------------------------
				// Copy the texture from old Handle to a new one.
				// Return the NEW handle.
				//
				// There should be no code here!!!
			}
		}
	}

	//--------------------------------------------------
	// If we get here, texture has not been loaded yet.
	// Load it now!
	//
	// Find first empty NODE
	for (i=0;i<MC_MAXTEXTURES;i++)
	{
		if (masterTextureNodes[i].gosTextureHandle == 0xffffffff)
		{
			break;
		}
	}

	if (i == MC_MAXTEXTURES)
		STOP(("TOO Many textures in game.  We have exceeded 4096 game handles"));
		
	if (key == gos_Texture_Alpha && Environment.Renderer == 3)
	{
		key = gos_Texture_Keyed;
	}

 	//--------------------------------------------------------
	// New Method.  Just store memory footprint of texture.
	// DO NOT create GOS handle until we need it.
 	masterTextureNodes[i].gosTextureHandle = CACHED_OUT_HANDLE;
	masterTextureNodes[i].nodeName = (char *)textureStringHeap->Malloc(strlen(textureFullPathName) + 1);
	gosASSERT(masterTextureNodes[i].nodeName != NULL);

	strcpy(masterTextureNodes[i].nodeName,textureFullPathName);
	masterTextureNodes[i].numUsers = 1;
	masterTextureNodes[i].key = key;
	masterTextureNodes[i].hints = hints;
	masterTextureNodes[i].uniqueInstance = uniqueInstance;
	masterTextureNodes[i].neverFLUSH = nFlush;

	//----------------------------------------------------------------------------------------------
	// Store 0xf0000000 & fileSize in width so that cache knows to create new texture from memory.
	// This way, we never need to know anything about the texture AND we can store PMGs
	// in memory instead of TGAs which use WAY less RAM!
	File textureFile;
#ifdef _DEBUG
	long textureFileOpenResult = 
#endif
		textureFile.open(textureFullPathName);
	gosASSERT(textureFileOpenResult == NO_ERR);
	
	long txmSize = textureFile.fileSize();
	
	if (!lzBuffer1)
	{
		lzBuffer1 = (MemoryPtr)textureCacheHeap->Malloc(MAX_LZ_BUFFER_SIZE);
		gosASSERT(lzBuffer1 != NULL);
		
		lzBuffer2 = (MemoryPtr)textureCacheHeap->Malloc(MAX_LZ_BUFFER_SIZE);
		gosASSERT(lzBuffer2 != NULL);
	}
	
	//Try reading the RAW data out of the fastFile.
	// If it succeeds, we just saved a complete compress, decompress and two memcpys!!
	//
	long result = textureFile.readRAW(masterTextureNodes[i].textureData,textureCacheHeap);
	if (!result)
	{
		textureFile.read(lzBuffer1,txmSize);

		textureFile.close();

		actualTextureSize += txmSize;
		DWORD txmCompressSize = LZCompress(lzBuffer2,lzBuffer1,txmSize);
		compressedTextureSize += txmCompressSize;

		masterTextureNodes[i].textureData = (DWORD *)textureCacheHeap->Malloc(txmCompressSize);
		if (masterTextureNodes[i].textureData == NULL)
			masterTextureNodes[i].gosTextureHandle = 0;
		else
			memcpy(masterTextureNodes[i].textureData,lzBuffer2,txmCompressSize);

		masterTextureNodes[i].lzCompSize = txmCompressSize;
	}
	else
	{
		masterTextureNodes[i].lzCompSize = result;
	}

	masterTextureNodes[i].width = 0xf0000000 + txmSize;

 	//-------------------
	return(i);
}
Esempio n. 2
0
void main(int argc, char **argv)
{
	HANDLE hSearch[64];
	WIN32_FIND_DATA stSearchInfo[16];
	unsigned int dwSubdir[16];
	BOOL bDirFounded[64];
	char szPath[64][256];

	char szPathName[256];
	char szOutputName[256];
	char szSpecialCompressionPath[64][256];
	char cSpecialCompressionType[64];
	int iSpecialCompressionCount;
	unsigned int dwDir, dwItem;
	unsigned int dwDataOffset;
	BOOL bFound;
	cFile *File;
	char cDefaultCompression;

	int iOption = 0;

	DWORD dwBitSize = 256 * 1024;


	cDefaultCompression = CompLZW;

	if (argc<3){
		PrintInfo();
		return;
	}

	argv++;  iOption++;
	if (_stricmp(*argv,"t")==0 && argc>2)
	{

		argv++;
		cLZFAT *File = new cLZFAT(*argv);
		if (!File->bOpened)
		{
			printf("\nFile %s doesn't exist.",*argv);
			return;
		}
		printf("\n Testing archive: %s\n",*argv);
		File->Test(*argv);
		return;
	}

	if (argc < 4)
	{
		PrintInfo();
		return;
	}

	if (_stricmp(*argv,"x")==0)
	{

		argv++;
		cLZFAT *File = new cLZFAT(*argv);
		if (!File->bOpened)
		{
			printf("\nFile %s doesn't exist.",*argv);
			return;
		}
		printf("\n Unpacking archive: %s\n",*argv);
		argv++; File->Decompress(*argv);
		return;
	}



	if (_stricmp(*argv,"c")!=0)
	{
		PrintInfo();
		return;;
	}

	argv++;  iOption++;
	iSpecialCompressionCount = 0;
	while(*argv[0] == '-' && argc>iOption-2)
	{
		char *argvpath;

		if (_stricmp(*argv,"-none")==0)
			cDefaultCompression = CompNone;
		if (_stricmp(*argv,"-xor")==0)
			cDefaultCompression = CompXOR;
		if (_stricmp(*argv,"-lz")==0)
			cDefaultCompression = CompLZ;
		if (_stricmp(*argv,"-lzw")==0)
			cDefaultCompression = CompLZW;

		if (_strnicmp(*argv,"-dnone",6)==0)
		{
			argvpath = *argv+6;
			strcpy(szSpecialCompressionPath[iSpecialCompressionCount], argvpath);
			cSpecialCompressionType[iSpecialCompressionCount] = CompNone;
			iSpecialCompressionCount++;
			//printf("\nNone - %s",szSpecialCompressionPath[iSpecialCompressionCount-1]);
		}
		if (_strnicmp(*argv,"-dxor",5)==0)
		{
			argvpath = *argv+5;
			strcpy(szSpecialCompressionPath[iSpecialCompressionCount], argvpath);
			cSpecialCompressionType[iSpecialCompressionCount] = CompXOR;
			iSpecialCompressionCount++;
			//printf("\nXOR  - %s",szSpecialCompressionPath[iSpecialCompressionCount-1]);
		}
		if (_strnicmp(*argv,"-dlz",4)==0)
		{
			argvpath = *argv+4;
			strcpy(szSpecialCompressionPath[iSpecialCompressionCount], argvpath);
			cSpecialCompressionType[iSpecialCompressionCount] = CompLZ;
			iSpecialCompressionCount++;
			//printf("\nLZ   - %s",szSpecialCompressionPath[iSpecialCompressionCount-1]);
		}
		if (_strnicmp(*argv,"-dlzw",5)==0)
		{
			argvpath = *argv+5;
			strcpy(szSpecialCompressionPath[iSpecialCompressionCount], argvpath);
			cSpecialCompressionType[iSpecialCompressionCount] = CompLZW;
			iSpecialCompressionCount++;
			//printf("\nLZW  - %s",szSpecialCompressionPath[iSpecialCompressionCount-1]);
		}
		argv++;  iOption++;
	}

	strcpy(szOutputName,*argv);
	argv++;

	dwDir = 0;  dwItem = 0;  dwDataOffset = 0;

	sprintf(szPath[dwDir],"%s\\*",*argv);

	printf("\n Creating archive: %s\n",szOutputName);

	hSearch[dwDir] = FindFirstFile( szPath[dwDir], &stSearchInfo[dwDir] );
	FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir] );

	dwItemsCount = ItemsCount(szPath[dwDir]);
	dwItemsCount++;
	while ( 1 )
	{
		bFound = FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir]);
		if (dwDir == 0  &&  bFound == 0)
			break;

		if (bFound)
		{
			if (stSearchInfo[dwDir].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				dwDir++;
				sprintf(szPath[dwDir],"%s",szPath[dwDir-1]);
				szPath[dwDir][strlen(szPath[dwDir])-1] = 0;
				strcat(szPath[dwDir],stSearchInfo[dwDir-1].cFileName);
				strcat(szPath[dwDir], "\\*");
				hSearch[dwDir] = FindFirstFile( szPath[dwDir], &stSearchInfo[dwDir] );
				FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir] );
				dwItemsCount += ItemsCount( szPath[dwDir] );
			}
			else
			{
			}
		}
		else
		{
			FindClose( hSearch[dwDir] );
			dwDir--;
		}
	}
	FindClose( hSearch[dwDir] );

	pFAT = new stFAT[dwItemsCount];
	memset(pFAT,0,dwItemsCount*ItemSize);
	memset(bDirFounded,0,sizeof(BOOL));

	File = new cFile(szOutputName,"w");

	hSearch[dwDir] = FindFirstFile( szPath[dwDir], &stSearchInfo[dwDir] );
	FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir] );

	
	while ( 1 )
	{
		bFound = FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir]);
		if (dwDir == 0  &&  bFound == 0 && bDirFounded[0] ==1)
		{
			pFAT[dwItem].cType = -1;
			break;
		}

		if (bFound)
		{
			if ((stSearchInfo[dwDir].dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
			{
				if (bDirFounded[dwDir]==0)
				{
					pFAT[dwItem].cType = 1;
					strcpy( pFAT[dwItem].szName, stSearchInfo[dwDir].cFileName);
					pFAT[dwItem].dwSubdir = dwItem+1;
					dwItem++;
					dwSubdir[dwDir] = dwItem - 1;
					dwDir++;
					
					sprintf(szPath[dwDir],"%s",szPath[dwDir-1]);
					szPath[dwDir][strlen(szPath[dwDir])-1] = 0;
					strcat(szPath[dwDir],stSearchInfo[dwDir-1].cFileName);
					strcat(szPath[dwDir], "\\*");
					hSearch[dwDir] = FindFirstFile( szPath[dwDir], &stSearchInfo[dwDir] );
					FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir] );
					
				}
			}
			else
			{
				if (bDirFounded[dwDir]==1)
				{
					cFile *FIn;
					unsigned char *buff1,*buff2;
					unsigned long buff1size, buff2size, buffsize;
					char cThisFileCompression;
					int i;
					DWORD dw, dwi;


					cThisFileCompression = cDefaultCompression;
					strcpy(szPathName,szPath[dwDir]);
					szPathName[strlen(szPathName)-2] = 0;
					for (i=0; i<iSpecialCompressionCount; i++)
						if (_strnicmp(szSpecialCompressionPath[i],szPathName,
							strlen(szSpecialCompressionPath[i])) == 0)
							cThisFileCompression = cSpecialCompressionType[i];

					strcpy(szPathName,szPath[dwDir]);
					szPathName[strlen(szPathName)-1] = 0;
					strcat(szPathName, stSearchInfo[dwDir].cFileName );

					char *szCompTypes[] = {"None","XOR ","LZ  ","LZW "};
					printf("\n Adding [%s]  -  %-54s",szCompTypes[cThisFileCompression],szPathName);
					FIn = new cFile(szPathName,"r");
					if (!FIn->bOpened)
					{
						printf(	"\n ERROR: file %s used by other application\n  exiting ...\n",
								szPathName);
						return;
					}
					buff1size = FIn->dwSize;
					buff1 = new unsigned char[dwBitSize];
					unsigned char *buff1set = buff1;

					pFAT[dwItem].cType = 0;
					pFAT[dwItem].cCompression = cThisFileCompression;
					strcpy( pFAT[dwItem].szName, stSearchInfo[dwDir].cFileName);
					pFAT[dwItem].dwNext = dwItem+1;
					pFAT[dwItem].dwData = dwDataOffset;
					switch (cThisFileCompression)
					{
						case CompNone:
							buff2size = 0;
							dw=0; while (dw<(buff1size / dwBitSize))
							{
								FIn->Read(buff1, dwBitSize);
								File->Write(buff1, dwBitSize);
								buff2size += dwBitSize;
								dw++;
							}
							if (buff1size % dwBitSize)
							{
								buffsize = buff1size % dwBitSize;
								FIn->Read(buff1, buffsize);
								File->Write(buff1, buffsize);
								buff2size += buffsize;
							}
							pFAT[dwItem].dwSize = buff1size;
							pFAT[dwItem].dwCompressedSize = buff2size;	//size file on disk
							dwDataOffset += buff2size;
							break;

						case CompXOR:
							buff2size = 0;
							dw=0; while (dw<(buff1size / dwBitSize))
							{
								FIn->Read(buff1, dwBitSize);

								buff2 = buff1;  dwi = dwBitSize;								
								while(dwi--) *buff2++ ^= dwi;

								File->Write(buff1, dwBitSize);
								buff2size += dwBitSize;
								dw++;
							}
							if (buff1size % dwBitSize)
							{
								buffsize = buff1size % dwBitSize;
								FIn->Read(buff1, buffsize);
								
								buff2 = buff1;  dwi = buffsize;								
								while(dwi--) *buff2++ ^= dwi;
								
								File->Write(buff1, buffsize);
								buff2size += buffsize;
							}
							pFAT[dwItem].dwSize = buff1size;
							pFAT[dwItem].dwCompressedSize = buff2size;	//size file on disk
							dwDataOffset += buff2size;
							break;

						case CompLZ:
							buff2size = 0;
							buffsize = dwBitSize + 256;
							buff2 = new unsigned char[buffsize];
							dw=0; while (dw<(buff1size / dwBitSize))
							{
								FIn->Read(buff1, dwBitSize);
								buffsize = dwBitSize + 256;
								LZCompress(buff1,dwBitSize, buff2, &buffsize);
								
								File->Write(&buffsize, 4);
								File->Write(buff2, buffsize);
								buff2size += buffsize+4;
								dw++;
							}
							if (buff1size % dwBitSize)
							{								
								FIn->Read(buff1, buff1size % dwBitSize);
								buffsize = dwBitSize + 256;
								LZCompress(buff1,buff1size % dwBitSize, buff2, &buffsize);

								File->Write(&buffsize,4);
								File->Write(buff2, buffsize);
								buff2size += buffsize+4;
							}
							pFAT[dwItem].dwSize = buff1size;
							pFAT[dwItem].dwCompressedSize = buff2size;	//size file on disk
							dwDataOffset += buff2size;

							delete buff2;
							break;

						case CompLZW:
							buff2size = 0;
							buffsize = (unsigned long) ( (float)dwBitSize * 1.1 );
							buff2 = new unsigned char[buffsize];
							dw=0; while (dw<(buff1size / dwBitSize))
							{
								FIn->Read(buff1, dwBitSize);
								buffsize = (unsigned long) ( (float)dwBitSize * 1.1 );
								LZWCompress(buff2, &buffsize, buff1,dwBitSize,9);
								
								File->Write(&buffsize, 4);
								File->Write(buff2, buffsize);
								buff2size += buffsize+4;
								dw++;
							}
							if (buff1size % dwBitSize)
							{								
								FIn->Read(buff1, buff1size % dwBitSize);
								buffsize = (unsigned long) ( (float)dwBitSize * 1.1 );
								LZWCompress(buff2, &buffsize, buff1,buff1size % dwBitSize, 9);

								File->Write(&buffsize,4);
								File->Write(buff2, buffsize);
								buff2size += buffsize+4;
							}
							pFAT[dwItem].dwSize = buff1size;
							pFAT[dwItem].dwCompressedSize = buff2size;	//size file on disk
							dwDataOffset += buff2size;

							delete buff2;
							break;

						default:
							break;
					}

					delete buff1set;
					delete FIn;
					printf("    OK");
					dwItem++;
				}
			}
		}
		else
		{
			if (bDirFounded[dwDir] == 0)
			{
				bDirFounded[dwDir] = 1;
				FindClose(hSearch[dwDir]);
				hSearch[dwDir] = FindFirstFile( szPath[dwDir], &stSearchInfo[dwDir] );
				FindNextFile( hSearch[dwDir], &stSearchInfo[dwDir] );				
			}
			else
			{
				bDirFounded[dwDir] = 0;

				pFAT[dwItem].cType = -1;
				dwItem++;

				FindClose( hSearch[dwDir] );

				dwDir--;
				pFAT[ dwSubdir[dwDir] ].dwNext = dwItem;
			}
		}
	}
	FindClose( hSearch[dwDir] );


	unsigned int i,j;
	unsigned long *pFATItemTrueSize;
	pFATItemTrueSize = new unsigned long[dwItemsCount+1];
	pFATItemTrueSize[0] = 0;
	j = 0;
	for (i=0; i<dwItemsCount; i++)
	{
		if (pFAT[i].cType == -1)
			j +=  1;
		else
			j += 19+strlen(pFAT[i].szName);
		pFATItemTrueSize[i+1] = j;
	}
	unsigned char *pFATTrue,*p;
	pFATTrue = new unsigned char[pFATItemTrueSize[dwItemsCount]];
	p = pFATTrue;
	for (i=0; i<dwItemsCount; i++)
	{
		pFAT[i].dwNext = pFATItemTrueSize[pFAT[i].dwNext];
		if (pFAT[i].cType == 1)
			pFAT[i].dwSubdir = pFATItemTrueSize[pFAT[i].dwSubdir];

		memcpy(p,&pFAT[i],pFATItemTrueSize[i+1]-pFATItemTrueSize[i]);
		p = pFATTrue + pFATItemTrueSize[i+1];
	}
	unsigned char *pCompFATTrue;
	unsigned long dwCompFATSize;

	dwCompFATSize = (int)((float)pFATItemTrueSize[dwItemsCount]*1.01)+512;
	pCompFATTrue = new unsigned char [dwCompFATSize];

	LZWCompress(pCompFATTrue,&dwCompFATSize,pFATTrue,pFATItemTrueSize[dwItemsCount],9);
	File->Write(pCompFATTrue,dwCompFATSize);
	File->Write(&pFATItemTrueSize[dwItemsCount],4);
	File->Write(&dwCompFATSize,4);

	printf("\n");
	delete pCompFATTrue;
	delete pFATTrue;
	delete pFATItemTrueSize;
	delete File;

	delete pFAT;
}
Esempio n. 3
0
//----------------------------------------------------------------------
DWORD MC_TextureManager::textureFromMemory (DWORD *data, gos_TextureFormat key, DWORD hints, DWORD width, DWORD bitDepth)
{
	long i=0;

	//--------------------------------------------------------
	// If we called this, we KNOW the texture is NOT loaded!
	//
	// Find first empty NODE
	for (i=0;i<MC_MAXTEXTURES;i++)
	{
		if (masterTextureNodes[i].gosTextureHandle == 0xffffffff)
		{
			break;
		}
	}

	if (i == MC_MAXTEXTURES)
		STOP(("TOO Many textures in game.  We have exceeded 4096 game handles"));
		
	//--------------------------------------------------------
	// New Method.  Just store memory footprint of texture.
	// DO NOT create GOS handle until we need it.
 	masterTextureNodes[i].gosTextureHandle = CACHED_OUT_HANDLE;
	masterTextureNodes[i].nodeName = NULL;

	masterTextureNodes[i].numUsers = 1;
	masterTextureNodes[i].key = key;
	masterTextureNodes[i].hints = hints;

	//------------------------------------------
	// Find and store the width.
	masterTextureNodes[i].width = width;
	long txmSize = width * width * bitDepth;
	
	if (!lzBuffer1)
	{
		lzBuffer1 = (MemoryPtr)textureCacheHeap->Malloc(MAX_LZ_BUFFER_SIZE);
		gosASSERT(lzBuffer1 != NULL);
		
		lzBuffer2 = (MemoryPtr)textureCacheHeap->Malloc(MAX_LZ_BUFFER_SIZE);
		gosASSERT(lzBuffer2 != NULL);
	}
	
	actualTextureSize += txmSize;
	DWORD txmCompressSize = LZCompress(lzBuffer2,(MemoryPtr)data,txmSize);
	compressedTextureSize += txmCompressSize;
	
 	//-------------------------------------------------------
	// Create a block of cache memory to hold this texture.
	if (!masterTextureNodes[i].textureData )
		masterTextureNodes[i].textureData = (DWORD *)textureCacheHeap->Malloc(txmCompressSize);
	
	//No More RAM.  Do not display this texture anymore.
	if (masterTextureNodes[i].textureData == NULL)
		masterTextureNodes[i].gosTextureHandle = 0;
	else
	{
		memcpy(masterTextureNodes[i].textureData,lzBuffer2,txmCompressSize);
		masterTextureNodes[i].lzCompSize = txmCompressSize;
	}
	
	//------------------	
	return(i);
}