//===============================================================================================
// FUNCTION: Read
// PURPOSE:  Reads the complete protocol from the data file.
//
BOOL CABF2ProtocolReader::Read( UINT fFlags )
{
   MEMBERASSERT();

   BOOL bOK = TRUE;
   bOK &= m_pFI->Seek( 0L, FILE_BEGIN);
   if( !bOK )
      return FALSE;

   bOK &= m_pFI->Read( &m_FileInfo, sizeof( m_FileInfo ) );

   if( m_FileInfo.StringsSection.uBlockIndex )
   {
      // Read the protocol strings into the cache.
      UINT uSeekPos = m_FileInfo.StringsSection.uBlockIndex * ABF_BLOCKSIZE;
      if( !m_Strings.Read( m_pFI->GetFileHandle(), uSeekPos ) )
         return FALSE;     //SetLastError( ABF_ENOSTRINGS );
   }

   bOK &= ReadFileInfo();
   bOK &= ReadProtocolInfo();
   bOK &= ReadADCInfo();
   bOK &= ReadDACInfo();
   bOK &= ReadEpochs();
   bOK &= ReadStats();
   bOK &= ReadUserList();
   bOK &= ReadMathInfo();

   RemoveExtraChannels( m_pFH, fFlags );
   FlattenGearShift( m_pFH );

   return bOK;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CTFStatPanel::CTFStatPanel( const char *pElementName )
	: EditablePanel( NULL, "StatPanel" ), CHudElement( pElementName )
{
	// Assert that all defined stats are in our prioritized list or explicitly unused
	Assert( ARRAYSIZE( g_statPriority ) + ARRAYSIZE( g_statUnused ) == TFSTAT_MAX );

	ResetDisplayedStat();
	m_bStatsChanged = false;
	m_bLocalFileTrusted = false;
	m_flTimeLastSpawn = 0;
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );
	m_bShouldBeVisible = false;
	SetScheme( "ClientScheme" );
	statPanel = this;
	m_bNeedToCalcMaxs = false;

	m_pClassImage = new CTFClassImage( this, "StatPanelClassImage" );
	m_iClassCurrentLife = TF_CLASS_UNDEFINED;
	m_iTeamCurrentLife = TEAM_UNASSIGNED;

	// Read stats from disk.  (Definitive stat store for X360; for PC, whatever we get from Steam is authoritative.)
	ReadStats();

	RegisterForRenderGroup( "mid" );
}
//-----------------------------------------------------------------------------
bool psNPCLoader::LoadFromFile(csString &filename)
{
    area.Clear();

    CPrintf(CON_DEBUG, "Importing NPC from file: %s\n",filename.GetData());

    // try to read data from the file

    csRef<iVFS> vfs =  csQueryRegistry<iVFS> (psserver->GetObjectReg());
    csRef<iDataBuffer> data(vfs->ReadFile(filename.GetData()));
    if(!data || !data->GetSize())
    {
        CPrintf(CON_ERROR, "Error: Couldn't load file '%s'.\n", filename.GetData());
        return false;
    }

    csRef<iDocumentSystem> xml;
    xml.AttachNew(new csTinyDocumentSystem);
    csRef<iDocument> doc = xml->CreateDocument();
    const char* error = doc->Parse(data);
    if(error)
    {
        Error2("Error in XML: %s", error);
        return false;
    }
    npcRoot = doc->GetRoot()->GetNode("npc");

    if(!npcRoot)
    {
        CPrintf(CON_ERROR, "Error: no <npc> tag found\n");
        return false;
    }


    dialogManager = new psDialogManager;
    npc = new psCharacter;
    npc->SetCharType(PSCHARACTER_TYPE_NPC);

    // process xml data
    if(!ReadBasicInfo() || !ReadLocation())
    {
        CPrintf(CON_ERROR, "Error: Failed to load NPC data\n");
        return false;
    }
    ReadDescription();
    ReadTraits();
    ReadStats();
    ReadMoney();
    ReadSkills();
    ReadTrainerInfo();
    ReadMerchantInfo();
    ReadFactions();
    ReadKnowledgeAreas();
    ReadSpecificKnowledge();
    ReadSpecialResponses();

    // insert processed data into the database
    if(!WriteToDatabase())
    {
        CPrintf(CON_ERROR, "Error: Failed to insert data into the database\n");
        return false;
    }

    return true;
}