Exemplo n.º 1
0
// Seek in the File.
VFS_BOOL VFS_File_Seek( VFS_Handle hFile, VFS_LONG lPosition, VFS_SeekOrigin eOrigin )
{
	// Not initialized yet?
	if( !IsInit() )
	{
		SetLastError( VFS_ERROR_NOT_INITIALIZED_YET );
		return VFS_FALSE;
	}

	// Invalid Handle Value?
	if( hFile == VFS_INVALID_HANDLE_VALUE )
	{
		SetLastError( VFS_ERROR_INVALID_PARAMETER );
		return VFS_FALSE;
	}

	// Get the File Pointer.
	IFile* pFile = ( IFile* )( VFS_DWORD )hFile;

	return pFile->Seek( lPosition, eOrigin );
}
Exemplo n.º 2
0
int KGTestMapDisuseResource::FindResInPVS(const char cszResourceName[], set<string>& setResList)
{
	//参照KG3DRepresentObjectPVS::LoadPvsFile
	int nRetCode = false;
	int nResult  = false;
	HRESULT hrRetCode = E_FAIL;
	IFile* pFile = NULL;
	DWORD dwVersion = PVS_FILE_VERSION;
	char szSourcePathName[MAX_PATH] = {0};
	char szResName[MAX_PATH]		= {0};
	long lOffset  = 0;
	size_t objNum = 0;

	KG_ASSERT_EXIT(cszResourceName);
	KGLOG_PROCESS_ERROR(cszResourceName[0] != '\0');

	nRetCode = _snprintf_s(szSourcePathName,
		sizeof(szSourcePathName),
		sizeof(szSourcePathName) - 1,
		"%s%s",
		m_szClientPath,
		cszResourceName);
	KGLOG_PROCESS_ERROR(nRetCode > 0);
	pFile = g_OpenFile(cszResourceName, false, false);
	KGLOG_PROCESS_ERROR(pFile);

	/* read version */
	pFile->Read(&dwVersion, sizeof(dwVersion));

	/* load border, the frist model of the pvs object is it's ouside border
	and the second model is it's inside border */
	pFile->Read(szResName, sizeof(szResName));
	FindResource(szResName, setResList);

	pFile->Read(szResName, sizeof(szResName));
	FindResource(szResName, setResList);

	/* load inside objects, the object may be is pvs object too */
	pFile->Read(&objNum, sizeof(objNum));

	for (size_t i = 0; i < objNum; i++)
	{
		DWORD dwObjType = 0;

		pFile->Read(&dwObjType, sizeof(dwObjType));

		switch (dwObjType)
		{
		case REPRESENTOBJECT_PVS :
		case REPRESENTOBJECT_SET :
			{
				pFile->Read(szResName, sizeof(szResName));
				FindResource(szResName, setResList);
			}
			break;
		case REPRESENTOBJECT_DEFAULT :
			{
				/* is normal object */
				DWORD dwCount = 0;
				DWORD dwColor = 0;
				DWORD dwType  = 0;
				char szAnins[MAX_PATH] = {0};

				pFile->Read(&dwCount, sizeof(dwCount));

				for (DWORD i = 0; i < dwCount; i++)
				{
					pFile->Read(szResName, sizeof(szResName));
					FindResource(szResName, setResList);
					if (dwVersion > PVS_FILE_VERSION_1)
					{
						pFile->Read(szAnins, sizeof(szAnins));
						FindResource(szAnins, setResList);
					}
					TypeInfo* pInfo				  = NULL;
					IEKG3DEngineManager* pIEEngineMgr = NULL;
					IEKG3DModelTable* pIEModelTable   = NULL;
					KG3DModelTable* pModelTable       = NULL;

					pIEEngineMgr = (IEKG3DEngineManager*)m_pEngineMgr;
					hrRetCode = pIEEngineMgr->GetIEKG3DModelTable(&pIEModelTable);
					KGLOG_COM_PROCESS_ERROR(hrRetCode);
					pModelTable= (KG3DModelTable*)pIEModelTable;
					hrRetCode = pModelTable->GetTypeInfoByFileName(&pInfo, szResName);

					if (pInfo)
						dwType = pInfo->dwType;
					else
						dwType = MESHTYPE_DEFAULT;

					switch (dwType)
					{
					case MESHTYPE_POINTLIGHT :
						{
							pFile->Read(&dwColor, sizeof(dwColor));
						}
						break;
					default :
						break;
					}
				}
			}
			break;
		default :
			_ASSERTE(false);
			break;
		}
		lOffset = sizeof(D3DXVECTOR3) +		//ScalingCenter
			sizeof(D3DXQUATERNION) +		//ScalingRotation
			sizeof(D3DXVECTOR3) +			//Scaling
			sizeof(D3DXVECTOR3) +			//RotationCenter
			sizeof(D3DXQUATERNION) +		//Rotation
			sizeof(D3DXVECTOR3) +			//Translation
			sizeof(AABBOX);					//aabox
		pFile->Seek(lOffset, SEEK_CUR);
		lOffset = 0;
	}
	pFile->Close();
	SAFE_RELEASE(pFile);

	nResult = true;
Exit0:
	if (pFile)
	{
		pFile->Close();
		SAFE_RELEASE(pFile);
	}
	return nResult;
}
Exemplo n.º 3
0
void SuiteFile::TestFunctionality(IFile& aFile, TUint32 aBytes)
{
    Print("Testing File implementation functionality on %d byte file\n", aBytes);

    // We should be able to set the file cursor to all
    // bytes, from 0, up to /and including/ aBytes.
    // i.e. it is valid for the cursor to be pointing off
    // the end, but not to read data from here.

    // Test absolute seeking - ensure cursor is changed for
    // each test.

    aFile.Seek(0, eSeekFromStart);
    TEST(aFile.Tell() == 0);

    aFile.Seek(aBytes, eSeekFromStart);
    TEST(aFile.Tell() == aBytes);

    aFile.Seek(-(TInt) aBytes, eSeekFromEnd);
    TEST(aFile.Tell() == 0);

    aFile.Seek(0, eSeekFromEnd);
    TEST(aFile.Tell() == aBytes);

    // Test boundaries

    // Backwards at start.
    TEST_THROWS(aFile.Seek(-1, eSeekFromStart),         FileSeekError);
    // Forwards at end
    //TEST_THROWS(aFile.Seek(+1, eSeekFromEnd),           FileSeekError);
    // Forwards from start
    //TEST_THROWS(aFile.Seek(aBytes+1, eSeekFromStart),   FileSeekError);

    // Backwards from end
    TEST_THROWS(aFile.Seek(-(TInt) (aBytes+1), eSeekFromEnd),  FileSeekError);

    // Reading

    Bwh buffer(aBytes + 20);
    aFile.Seek(0, eSeekFromStart);

    aFile.Read(buffer);
    TEST(aFile.Tell() == aBytes);
    TEST(buffer.Bytes() == aBytes);

    const TUint readBytes = 10;
    aFile.Seek(0, eSeekFromStart);
    buffer.SetBytes(0);
    aFile.Read(buffer, readBytes);
    TEST(aFile.Tell() == readBytes);
    TEST(buffer.Bytes() == readBytes);

    aFile.Seek(-(TInt) (readBytes/2), eSeekFromEnd);
    buffer.SetBytes(0);
    aFile.Read(buffer);
    TEST(aFile.Tell() == aBytes);
    TEST(buffer.Bytes() == readBytes/2);

    aFile.Seek(0, eSeekFromEnd);
    buffer.SetBytes(0);
    TEST_THROWS(aFile.Read(buffer), FileReadError);

    // Writing

    buffer.SetBytes(aBytes/2);
    TEST_THROWS(aFile.Write(buffer), FileWriteError);

    Print("\n");
}
Exemplo n.º 4
0
HRESULT KG3DTerrainRoad::LoadFromFile(const char cszFileName[])
{
	//return E_FAIL;
    HRESULT hr = E_FAIL;	
	int nRetCode = false;
	TCHAR FileName[MAX_PATH]; 
	_RoadHead RoadHead;
	DWORD nSize;
	D3DXVECTOR3* pNodePos = NULL;
	IFile* pFile = NULL;
	pFile = g_OpenFile(cszFileName);
	KG_PROCESS_ERROR(pFile);

	nSize = pFile->Read(&RoadHead,sizeof(_RoadHead));//fread(&RoadData,sizeof(_RoadData),1,pFile);
	
	m_scName        = RoadHead.scName;
	m_scTextureName = RoadHead.scTextureName;
	m_fBendModulus  = RoadHead.fBendModulus;
	m_fEdgeModulus  = RoadHead.fEdgeModulus;
	m_fTexDensity   = RoadHead.fTexDensity;
	m_fBlendLength  = RoadHead.fBlendLength;
	m_fWidth        = RoadHead.fWidth;
	m_nID           = RoadHead.nID;
	m_fNodeSize     = RoadHead.fNodeSize;
	m_nSegmentLength= RoadHead.dwSegmentLength;
	wsprintf(FileName,"%s", m_scTextureName.c_str());
	LoadRoadTexture(FileName);
	if (!g_bClient)
	{
		nRetCode = pFile->Seek(RoadHead.dwParentTerrainBlock, SEEK_SET);
		KGLOG_PROCESS_ERROR(nRetCode != -1);
		for (int nT= 0; nT < (int)RoadHead.dwNumParentBlock; nT++)
		{
			TerrainBlockInfo TBI;
			pFile->Read(&TBI,sizeof(TerrainBlockInfo));
			m_vecParentTerrainBlock.push_back(TBI);
		}

		nRetCode = pFile->Seek(RoadHead.dwNodePosBlock, SEEK_SET);//fseek(pFile,RoadData.dwNodePosBlock, SEEK_SET);
		KGLOG_PROCESS_ERROR(nRetCode != -1);
		pNodePos = new D3DXVECTOR3[RoadHead.dwNumOfNode];
		KGLOG_PROCESS_ERROR(pNodePos);
		nSize = pFile->Read(pNodePos,sizeof(D3DXVECTOR3)*RoadHead.dwNumOfNode);//fread(RoadData.pNodePos,sizeof(D3DXVECTOR3),RoadData.dwNumOfNode,pFile);
		LoadNode(RoadHead.dwNumOfNode,pNodePos);
	}

	nRetCode = pFile->Seek(RoadHead.dwPassageBlock, SEEK_SET);
	KGLOG_PROCESS_ERROR(nRetCode != -1);
	{
	
	list<KG3DRepresentObjectNode*>::iterator iNode = m_listNode.begin();
	
	float fLengthSum = 0;
	m_BBox.Clear();
	for (DWORD i = 0;i < RoadHead.dwNumOfPassage;i++)
	{
		KG3DTerrainRoadPassage* pPassage = new KG3DTerrainRoadPassage();
		if(pPassage)
		{
			_PassageData ReadData;
			if(SUCCEEDED(pPassage->ReadDataFromFile(pFile,ReadData)))
			{
				pPassage->CreateBuffersFromData(ReadData);
				pPassage->BulidPassageData(fLengthSum,m_fWidth,m_fBendModulus,m_nSegmentLength,m_fExtendWidth);
				m_listPassage.push_back(pPassage);
				m_BBox.AddPosition(pPassage->m_Bbox.A);
				m_BBox.AddPosition(pPassage->m_Bbox.B);
				m_ExtendBBox.AddPosition(pPassage->m_ExtendBBox.A);
				m_ExtendBBox.AddPosition(pPassage->m_ExtendBBox.B);

				if(iNode!=m_listNode.end())
				{
					pPassage->m_pNodeA = *iNode;
					++iNode;
					if(iNode!=m_listNode.end())
						pPassage->m_pNodeB = *iNode;
				}
			}
			else
			{
				SAFE_DELETE(pPassage);
			}
		}
		
	}
	this->m_fLength = fLengthSum;
	}
	hr = S_OK;
Exit0:
	KG_COM_RELEASE(pFile);
	SAFE_DELETE_ARRAY(pNodePos);
	return hr;
}
Exemplo n.º 5
0
HRESULT KTgaButton::LoadTgaFile(const char* strFileName,BOOL bAddToTerrian)
{
	if (!strFileName)
		return E_FAIL;
	
	bool isload = false;
	std::string dir = strFileName;
	std::list<std::string>::iterator its,listend;
	std::string *psting = NULL;

	//FILE* fpFile = fopen(strFileName, "rb");
	 IFile* fpFile = g_OpenFile(strFileName);
	if (!fpFile)//打开默认的贴图文件
	{
		TCHAR NameDefault[MAX_PATH];
		wsprintf(NameDefault,"%s\\Data\\Editor\\Texture\\replace.tga",g_szDefWorkDirectory);

		fpFile = g_OpenFile(NameDefault);//fpFile = fopen(NameDefault, "rb");//
		if(!fpFile)
			return E_FAIL;
	}
	

	fpFile->Read(&m_TgaInfo.Header, sizeof(TGAHeader));//fread(&m_TgaInfo.Header, sizeof(TGAHeader), 1, fpFile);//
	const int byteperpixel = m_TgaInfo.Header.bitsperpixel / 8;
	if(byteperpixel != 4)
	{
		MessageBox("Tga格式不正确,必须为32位");
		SAFE_RELEASE(fpFile);//fclose(fpFile);//
		return E_FAIL;
	}

	m_TextureDir = dir;

	//TCHAR drive[256];
	//TCHAR dirc[256];
	//TCHAR fname[256];
	//TCHAR ext[256];
	std::string filename;

	if(m_pTerrainEx && m_nButtonType == GROUNDTEXTURE)
	{
		int nCoord =0;
		if(SUCCEEDED(m_pTerrainEx->GetGroundTextureCoordIndex(m_nLayer,m_nTextureIndex,&nCoord)))
		{
			SetTexCoordIndex(nCoord);
		}
	}

	size_t Size = m_TgaInfo.Header.height * m_TgaInfo.Header.width * byteperpixel;
	SAFE_DELETE_ARRAY(m_TgaInfo.pBuffer);
	m_TgaInfo.pBuffer = new BYTE[Size];
	fpFile->Seek(m_TgaInfo.Header.Id + sizeof(TGAHeader), SEEK_SET);//fseek(fpFile, m_TgaInfo.Header.Id + sizeof(TGAHeader), SEEK_SET);//
	fpFile->Read(m_TgaInfo.pBuffer, (unsigned long)Size);//fread(m_TgaInfo.pBuffer, Size, 1, fpFile);//

	RECT rect;
	GetClientRect(&rect);
	int height = abs(rect.bottom - rect.top);
	int width = abs(rect.left - rect.right);

	BYTE *NewData = SacleData(rect,m_TgaInfo.pBuffer,byteperpixel);
	CDC* pDc = this->GetDC();
	m_Bitmap.CreateCompatibleBitmap(pDc, width, height);
	m_Bitmap.SetBitmapBits(width * height * byteperpixel,NewData);

	SetBitmap(m_Bitmap);
	ReleaseDC(pDc);
	SAFE_DELETE_ARRAY(NewData);

	//fclose(fpFile);
	SAFE_RELEASE(fpFile);
	m_bIsHaveTexture = true;

	return S_OK;
}
Exemplo n.º 6
0
bool
sMap::Import_VergeMAP(const char* filename, const char* tilesetFilename, IFileSystem& fs)
{
  m_MusicFile   = "";
  m_EntryScript = "";
  m_ExitScript  = "";
  m_Layers.clear();
  m_Entities.clear();

  IFile* file = fs.Open(filename, IFileSystem::read);
  if (file == NULL) 
    return false;

  // check for v1 maps (ver 4) and v2 maps (ver 5)
  char signature[6];
  file->Read(signature, 6);
  file->Seek(0);

  bool success = false;

  if (signature[0] == 4)
  {
    V1MAP_HEADER header;
    file->Read(&header, sizeof(header));

    word* layer_background = new word[header.layer_size_x * header.layer_size_y];
    word* layer_foreground = new word[header.layer_size_x * header.layer_size_y];
    file->Read(layer_background, header.layer_size_x * header.layer_size_y * sizeof(word));
    file->Read(layer_foreground, header.layer_size_x * header.layer_size_y * sizeof(word));

    sTileset tileset;
    if (strcmp_ci(tilesetFilename + strlen(tilesetFilename) - 4, ".vsp") == 0)
      success = tileset.Import_VSP(tilesetFilename, fs);
    else
      success = tileset.Load(tilesetFilename, fs);

    sLayer layer[2];

    if (success)
    {
      // process map and see if the map has tiles that are out of range
      int highestTileIndex = 0;
      for (int j=0; j<header.layer_size_y; j++)
        for (int i=0; i<header.layer_size_x; i++)
          if (layer_background[j * header.layer_size_x + i] >= highestTileIndex)
            highestTileIndex = layer_background[j * header.layer_size_x + i];
          else if (layer_foreground[j * header.layer_size_x + i] >= highestTileIndex)
            highestTileIndex = layer_foreground[j * header.layer_size_x + i];

      if (highestTileIndex >= tileset.GetNumTiles())
        success = false;

      // transfer data across into the sMap now...
      if (success)
      {
        layer[0].SetName("Background");
        layer[1].SetName("Foreground");
        layer[0].Resize(header.layer_size_x, header.layer_size_y);
        layer[1].Resize(header.layer_size_x, header.layer_size_y);

        for (int j=0; j<header.layer_size_y; j++)
          for (int i=0; i<header.layer_size_x; i++)
          {
            layer[0].SetTile(i,j, layer_background[j * header.layer_size_x + i]);

            if (layer_foreground[j * header.layer_size_x + i])
            layer[1].SetTile(i,j, layer_foreground[j * header.layer_size_x + i]);
            else
              layer[1].SetTile(i,j, tileset.GetNumTiles());
          }

        tileset.AppendTiles(1);
        memset(tileset.GetTile(tileset.GetNumTiles() - 1).GetPixels(), 0, 256 * sizeof(RGBA));
        m_Tileset = tileset;
        AppendLayer(layer[0]);
        AppendLayer(layer[1]);
        SetMusicFile((char*)header.music_fname);
        SetStartX(header.x_start);
        SetStartX(header.y_start);
        SetStartDirection(4);

        // calculate the parallax mode
        for (int i=0; i<2; i++)
        {
          // FIXME (set parallax properly)
//          GetLayer(i).SetParallaxX(1, 1);
//          GetLayer(i).SetParallaxY(1, 1);
//          GetLayer(i).SetScrollingX(1, 1);
//          GetLayer(i).SetScrollingX(1, 1);
        }

        switch(header.parallax_mode)
        {
          case 0:
            SetStartLayer(0);
            break;

          case 1:
            SetStartLayer(1);
            break;

          case 2:
            // FIXME (set parallax properly)
            SetStartLayer(1);
//            GetLayer(0).SetParallaxX(header.parallax_multiplier, header.parallax_divisor);
//            GetLayer(0).SetParallaxY(header.parallax_multiplier, header.parallax_divisor);
            break;

          case 3:
            // FIXME (set parallax properly)
            SetStartLayer(0);
//            GetLayer(1).SetParallaxX(header.parallax_multiplier, header.parallax_divisor);
//            GetLayer(1).SetParallaxY(header.parallax_multiplier, header.parallax_divisor);
            break;
        }
      }
    }

    // cleanup
    delete[] layer_background;
    delete[] layer_foreground;
  }
  else if (strcmp(signature, "MAPù5") == 0)
  {
    V2MAP_HEADER header;
    V2MAP_LAYERINFO LayerInfo[7];
    sTileset tileset;
    word *mapLayer[7];
    int i,j,k;
    int highestTileIndex = 0;

    file->Read(&header, sizeof(header));
    for (i=0; i<header.num_layers; i++)
    {
      file->Read(LayerInfo + i, sizeof(V2MAP_LAYERINFO));
      //bug for v2's map: two bytes are added for no reason
      word w;
      file->Read(&w, 2);
    }

    // get info about map and uncompress it
    for (i=0; i<header.num_layers; i++)
      mapLayer[i] = new word[LayerInfo[i].size_x * LayerInfo[i].size_y];

    for (i=0; i<header.num_layers; i++)
    {
      // god, this is so dumb. It's supposed to be the buffersize, but do I look like I need it?
      file->Read(&j, 4);
      for (j=0; j<LayerInfo[i].size_x*LayerInfo[i].size_y; j++)
      {
        word value;
        byte run;
        
        file->Read(&value, sizeof(word));

        if ((value & 0xFF00) == 0xFF00)
        {
          run = (byte)value & 0x00FF;
          file->Read(&value, sizeof(word));
          
          mapLayer[i][j] = value;
          for (k=1; k<run; k++)
          {
            j++;
            mapLayer[i][j] = value;
          }
        }
        else
        {
          mapLayer[i][j]  = value;
        }
      }
    }

    if (strcmp_ci(tilesetFilename + strlen(tilesetFilename) - 4, ".vsp") == 0)
      success = tileset.Import_VSP(tilesetFilename);
    else
      success = tileset.Load(tilesetFilename);


    // transfer map array into the class
    if (success)
    {
      highestTileIndex = 0;
      // check for any tile index larger than the tilset's index
      for (i=0; i<header.num_layers; i++)
        for (j=0; j<LayerInfo[i].size_x*LayerInfo[i].size_y; j++)
          if (mapLayer[i][j] >= highestTileIndex)
            highestTileIndex = mapLayer[i][j];

      if (highestTileIndex >= tileset.GetNumTiles())
        success = false;

    if (success)
      {
        sLayer *layer;
        layer = new sLayer[header.num_layers];

        for (i=0; i<header.num_layers; i++)
        {
          char Name[7];
          memcpy(Name, "Layer A", 8);
          Name[6] += i;

          layer[i].SetName(Name);
          layer[i].Resize(LayerInfo[i].size_x, LayerInfo[i].size_y);
        }

        for (i=0; i<header.num_layers; i++)
        {
          for (j=0; j<LayerInfo[i].size_y; j++)
            for (k=0; k<LayerInfo[i].size_x; k++)
              layer[i].SetTile(k, j, mapLayer[i][(j * LayerInfo[i].size_x) + k]);

          // FIXME: set parallax properly
//          layer[i].SetParallaxX(LayerInfo[i].multx, LayerInfo[i].pdivx);
//          layer[i].SetParallaxY(LayerInfo[i].multy, LayerInfo[i].pdivy);
//          layer[i].SetScrollingX(1,1);
//          layer[i].SetScrollingY(1,1);
        }

        for (i=0; i<(int)strlen((char*)header.renderstring); i++)
          switch(header.renderstring[i])
          {
          case '1': AppendLayer(layer[0]); j = 0; break;
          case '2': AppendLayer(layer[1]); j = 1; break;
          case '3': AppendLayer(layer[2]); j = 2; break;
          case '4': AppendLayer(layer[3]); j = 3; break;
          case '5': AppendLayer(layer[4]); j = 4; break;
          case '6': AppendLayer(layer[5]); j = 5; break;
          case 'E': SetStartLayer(j); break;
          }

        SetMusicFile((char*)header.music_name);
        SetStartX(header.x_start);
        SetStartY(header.y_start);
        m_Tileset = tileset;

        delete[] layer;
      } 
    }

    for (i=0; i<header.num_layers; i++)
      delete mapLayer[i];
  }

  file->Close();
  return success;
}