void vHavokShapeFactory::GetHktDependencies(VResourceSnapshot &snapshot, VisBaseEntity_cl *pEntity)
{
  VASSERT(pEntity != NULL);

  // Get wrapped rigid body
  vHavokRigidBody *pWrappedRigidBody = pEntity->Components().GetComponentOfType<vHavokRigidBody>();
  if (pWrappedRigidBody == NULL)
    return;

  // Get shape
  vHavokPhysicsModule *pModule = vHavokPhysicsModule::GetInstance();
  VASSERT(pModule != NULL);
  pModule->MarkForRead();
  const hkpRigidBody *pRigidBody = pWrappedRigidBody->GetHkRigidBody();
  if (pRigidBody == NULL)
  {
    pModule->UnmarkForRead();
    return;
  }
  const hkpShape *pShape = pRigidBody->getCollidable()->getShape();
  pModule->UnmarkForRead();
  
  // Only convex/ mesh rigid bodies have a cached HKT file
  const hkClass* loadedClassType = hkVtableClassRegistry::getInstance().getClassFromVirtualInstance(pShape);
  if (loadedClassType!=&hkvConvexVerticesShapeClass && loadedClassType!=&hkvBvCompressedMeshShapeClass)
    return;

  // Get mesh
  VDynamicMesh *pMesh = pEntity->GetMesh();
  if (pMesh == NULL)
    return;

  // Get scale
  hkvVec3 vScale = pEntity->GetScaling();
  bool shrinkToFit = pWrappedRigidBody->Havok_TightFit;

  // Get HKT file dependency for convex/ mesh rigid body
  if (loadedClassType == &hkvConvexVerticesShapeClass) 
  {
    VStaticString<FS_MAX_PATH> szCachedShapeName(pMesh->GetFilename());
    vHavokCachedShape::GetConvexShapePath(szCachedShapeName, vScale, shrinkToFit);
    IVFileInStream *pIn = Vision::File.Open(szCachedShapeName);
    if (pIn)
    {
      snapshot.AddFileDependency(pMesh, szCachedShapeName, pIn->GetSize() );
      pIn->Close();
    }
  }
  else if(loadedClassType == &hkvBvCompressedMeshShapeClass) 
  {
    VStaticString<FS_MAX_PATH> szCachedShapeName(pMesh->GetFilename());
    vHavokCachedShape::GetMeshShapePath(szCachedShapeName, vScale, VisStaticMeshInstance_cl::VIS_COLLISION_BEHAVIOR_CUSTOM, (VisWeldingType_e)pWrappedRigidBody->Havok_WeldingType);
    IVFileInStream *pIn = Vision::File.Open(szCachedShapeName);
    if (pIn)
    {
      snapshot.AddFileDependency(pMesh, szCachedShapeName, pIn->GetSize() );
      pIn->Close();
    }
  }
}
Beispiel #2
0
BOOL VPrefab::Reload()
{
  if (IsMissing())
    return FALSE;
  IVFileInStream *pIn;
  if (GetParentManager()!=NULL)
    pIn = GetParentManager()->CreateFileInStream(GetFilename(),this);
  else
    pIn = Vision::File.Open(GetFilename());
  if (pIn==NULL)
  {
    FlagAsMissing();
    return FALSE;
  }
  // must match saving code by vForge
  pIn->Read(&m_Header,sizeof(m_Header),"6i");
  m_iSize = pIn->GetSize() - sizeof(m_Header);

  // header validation:
  if (m_iSize<0 || m_Header.m_iArchiveVersion<0 || m_Header.m_iArchiveVersion>Vision::GetArchiveVersion() 
   || m_Header.m_iLocalVersion<0 || m_Header.m_iLocalVersion>VPREFAB_BINARY_VERSION_CURRENT)
  {
    pIn->Close();
    hkvLog::Warning("Cannot load VPrefab '%s': Invalid version or broken file", GetFilename());
    FlagAsMissing();
    return FALSE;
  }

  m_BinaryBlock.EnsureCapacity(m_iSize);
  pIn->Read(m_BinaryBlock.GetBuffer(),m_iSize);

  pIn->Close();

  return TRUE;
}
void TestSerialize(VisBaseEntity_cl *pEntity)
{
  // create a clone of the entity via serializing it to a memory stream
  VClipboardPtr m_spClipboard = new VClipboard(); // for memory streams
  {
    // write the entity into a memory clipboard
    IVFileOutStream *pMemStream = m_spClipboard->Create("MemoryStream");
    VArchive ar("TestArchive",pMemStream,Vision::GetTypeManager());
      ar << Vision::GetArchiveVersion();
      ar << pEntity;
    ar.Close();
    pMemStream->Close();
  }
  {
    // ...and load again as a separate instance running parallel
    IVFileInStream *pMemStream = m_spClipboard->Open("MemoryStream");
    VArchive ar("TestArchive",pMemStream,Vision::GetTypeManager());
      int iVers;
      ar >> iVers;
      ar.SetLoadingVersion(iVers);
      VisBaseEntity_cl *pEntity1 = (VisBaseEntity_cl *)ar.ReadObject(NULL);
    ar.Close();
    pMemStream->Close();
    pEntity1->IncPosition(10.f,0,0); // shift to the side so we can see whether both are synchronous
  }
}
void vHavokShapeFactory::GetHktDependencies(VResourceSnapshot &snapshot, VTerrainSector *pSector)
{
  VASSERT(pSector != NULL);

  // Return when no physics representation
  vHavokTerrain *pHavokTerrain = (vHavokTerrain*) pSector->GetPhysicsUserData();
  if (pHavokTerrain == NULL)
    return;

  const VTerrainSector::VPhysicsType_e ePhysicsType = pSector->GetPhysicsType();
  const bool bHasHoles = pSector->HasHoles();

  // First get filename by specifying extension (hmap) to be able to retrieve the absolute path. 
  // Afterwards remove extension.
  char szFilename[FS_MAX_PATH];
  pSector->m_Config.GetSectorFilename(szFilename, pSector->m_iIndexX, pSector->m_iIndexY, "hmap", true);
  char szPath[FS_MAX_PATH];
  bool bSuccess = VFileHelper::GetAbsolutePath(szFilename, szPath, Vision::File.GetManager());
  VASSERT_MSG(bSuccess, "vHavokShapeFactory::GetHktDependencies: Failed to make the path to the sector hmap absolute, the file may not exist!");
  VFileHelper::GetFilenameNoExt(szPath, szFilename);

  // Build the cached shape filename
  VStaticString<FS_MAX_PATH> szCachedShapeName(szPath);
  vHavokCachedShape::GetTerrainSectorShapePath(szCachedShapeName, ePhysicsType, bHasHoles);
  IVFileInStream *pIn = Vision::File.Open(szCachedShapeName);
  if (pIn)
  {
    snapshot.AddFileDependency(pSector, szCachedShapeName, pIn->GetSize() );
    pIn->Close();
  }

}
inline void VGroupInstanceFile::Close(bool bCloseStream)
{
  VASSERT_MSG(m_iInstanceCount==0, "mismatch number of instances read or written");
  if (m_pIn) {if (bCloseStream) m_pIn->Close();m_pIn=NULL;}
  if (m_pOut) {if (bCloseStream) m_pOut->Close();m_pOut=NULL;}
  m_iInstanceCount = 0;
  m_eFlags = VGroupInstance::INSTANCE_UNDEFINED;
}
FMOD_RESULT F_CALLBACK VisionFM_Close(void *handle, void *userdata)
{
  if (!handle)
    return FMOD_ERR_INVALID_PARAM;

  IVFileInStream *pStream = (IVFileInStream *)handle;
  pStream->Close();

  return FMOD_OK;
}
void vHavokAiNavMeshResource::GetDependencies(VResourceSnapshot &snapshot)
{
  IVFileInStream* pIn = Vision::File.Open(GetFilename());
  if (pIn != NULL)
  {
    snapshot.AddFileDependency(this, GetFilename(), pIn->GetSize());
    pIn->Close();
  }

  VManagedResource::GetDependencies(snapshot);
}
void vHavokShapeFactory::GetHktDependencies(VResourceSnapshot &snapshot, VisStaticMeshInstance_cl *pMeshInstance)
{
  VASSERT(pMeshInstance != NULL);

  // Get scale
  hkvVec3 vScale(hkvNoInitialization);
  ExtractScaling(pMeshInstance->GetTransform(), vScale);

  // There is no real way to figure out if a convex or mesh shape is required and the filename is based on that.
  // So try them both.

  // Convex version
  {
    VStaticString<FS_MAX_PATH> szCachedShapeName(pMeshInstance->GetMesh()->GetFilename());
    bool shrinkToFit = false; //how do if true / can it be true for static mesh?
    vHavokCachedShape::GetConvexShapePath(szCachedShapeName, vScale, shrinkToFit);
    IVFileInStream *pIn = Vision::File.Open(szCachedShapeName);
    if (pIn)
    {
      snapshot.AddFileDependency(pMeshInstance->GetMesh(), szCachedShapeName, pIn->GetSize() );
      pIn->Close();
      return;
    }
  }

  // Mesh version
  {
    VStaticString<FS_MAX_PATH> szCachedShapeName(pMeshInstance->GetMesh()->GetFilename());
    vHavokCachedShape::GetMeshShapePath(szCachedShapeName, vScale, pMeshInstance->GetCollisionBehavior(), pMeshInstance->GetWeldingType());
    IVFileInStream *pIn = Vision::File.Open(szCachedShapeName);
    if (pIn)
    {
      snapshot.AddFileDependency(pMeshInstance->GetMesh(), szCachedShapeName, pIn->GetSize() );
      pIn->Close();
      return;
    }
  }
}
void vHavokBehaviorScriptAssetLoader::loadScript(char const *filePath, bool forceLoad)
{
  hkStorageStringMap<ScriptEntry*>& scriptEntries = accessScriptEntries();

  // Check for cached script
  ScriptEntry* cachedEntry = HK_NULL;
  if(scriptEntries.hasKey(filePath))
  {
    if( !forceLoad )
    {
      //Already loaded.
      return;
    }
		
    // Want to force load the script asset
    scriptEntries.get(filePath, &cachedEntry);
  }

  // Load the stream
  IVFileInStream *pIn = m_resourceManager->CreateFileInStream(filePath, HK_NULL);
  if(!pIn)
  {
    return;
  }

  LONG const size = pIn->GetSize();

  if(forceLoad && cachedEntry != HK_NULL)
  {
    // Clear out current cached entry
    cachedEntry->m_content.clearAndDeallocate();

    // Set new entry
    cachedEntry->m_content.setSize(size);
    pIn->Read(cachedEntry->m_content.begin(), size);
  }
  else
  {
    // Create new script entry
    ScriptEntry* entry = new ScriptEntry(filePath, size);
    pIn->Read(entry->m_content.begin(), size);

    // Add to map
    scriptEntries.insert(filePath, entry);
  }

  pIn->Close();
}
void VFmodSoundResource::GetDependencies(VResourceSnapshot &snapshot)
{
  if (!IsStreaming())
  {
    VManagedResource::GetDependencies(snapshot);
    IVFileInStream *pFile = Vision::File.Open(GetFilename());
    if (pFile)
    {
      // patch the file size afterwards
      VResourceSnapshotEntry *pEntry = snapshot.FindResourceEntry(this);
      VASSERT(pEntry!=NULL)
        if (pEntry)
        {
          pEntry->m_iFileSize = pFile->GetSize();
          pEntry->SetCustomIntValue(0,m_iSoundFlags);
        }
        pFile->Close();
    }
  }
  static bool LoadScript(lua_State *L, const char * szFileName) 
  {
    IVFileInStream *pIn = Vision::File.Open(szFileName);
    if (!pIn) return false;

    int iScriptLen = pIn->GetSize();
    VMemoryTempBuffer<16*1024> buffer(iScriptLen+1);
    char *szBuffer = (char *)buffer.GetBuffer();
    pIn->Read(szBuffer,iScriptLen);
    szBuffer[iScriptLen] = 0;
    pIn->Close();

    if (!VScriptResourceManager::LuaErrorCheck(L, luaL_loadbuffer(L, szBuffer, iScriptLen, szFileName)))
      return false;

    if (!VScriptResourceManager::LuaErrorCheck(L, lua_pcall (L, 0, LUA_MULTRET, 0)))
      return false;

    return true;
  }
Beispiel #12
0
VDialog* VDialog::LoadFromBinaryFile(const char *szFilename)
{
  // no file extension? Then add default binary extension
  char szBuffer[FS_MAX_PATH];
  if (VFileHelper::GetExtensionPos(szFilename)<0)
  {
    VFileHelper::AddExtension(szBuffer, szFilename, DIALOG_BINARYFILEEXT);
    szFilename = szBuffer;
  }
  IVFileInStream *pIn = Vision::File.Open(szFilename);
  if (!pIn)
    return NULL;

  VArchive ar(NULL, pIn, Vision::GetTypeManager());
  int iArchiveVersion;
  ar >> iArchiveVersion;
  ar.SetLoadingVersion(iArchiveVersion);

  VDialog* pDlg = ar.ReadObject<VDialog>();
  ar.Close();
  pIn->Close();
  return pDlg;
}
Beispiel #13
0
// ---------------------------------------------------------------------------------
// Method: LoadGame
// Notes: The file format for save games in this example is as follows:
//
//        "SERIALIZE_API_SAMPLE"  magic "number" (a string, really)
//        <version>
//        <map_number>            the current map number
//        <time>                  the current game timer's value
//        <num_recreate_ent>      number of re-creatable entities
//        <num_full_ent>          number of fully serialized entities
//        <recreate_ent>...       re-creatable entity data
//        <full_ent>...           fully serialized entities
//        "END_OF_ARCHIVE"        ending key (useful for validating the file)
//
// ---------------------------------------------------------------------------------
VBool LoadGame(int iNum)
{
  if ( (iNum < 1) || (iNum > 6) )
  {
    return FALSE;
  }

  GameLoadData_t snap;

  int i;
  char pszLoadFileName[FS_MAX_PATH];
  char szArchiveTag[FS_MAX_PATH];
  
  sprintf(pszLoadFileName,SAVEGAME_NAME, iNum);

  IVFileInStream* pIn = Vision::File.Open(pszLoadFileName);
  // opening the file didn't work!
  if (!pIn)
    return FALSE;

  VArchive ar( pszLoadFileName, pIn, Vision::GetTypeManager() );

  ar.ReadStringBinary(szArchiveTag, FS_MAX_PATH);
  if (strcmp(szArchiveTag, ARCHIVE_START_TAG) != 0)
  { 
    return FALSE;
  }

  ar >> i;                        // loading version
  ar.SetLoadingVersion(i);
  if (! Vision::IsValidArchive(ar))
  {
    // TODO
    return FALSE;
  }
    
  ar >> snap.iMapNumber;          // map number
  if (snap.iMapNumber == g_iCurrentMap)
    snap.bJustReset = TRUE;
  else
    snap.bJustReset = FALSE;

  ar >> snap.fTime;               // game timer
  ar >> snap.iRecreateEntityCount;// number of re-createable entities
  ar >> snap.iFullEntityCount;    // number of fullly serialized entities

  //snap.bJustReset = FALSE;
  
  if (snap.bJustReset == TRUE)
  {
    // as it's the same map, we just clean up and re-use it :-)
    ResetWorld();
  }
  else
  {
    // load the other map (without entities)
    LoadMap(snap.iMapNumber,FALSE);
  }

  Vision::GetTimer()->SetTime(snap.fTime);

  // now re-create all the "RECREATE" entities
  VMemoryTempBuffer<1024> modelFileName;
  VMemoryTempBuffer<1024> entityParams;
  VMemoryTempBuffer<1024> entityKey;
  VMemoryTempBuffer<1024> className;

  // now re-create all the "RECREATE" entities
  for (i = 0; i < snap.iRecreateEntityCount; i++)
  {
    char* pszClassName;
    char* pszEntityKey;
    hkvVec3 vOrigin;
    hkvVec3 vOrientation;
    char* pszModelFile;
    char* pszEntityParams;
    // get entity data
    pszClassName = className.ReadStringBinary(ar);
    pszEntityKey = entityKey.ReadStringBinary(ar);
    vOrigin.SerializeAsVisVector (ar);
    vOrientation.SerializeAsVisVector (ar);
    pszModelFile = modelFileName.ReadStringBinary(ar);
    pszEntityParams = entityParams.ReadStringBinary(ar);
    SerializeBaseEntity_cl* pSerEnt = static_cast<SerializeBaseEntity_cl*>(
      Vision::Game.CreateEntity(pszClassName, vOrigin, pszModelFile,
      pszEntityParams));
    pSerEnt->SetOrientation(vOrientation);
    pSerEnt->SetEntityKey(pszEntityKey);
  }

  // finally, re-create all the "FULL" entities
  for (i = 0; i < snap.iFullEntityCount; i++)
  {
    SerializeBaseEntity_cl *pSerEnt;
    // get an entity from the archive...
    ar >> pSerEnt;
  }

  // check if the ARCHIVE_END_TAG exists
  ar.ReadStringBinary(szArchiveTag, FS_MAX_PATH);
  if (strcmp(szArchiveTag, ARCHIVE_END_TAG) != 0)
  {
    Vision::Error.FatalError("No (or invalid) end tag found in save game!");
    ar.Close();
    pIn->Close();
    return FALSE;
  }
  
  ar.Close();
  pIn->Close();

  return TRUE;
}
// ReadPlacesFile: Reads in the PLACES_FILE and sets up all the WorldPlace_t
//                 objects.
void WorldLanguages_cl::ReadPlacesFile()
{
  IVFileInStream* pIn = Vision::File.Open(PLACES_FILE);
  char *buffer = NULL;
  
  if (pIn)
  {
    int fsize= pIn->GetSize();
    
    buffer = new char[fsize+1];
    pIn->Read(buffer, fsize);
    buffer[fsize] = 0;
    
    pIn->Close();
  }
  // if we couldn't read anything, return here!
  if (!buffer)
    return;

  static const char Newline[] = "\r\n";
  // tokenize buffer line-wise
  char *line = strtok(buffer, Newline);
  
  // skip first character, it only marks the UTF8 file (Byte Order Mark)
  line += 3;
  // read number of places
  sscanf(line, "%d", &m_iNumPlaces);
  // no places -> return without doing anything
  if (m_iNumPlaces < 1)
  {
    V_SAFE_DELETE_ARRAY(buffer);
    return;
  }
  // create m_iNumPlaces WorldPlace_t structs
  m_pPlaces = new WorldPlace_t[m_iNumPlaces];
  m_iCurrentPlace = 0;

  // get all places
  while (m_iCurrentPlace < m_iNumPlaces)
  {
    WorldPlace_t &pThisPlace = m_pPlaces[m_iCurrentPlace];
    // get next line
    line = strtok(NULL, Newline);
    if (line == NULL)
    {
      V_SAFE_DELETE_ARRAY(buffer);
      return;
    }

    // skip comments
    if (line[0] == ';')
      continue;
    // skip empty lines
    if (line[0] == '\0')
      continue;
    // find [
    if (line[0] == '[')
    {
      // the next line contains the name of the place
      line = strtok(NULL, Newline);
      pThisPlace.m_PlaceName = line;

      //is it Arabic?
      bool bArabic = strcmp(line, "UAE - Arabic") == 0;

      // the next line contains the font type
      line = strtok(NULL, Newline);

      // load font type with resource manager
      pThisPlace.m_spFont = Vision::Fonts.LoadFont(line);

      //left to right or right to left?
      line = strtok(line + strlen(line) + 1, Newline); // LoadFont() seems to use strtok too -> reinitialize
      bool bIsRightToLeft = strcmp(line,"RTL") == 0;

      // the next line contains the alphabet index, x position and y position
      // read alphabet, x_pos, y_pos;
      line = strtok(NULL, Newline);
      float x,y;
      sscanf(line, "%f,%f", &x, &y);
      
      // fix up angles (convert from long/lat to yaw/pitch)
      pThisPlace.m_angleY = -x;
      pThisPlace.m_angleZ = -y-90;
      // the next line(s) contain(s) the text
      // read text
      line = strtok(NULL, Newline);
      pThisPlace.m_Text.Reset();
      // until text is ]
      while (line[0] != ']')
      {
        pThisPlace.m_Text += line;
        pThisPlace.m_Text += "\n";
        line = strtok(NULL, Newline);
      }

      // Init Arabic support helper and set text which gets transformed from uniform to contextual Arabic
      if(bArabic)
      {
        pThisPlace.m_pRTLSupport = new VArabicSupport_cl(pThisPlace.m_spFont, pThisPlace.m_Text);
      }
      else if(bIsRightToLeft)
      {
        pThisPlace.m_pRTLSupport = new VRTLSupport_cl(pThisPlace.m_spFont, pThisPlace.m_Text);
      }

    }

    m_iCurrentPlace++;
  }
  V_SAFE_DELETE_ARRAY(buffer);

}