//---------------------------------------------------------------------- long MC_TextureManager::saveTexture (DWORD textureIndex, const char *textureFullPathName) { if ((MC_MAXTEXTURES <= textureIndex) || (NULL == masterTextureNodes[textureIndex].textureData)) { return (~NO_ERR); } File textureFile; long textureFileOpenResult = textureFile.create(textureFullPathName); if (NO_ERR != textureFileOpenResult) { textureFile.close(); return textureFileOpenResult; } { if (masterTextureNodes[textureIndex].width == 0) { textureFile.close(); return (~NO_ERR); //These faces have no texture!! } { //------------------------------------------ // Badboys are now LZ Compressed in texture cache. long origSize = LZDecomp(MC_TextureManager::lzBuffer2,(MemoryPtr)masterTextureNodes[textureIndex].textureData,masterTextureNodes[textureIndex].lzCompSize); if (origSize != (long)(masterTextureNodes[textureIndex].width & 0x0fffffff)) STOP(("Decompressed to different size from original! Txm:%s Width:%d DecompSize:%d",masterTextureNodes[textureIndex].nodeName,(masterTextureNodes[textureIndex].width & 0x0fffffff),origSize)); if (origSize >= MAX_LZ_BUFFER_SIZE) STOP(("Texture TOO large: %s",masterTextureNodes[textureIndex].nodeName)); textureFile.write(MC_TextureManager::lzBuffer2, origSize); } textureFile.close(); } return NO_ERR; }
//--------------------------------------------------------------------------- int32_t PacketFile::readPacket(int32_t packet, puint8_t buffer) { int32_t result = 0; if((packet == -1) || (packet == currentPacket) || (seekPacket(packet) == NO_ERROR)) { if((getStorageType() == STORAGE_TYPE_RAW) || (getStorageType() == STORAGE_TYPE_FWF)) { seek(packetBase); result = read(buffer, packetSize); } else { switch(getStorageType()) { case STORAGE_TYPE_LZD: { seek(packetBase + sizeof(int32_t)); if(!LZPacketBuffer) { LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if((int32_t)LZPacketBufferSize < packetSize) { LZPacketBufferSize = packetSize; free(LZPacketBuffer); LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if(LZPacketBuffer) { read(LZPacketBuffer, (packetSize - sizeof(int32_t))); int32_t decompLength = LZDecomp(buffer, LZPacketBuffer, packetSize - sizeof(int32_t)); if(decompLength != packetUnpackedSize) result = 0; else result = decompLength; } } break; case STORAGE_TYPE_ZLIB: { seek(packetBase + sizeof(int32_t)); if(!LZPacketBuffer) { LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if((int32_t)LZPacketBufferSize < packetSize) { LZPacketBufferSize = packetSize; free(LZPacketBuffer); LZPacketBuffer = (puint8_t)malloc(LZPacketBufferSize); gosASSERT(LZPacketBuffer); } if(LZPacketBuffer) { read(LZPacketBuffer, (packetSize - sizeof(int32_t))); uint32_t decompLength = LZPacketBufferSize; int32_t decompResult = uncompress(buffer, &decompLength, LZPacketBuffer, packetSize - sizeof(int32_t)); if((decompResult != Z_OK) || ((int32_t)decompLength != packetUnpackedSize)) result = 0; else result = decompLength; } } break; case STORAGE_TYPE_HF: STOP(("Tried to read a Huffman Compressed Packet. No Longer Supported!!")); break; } } } return result; }
//---------------------------------------------------------------------- // MC_TextureNode DWORD MC_TextureNode::get_gosTextureHandle (void) //If texture is not in VidRAM, cache a texture out and cache this one in. { if (gosTextureHandle == 0xffffffff) { //Somehow this texture is bad. Probably we are using a handle which got purged between missions. // Just send back, NO TEXTURE and we should be able to debug from there because the tri will have no texture!! return 0x0; } if (gosTextureHandle != CACHED_OUT_HANDLE) { lastUsed = turn; return gosTextureHandle; } else { if ((mcTextureManager->currentUsedTextures >= MAX_MC2_GOS_TEXTURES) && !mcTextureManager->flushCache()) return 0x0; //No texture! if (width == 0) return 0; //These faces have no texture!! if (!textureData) return 0x0; //No Texture. Cache is out of RAM!! if (width > 0xf0000000) { //------------------------------------------ // Cache this badboy IN. // Badboys are now LZ Compressed in texture cache. // Uncompress, then memcpy. long origSize = LZDecomp(MC_TextureManager::lzBuffer2,(MemoryPtr)textureData,lzCompSize); if (origSize != (long)(width & 0x0fffffff)) STOP(("Decompressed to different size from original! Txm:%s Width:%d DecompSize:%d",nodeName,(width & 0x0fffffff),origSize)); if (origSize >= MAX_LZ_BUFFER_SIZE) STOP(("Texture TOO large: %s",nodeName)); gosTextureHandle = gos_NewTextureFromMemory(key,nodeName,MC_TextureManager::lzBuffer2,(width & 0x0fffffff),hints); mcTextureManager->currentUsedTextures++; lastUsed = turn; return gosTextureHandle; } else { gosTextureHandle = gos_NewEmptyTexture(key,nodeName,width,hints); mcTextureManager->currentUsedTextures++; //------------------------------------------ // Cache this badboy IN. TEXTUREPTR pTextureData; gos_LockTexture(gosTextureHandle, 0, 0, &pTextureData); //------------------------------------------------------- // Create a block of cache memory to hold this texture. DWORD txmSize = pTextureData.Height * pTextureData.Height * sizeof(DWORD); gosASSERT(textureData); LZDecomp(MC_TextureManager::lzBuffer2,(MemoryPtr)textureData,lzCompSize); memcpy(pTextureData.pTexture,MC_TextureManager::lzBuffer2,txmSize); //------------------------ // Unlock the texture gos_UnLockTexture(gosTextureHandle); lastUsed = turn; return gosTextureHandle; } } }
//--------------------------------------------------------------------------- long FastFile::readFast (long fastFileHandle, void *bfr, long size) { size; long result = 0; if ((fastFileHandle >= 0) && (fastFileHandle < numFiles) && files[fastFileHandle].inuse) { logicalPosition = fseek(handle,files[fastFileHandle].pos + files[fastFileHandle].pfe->offset,SEEK_SET); //ALL files in the fast file are now zLib compressed. NO EXCEPTIONS!! // This fixes a bug where the zLib Compressed version is the same length // as the raw version. Yikes but this is rare. Finally happened though! // -fs /* if (files[fastFileHandle].pfe->size == files[fastFileHandle].pfe->realSize) { result = fread(bfr,1,size,handle); logicalPosition += size; if (result != size) { long lastError = errno; return lastError; } } else //File is NOW zLib Compressed. Read In Appropriately */ { if (!LZPacketBuffer) { LZPacketBuffer = (MemoryPtr)malloc(LZPacketBufferSize); if (!LZPacketBuffer) return 0; } if ((long)LZPacketBufferSize < files[fastFileHandle].pfe->size) { LZPacketBufferSize = files[fastFileHandle].pfe->size; free(LZPacketBuffer); LZPacketBuffer = (MemoryPtr)malloc(LZPacketBufferSize); if (!LZPacketBuffer) return 0; } if (LZPacketBuffer) { result = fread(LZPacketBuffer,1,files[fastFileHandle].pfe->size,handle); logicalPosition += files[fastFileHandle].pfe->size; if (result != files[fastFileHandle].pfe->size) { //READ Error. Maybe the CD is missing? bool openFailed = false; bool alreadyFullScreen = (Environment.fullScreen != 0); while (result != files[fastFileHandle].pfe->size) { openFailed = true; EnterWindowMode(); char data[2048]; sprintf(data,FileMissingString,fileName,CDMissingString); DWORD result1 = MessageBox(NULL,data,MissingTitleString,MB_OKCANCEL | MB_ICONWARNING); if (result1 == IDCANCEL) { ExitGameOS(); return (2); //File not found. Never returns though! } logicalPosition = fseek(handle,files[fastFileHandle].pos + files[fastFileHandle].pfe->offset,SEEK_SET); result = fread(LZPacketBuffer,1,files[fastFileHandle].pfe->size,handle); logicalPosition += files[fastFileHandle].pfe->size; } if (openFailed && (Environment.fullScreen == 0) && alreadyFullScreen) EnterFullScreenMode(); } //-------------------------------------------------------- //USED to LZ Compress here. It is NOW zLib Compression. // We should not try to use old fastfiles becuase version check above should fail when trying to open!! unsigned long decompLength = 0; if (useLZCompress) { decompLength = LZDecomp((MemoryPtr)bfr,LZPacketBuffer,files[fastFileHandle].pfe->size); } else { decompLength = files[fastFileHandle].pfe->realSize; long error = uncompress((MemoryPtr)bfr,&decompLength,LZPacketBuffer,files[fastFileHandle].pfe->size); if (error != Z_OK) STOP(("Error %d UnCompressing File %s from FastFile %s",error,files[fastFileHandle].pfe->name,fileName)); } if ((long)decompLength != files[fastFileHandle].pfe->realSize) result = 0; else result = decompLength; } } return result; } return FILE_NOT_OPEN; }