Example #1
0
void CLuaRemoteDebug::ReceiveFileContentsRequest(CSerializationHelper &buffer)
{
	const char* fileName = buffer.ReadString();
	ICryPak* pCryPak = gEnv->pCryPak;
	FILE* pFile = pCryPak->FOpen(fileName + 1, "rb");
	if (pFile != NULL)
	{
		m_sendBuffer.Write((char)ePT_FileContents);
		m_sendBuffer.WriteString(fileName);

		// Get file length
		pCryPak->FSeek(pFile, 0, SEEK_END);
		uint32 length = (uint32)pCryPak->FTell(pFile);
		pCryPak->FSeek(pFile, 0, SEEK_SET);

		m_sendBuffer.Write(length);

		const int CHUNK_BUF_SIZE = 1024;
		char buf[CHUNK_BUF_SIZE];
		size_t lenRead;

		while (!pCryPak->FEof(pFile))
		{
			lenRead = pCryPak->FRead(buf, CHUNK_BUF_SIZE, pFile);
			m_sendBuffer.WriteBuffer(buf, (int)lenRead);
		}

		SendBuffer();
	}
	else
	{
		assert(false);
	}
}
bool CGameAIRecorder::SendRemoteArchive(const char* szRecordingFile)
{
	ICryPak *pCryPak = gEnv->pSystem->GetIPak();
	assert(pCryPak);

	bool bResult = false;

	string sPAKFileName;
	sPAKFileName.Format("%s_%s", gEnv->pSystem->GetUserName(), PathUtil::GetFileName(szRecordingFile).c_str());
	string sDestFile = PathUtil::Make(CGameAIRecorderCVars::ai_remoteRecorder_serverDir, sPAKFileName.c_str(), "zip");

	CryLogAlways("AI Recording packed successfully! Copying to remote directory \'%s\'...", sDestFile.c_str());

	// Remote copy the file
	string sPAKFileLocalPath = PathUtil::Make("..", g_szRemoteTempArchive);
	AZ::IO::HandleType pakFileHandle = pCryPak->FOpen(sPAKFileLocalPath.c_str(), "rb");
	if (pakFileHandle != AZ::IO::InvalidHandle)
	{
		AZ::IO::HandleType destFileHandle = pCryPak->FOpen(sDestFile.c_str(), "wb");
		if (destFileHandle != AZ::IO::InvalidHandle)
		{
			BYTE pBuffer[512];
			while (true)
			{
				const int iReadAmount = pCryPak->FReadRaw(pBuffer, 1, 512, pakFileHandle);
				if (iReadAmount <= 0)
					break;

				if (pCryPak->FWrite(pBuffer, iReadAmount, 1, destFileHandle) > 0)
					bResult = true;
			}

			pCryPak->FClose(destFileHandle);
			pCryPak->FClose(pakFileHandle);
		}
	}

	return bResult;
}
static bool ModInfo_LoadFromFile(ModInfo* pMod, const char* pFilename)
{
	if (!pMod)
		return false;

	if (!gEnv || !gEnv->pCryPak)
	{
		assert(0);
		return false;
	}

	ICryPak* pCryPak = gEnv->pCryPak;

	FILE* f = pCryPak->FOpen(pFilename, "rb", ICryPak::FOPEN_ONDISK);
	if (!f)
		return false;

	pCryPak->FSeek(f, 0, SEEK_END);
	const size_t fileSize = pCryPak->FTell(f);
	pCryPak->FSeek(f, 0, SEEK_SET);
	if (fileSize == 0)
	{
		pCryPak->FClose(f);
		return false;
	}

	std::vector<char> buffer;
	buffer.resize(fileSize);
	if (pCryPak->FRead(&buffer[0], fileSize, f) != fileSize)
	{
		pCryPak->FClose(f);
		return false;
	}
	pCryPak->FClose(f);
	
	std::auto_ptr<IXmlParser> pParser(GetISystem()->GetXmlUtils()->CreateXmlParser());
	XmlNodeRef pRoot = pParser->ParseBuffer(&buffer[0], buffer.size(), true);
	if (!pRoot)
		return false;

	if (!ModInfo_LoadFromXML(pMod, pRoot))
		return false;

	return true;
}
bool CGameAIRecorder::AddFileToRemoteArchive(const char* szFile)
{
	bool bResult = false;

	ICryPak *pCryPak = gEnv->pSystem->GetIPak();
	assert(pCryPak);

	if (m_pRemoteArchive && pCryPak)
	{
		string sLocalPath = PathUtil::Make("..", szFile);

		AZ::IO::HandleType fileHandle = pCryPak->FOpen(sLocalPath.c_str(), "rb");
		if (fileHandle != AZ::IO::InvalidHandle)
		{
			// Add the file to the PAK
			size_t iFileSize = pCryPak->FGetSize(fileHandle);
			BYTE* pBuffer = (BYTE*)pCryPak->PoolMalloc(iFileSize);
			if (!pBuffer)
			{
				CryLogAlways("[Warning] Failed when packing file to remote archive: Out of memory. (\'%s\')", szFile);
			}
			else
			{
				pCryPak->FReadRaw(pBuffer, iFileSize, 1, fileHandle);
				int iResult = m_pRemoteArchive->UpdateFile(PathUtil::GetFile(szFile), pBuffer, iFileSize, ICryArchive::METHOD_DEFLATE, ICryArchive::LEVEL_BETTER);
				if (0 != iResult)
				{
				CryLogAlways("[Warning] Failed when packing file to remote archive: File update failed. (\'%s\')", szFile);
				}
				else
				{
					bResult = true;
				}

				pCryPak->PoolFree(pBuffer);
			}

			pCryPak->FClose(fileHandle);
		}
	}

	return bResult;
}
void CVehicleMovementAerodynamic::ReadFile(string _strFile,TPointsMap *_pPointsMap)
{
	ICryPak *pCryPak = gEnv->pCryPak;
	assert(pCryPak);

	FILE *pFile = pCryPak->FOpen(_strFile.c_str(),"r");
	if(pFile)
	{
		char acBuffer[256];
		while(pCryPak->FGets(acBuffer,256,pFile))
		{
			float fX;
			float fY;
			sscanf(acBuffer,"%f %f\n",&fX,&fY);

			_pPointsMap->insert(std::make_pair(fX,fY));
		}
		pCryPak->FClose(pFile);
	}
}
bool GetFileHash(const char *pPath, uint32 &outResult)
{
	if (!gEnv || !gEnv->pCryPak)
	{
		assert(0);
		return false;
	}

	ICryPak* pCryPak = gEnv->pCryPak;

	// Try to open file on disk and hash it (using algorithm taken from CryHashStringId)
	FILE *file = pCryPak->FOpen( pPath,"rb",ICryPak::FOPEN_ONDISK );
	if (file)
	{
		pCryPak->FSeek( file,0,SEEK_END );
		unsigned int nFileSize = pCryPak->FTell(file);
		pCryPak->FSeek( file,0,SEEK_SET );

		outResult = FILE_HASH_SEED;

		unsigned char *pBuf = (unsigned char*)malloc( FILE_CHECK_BUFFER_SIZE );
		if (!pBuf)
		{
			pCryPak->FClose(file);
			return false;
		}

		while (nFileSize)
		{
			unsigned int fetchLength=min(nFileSize,(unsigned int)FILE_CHECK_BUFFER_SIZE);

			unsigned int result = pCryPak->FRead( pBuf,fetchLength,file );
			if (result != fetchLength)
			{
				free( pBuf );
				pCryPak->FClose(file);
				return false;
			}
			
			const char *pChar = (const char*)pBuf;
			for (unsigned int i = 0; i < fetchLength; ++ i, ++ pChar)
			{
				outResult += *pChar;
				outResult += (outResult << 10);
				outResult ^= (outResult >> 6);
			}

			nFileSize-=fetchLength;
		}

		outResult += (outResult << 3);
		outResult ^= (outResult >> 11);
		outResult += (outResult << 15);

		free( pBuf );
		pCryPak->FClose(file);

		return true;
	}

	return false;
}