📄 trackerdoc.cpp
字号:
//=========== (C) Copyright 2000 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose: Maintains the tracker document
//=============================================================================
#include "../TrackerNET/TrackerNET_Interface.h"
#include "Buddy.h"
#include "OnlineStatus.h"
#include "ServerSession.h"
#include "Tracker.h"
#include "TrackerDoc.h"
#include "IceKey.h"
#include "Random.h"
#include <stdio.h>
#include <stdlib.h>
#include <VGUI_Controls.h>
#include <VGUI_ISurface.h>
#include <VGUI_KeyValues.h>
#include <VGUI_Panel.h>
#include "FileSystem.h"
//-----------------------------------------------------------------------------
/* Tracker/AppData
AppData contains all the information that is specific to the tracker
application, and contains no user specific information (which is in Tracker/UserData)
AppData is the first set of data which get loaded.
AppData/UID - the userid of the last user to use the app
AppData/Password - the current active password
if no password saved then the user can't auto log on
*/
//-----------------------------------------------------------------------------
// encryptor
IceKey g_Cipher(2); /* high level encryption */
unsigned char g_EncryptionKey[16] = { 63, 12, 129, 32, 173, 194, 222, 1, 77, 16, 24, 99, 123, 132, 197, 201 };
static const int MAX_PASSWORD_LEN = 64;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CTrackerDoc::CTrackerDoc()
{
g_Cipher.set(g_EncryptionKey);
m_BuddyMap.SetLessFunc(BuddyMapItemLessFunc);
// create the base registry
m_pDataRegistry = new vgui::KeyValues("Tracker");
// create the sub registries
// m_pAppData contains all the application-specific data
m_pAppData = m_pDataRegistry->FindKey("App", true);
// m_pUserData contains all the user specific data
// this will be empty until LoadUserData() is called (after the user logs in)
m_pUserData = m_pDataRegistry->FindKey("User", true);
m_pBuddyData = m_pDataRegistry->FindKey("User/Buddies", true);
// load the app file
LoadAppData();
// user data is not loaded UNTIL after a successful login
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CTrackerDoc::~CTrackerDoc()
{
// clear the CBuddy's
for (int i = 0; i < m_BuddyMap.MaxElement(); i++)
{
if (m_BuddyMap.IsValidIndex(i))
{
delete m_BuddyMap[i].pBuddy;
}
}
// delete main data registry
if (m_pDataRegistry)
{
m_pDataRegistry->deleteThis();
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns the userID of the current user, 0 if none
//-----------------------------------------------------------------------------
unsigned int CTrackerDoc::GetUserID()
{
return Data()->GetInt("App/UID", 0);
}
//-----------------------------------------------------------------------------
// Purpose: returns the user name of the current user
//-----------------------------------------------------------------------------
const char *CTrackerDoc::GetUserName()
{
return Data()->GetString("User/UserName");
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBuddy *CTrackerDoc::GetBuddy(unsigned int buddyID)
{
// check in the buddy map first
BuddyMapItem_t searchItem = { buddyID, NULL };
int buddyIndex = m_BuddyMap.Find(searchItem);
if (m_BuddyMap.IsValidIndex(buddyIndex))
{
return m_BuddyMap[buddyIndex].pBuddy;
}
// if it's not in the map, try the raw data
for (KeyValues *dat = m_pBuddyData->GetFirstSubKey(); dat != NULL; dat = dat->GetNextKey())
{
if ((unsigned int)atoi(dat->GetName()) == buddyID)
{
// found it, insert it into cache list
BuddyMapItem_t newItem = { buddyID, new CBuddy(buddyID, dat) };
buddyIndex = m_BuddyMap.Insert(newItem);
return m_BuddyMap[buddyIndex].pBuddy;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Adds a new buddy to the doc
// Input : *buddyData -
//-----------------------------------------------------------------------------
void CTrackerDoc::AddNewBuddy(KeyValues *newBuddy)
{
int buddyID = newBuddy->GetInt("uid");
if (buddyID)
{
// make sure the buddy doesn't already exist
// add in their details
char buf[64];
sprintf(buf, "%d", buddyID);
KeyValues *newKey = m_pBuddyData->FindKey(buf, true);
for (KeyValues *dat = newBuddy->GetFirstSubKey(); dat != NULL; dat = dat->GetNextKey())
{
newKey->SetString(dat->GetName(), dat->GetString());
}
}
}
//-----------------------------------------------------------------------------
// Purpose: moves all the buddies offline
//-----------------------------------------------------------------------------
void CTrackerDoc::ClearBuddyStatus()
{
KeyValues *buddyData = GetDoc()->Data()->FindKey("User/Buddies", true);
for (KeyValues *dat = buddyData->GetFirstSubKey(); dat != NULL; dat = dat->GetNextKey())
{
// doesn't affect any of the negative offline states
if (dat->GetInt("Status") > COnlineStatus::RECENTLYONLINE)
{
dat->SetInt("Status", COnlineStatus::OFFLINE);
}
dat->SetString("Game", "");
dat->SetInt("GameIP", 0);
dat->SetInt("GamePort", 0);
}
}
//-----------------------------------------------------------------------------
// Purpose: Updates the status of a single user
//-----------------------------------------------------------------------------
void CTrackerDoc::UpdateBuddyStatus(unsigned int buddyID, int status, unsigned int sessionID, unsigned int serverID, unsigned int *IP, unsigned int *port, unsigned int *gameIP, unsigned int *gamePort, const char *gameType)
{
CBuddy *buddy = GetBuddy(buddyID);
if (buddy)
{
buddy->UpdateStatus(status, sessionID, serverID, IP, port, gameIP, gamePort, gameType);
}
else
{
// request information about this buddy from the server, as long as it's not us
if (GetUserID() != buddyID)
{
ServerSession().RequestUserInfoFromServer(buddyID);
}
// store off their base details
char buf[64];
sprintf(buf, "%d", buddyID);
KeyValues *bud = m_pBuddyData->FindKey(buf, true);
bud->SetInt("Status", status);
bud->SetInt("ServerID", serverID);
if (sessionID)
{
bud->SetInt("SessionID", sessionID);
}
if (IP)
{
bud->SetInt("IP", *IP);
}
if (port)
{
bud->SetInt("port", *port);
}
if (gameIP)
{
bud->SetInt("GameIP", *gameIP);
}
if (gamePort)
{
bud->SetInt("GamePort", *gamePort);
}
if (gameType)
{
bud->SetString("Game", gameType);
}
// set as not authed by default, until we receive
// confirmation from the server that we are authorized to see this user
bud->SetInt("NotAuthed", 1);
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : buddyID -
// Output : KeyValues
//-----------------------------------------------------------------------------
KeyValues *CTrackerDoc::GetFirstMessage(int buddyID)
{
KeyValues *msgList = GetBuddy(buddyID)->Data()->FindKey("Messages", true);
KeyValues *msg = msgList->GetFirstSubKey();
if (!msg)
{
// no messages
GetDoc()->GetBuddy(buddyID)->Data()->SetInt("MessageAvailable", 0);
return NULL;
}
return msg;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *message -
//-----------------------------------------------------------------------------
void CTrackerDoc::MoveMessageToHistory(int buddyID, KeyValues *msg)
{
KeyValues *msgList = GetBuddy(buddyID)->Data()->FindKey("Messages", true);
msgList->RemoveSubKey(msg);
KeyValues *buddy = GetDoc()->GetBuddy(buddyID)->Data();
/* MESSAGE HISTORY DISABLED
KeyValues *msgHistory = buddy->FindKey("MessageHistory", true);
KeyValues *newMsg = msgHistory->CreateNewKey();
// copy all the data across
for (KeyValues *kv = msg->GetFirstSubKey(); kv != NULL; kv = kv->GetNextKey())
{
newMsg->SetString(kv->GetName(), kv->GetString());
}
*/
// check the message flag
if (msgList->GetFirstSubKey() == NULL)
{
buddy->SetInt("MessageAvailable", 0);
}
if (msg)
{
msg->deleteThis();
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes a buddy from use
//-----------------------------------------------------------------------------
void CTrackerDoc::RemoveBuddy(unsigned int buddyID)
{
// deauthorize the user on the server
ServerSession().SendAuthUserMessage(buddyID, 0);
// set them as removed so they disappear from list
// records will still be kept in database
GetBuddy(buddyID)->Data()->SetInt("Removed", 1);
GetBuddy(buddyID)->UpdateStatus(COnlineStatus::REMOVED, 0, 0, NULL, NULL, NULL, NULL, NULL);
GetBuddy(buddyID)->ShutdownDialogs();
}
//-----------------------------------------------------------------------------
// Purpose: Returns number of buddies in list
//-----------------------------------------------------------------------------
int CTrackerDoc::GetNumberOfBuddies()
{
return m_BuddyMap.Count();
}
//-----------------------------------------------------------------------------
// Purpose: Used to walk buddy list.
// index is in the range [0, GetNumberOfBuddies).
//-----------------------------------------------------------------------------
int CTrackerDoc::GetBuddyByIndex(int index)
{
// walk the list until we find the specified index
int walkCount = 0;
int foundIndex = m_BuddyMap.FirstInorder();
while (walkCount++ < index)
{
foundIndex = m_BuddyMap.NextInorder(foundIndex);
}
if (!m_BuddyMap.IsValidIndex(foundIndex))
{
return 0;
}
return m_BuddyMap[foundIndex].buddyID;
}
//-----------------------------------------------------------------------------
// Purpose: Loads specific user data file, clearing out any old data first
//-----------------------------------------------------------------------------
bool CTrackerDoc::LoadUserData(unsigned int iUID)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -