⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trackerdoc.cpp

📁 hl2 source code. Do not use it illegal.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
{
	// clear the current user data
	m_pUserData->Clear();
	for (int i = 0; i < m_BuddyMap.MaxElement(); i++)
	{
		if (m_BuddyMap.IsValidIndex(i))
		{
			delete m_BuddyMap[i].pBuddy;
		}
	}
	m_BuddyMap.RemoveAll();

	// make up the path to look for the data file
	char szUserFile[128];
	sprintf( szUserFile, "UserData_%d.vdf", iUID );
	m_pUserData->LoadFromFile(vgui::filesystem(), szUserFile, "CONFIG");

	m_pUserData = m_pDataRegistry->FindKey("User", true);
	m_pBuddyData = m_pDataRegistry->FindKey("User/Buddies", true);

	// reset any buddy status
	GetDoc()->ClearBuddyStatus();

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Saves the current user data
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CTrackerDoc::SaveUserData( void )
{
	int uid = m_pAppData->GetInt("uid");
	if (!uid)
		return false;

	// make up the path to look for the data file
	char szUserFile[128];
	sprintf( szUserFile, "UserData_%d.vdf", uid );

	// write out the userdata registry into the file
	m_pUserData->SaveToFile(vgui::filesystem(), szUserFile, "CONFIG");

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Loads in application data
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CTrackerDoc::LoadAppData( void )
{
	// clear the current app data
	m_pAppData->Clear();

	// load from file
	m_pAppData->LoadFromFile(vgui::filesystem(), "AppData.vdf", "CONFIG");

	// load in the password for auto-login
	FileHandle_t f = vgui::filesystem()->Open("masters.dat", "rb", "CONFIG");
	if (f)
	{
		char encryptedPassword[MAX_PASSWORD_LEN], password[MAX_PASSWORD_LEN];

		// load encrypted password from file
		vgui::filesystem()->Read(encryptedPassword, MAX_PASSWORD_LEN, f);
		vgui::filesystem()->Close(f);

		// decrypt 8 bytes at a time
		for (int i = 0; i < MAX_PASSWORD_LEN; i += 8)
		{
			g_Cipher.decrypt((unsigned char *)(encryptedPassword + i), (unsigned char *)(password + i));
		}

		// set the deciphered password in the doc
		m_pAppData->SetString("password", password);
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Saves out application data
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CTrackerDoc::SaveAppData( void )
{
	// kill the UID password in the app data when saving it
	char password[MAX_PASSWORD_LEN];
	memset(password, 0, MAX_PASSWORD_LEN);
	strncpy(password, m_pAppData->GetString("password"), MAX_PASSWORD_LEN - 1);
	password[MAX_PASSWORD_LEN - 1] = 0;

	// clear password
	m_pAppData->SetString("password", "");

	// make up the path to look for the data file

	// save the data
	m_pAppData->SaveToFile(vgui::filesystem(), "AppData.vdf", "CONFIG");

	// reset password
	m_pAppData->SetString("password", password);


	// encrypt the password
	int pos = strlen(password) + 1;

	// munge the end of the password, deterministically
	while (pos < MAX_PASSWORD_LEN)
	{
		password[pos] = 'a' + pos;
		pos++;
	}

	char encryptedPassword[MAX_PASSWORD_LEN];
	// encryption acts 8 bytes at a time
	for (int i = 0; i < MAX_PASSWORD_LEN; i += 8)
	{
		g_Cipher.encrypt((unsigned char *)(password + i), (unsigned char *)(encryptedPassword + i));
	}
	
	// save the password out to a seperate file
	FileHandle_t f = vgui::filesystem()->Open("masters.dat", "wb", "CONFIG");
	if (f)
	{
		vgui::filesystem()->Write(encryptedPassword, MAX_PASSWORD_LEN, f);
		vgui::filesystem()->Close(f);
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTrackerDoc::SaveDialogState(vgui::Panel *dialog, const char *dialogName, unsigned int buddyID)
{
	// write the size and position to the document
	int x, y, wide, tall;
	dialog->GetBounds(x, y, wide, tall);

	KeyValues *data;
	if (buddyID)
	{
		data = GetBuddy(buddyID)->Data()->FindKey("Dialogs", true);
	}
	else
	{
		data = Data()->FindKey("User/Dialogs", true);
	}

	data = data->FindKey(dialogName, true);

	// write, tracker running standalone
	if (Tracker_StandaloneMode())
	{
		data->SetInt("x", x);
		data->SetInt("y", y);
		data->SetInt("w", wide);
		data->SetInt("t", tall);
	}
	else
	{
		// write, tracker running in game
		data->SetInt("game_x", x);
		data->SetInt("game_y", y);
		data->SetInt("game_w", wide);
		data->SetInt("game_t", tall);
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTrackerDoc::LoadDialogState(vgui::Panel *dialog, const char *dialogName, unsigned int buddyID)
{
	// read the size and position from the document
	KeyValues *data;
	if (buddyID)
	{
		data = GetBuddy(buddyID)->Data()->FindKey("Dialogs", true);
	}
	else
	{
		data = Data()->FindKey("User/Dialogs", true);
	}
	data = data->FindKey(dialogName, true);

	// calculate defaults
	int x, y, wide, tall, dwide, dtall;
	int nx, ny, nwide, ntall;
	vgui::surface()->GetScreenSize(wide, tall);
	dialog->GetSize(dwide, dtall);
	x = (wide - dwide) / 2;
	y = (tall - dtall) / 2;

	// set dialog

	if (Tracker_StandaloneMode())
	{
		nx = data->GetInt("x", x);
		ny = data->GetInt("y", y);
		nwide = data->GetInt("w", dwide);
		ntall = data->GetInt("t", dtall);
	}
	else
	{
		nx = data->GetInt("game_x", x);
		ny = data->GetInt("game_y", y);
		nwide = data->GetInt("game_w", dwide);
		ntall = data->GetInt("game_t", dtall);	
	}

	// make sure it's on the screen
	if (nx + nwide > wide)
	{
		nx = wide - nwide;
	}
	if (ny + ntall > tall)
	{
		ny = tall - ntall;
	}
	if (nx < 0)
	{
		nx = 0;
	}
	if (ny < 0)
	{
		ny = 0;
	}

	dialog->SetBounds(nx, ny, nwide, ntall);
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTrackerDoc::ShutdownAllDialogs()
{
	// clear the CBuddy's
	for (int i = 0; i < m_BuddyMap.MaxElement(); i++)
	{
		if (m_BuddyMap.IsValidIndex(i))
		{
			m_BuddyMap[i].pBuddy->ShutdownDialogs();
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTrackerDoc::WriteOut(KeyValues *dat, ISendMessage *outFile)
{
	// data items with no type yet, must have no info, so never serialize them
	if (dat->IsEmpty(NULL))
		return;

	// write out the data
	if (dat->GetFirstSubKey())
	{
		// iterate through saving all sub items in this key
		for (vgui::KeyValues *it = dat->GetFirstSubKey(); it != NULL; it = it->GetNextKey())
		{
			WriteOut(it, outFile);
		}
	}
	else
	{
		// single item
		switch (dat->GetDataType(NULL))
		{
		case vgui::KeyValues::TYPE_INT:
			{
				int idat = dat->GetInt(NULL);
				outFile->WriteInt(dat->GetName(), idat);
			}
			break;

		case vgui::KeyValues::TYPE_STRING:
			{
				const char *str = dat->GetString(NULL);
				outFile->WriteString(dat->GetName(), str);
			}
			break;

		default:
			// do nothing
			break;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CTrackerDoc::ReadIn(KeyValues *dat, IReceiveMessage *inFile)
{
	// loop through reading until there are no more fields
	while (inFile->ReadField())
	{
		// find/create the field to set
		KeyValues *newDat = dat->FindKey(inFile->GetFieldName(), true);

		switch (inFile->GetFieldType())
		{
		case 1:	// binary, read it as an int
			{
				int idat;
				inFile->GetFieldData(&idat, 4);
				newDat->SetInt(NULL, idat);
			}
			break;

		case 0:	// string
			{
				static char buf[1024];
				inFile->GetFieldData(buf, 1023);
				newDat->SetString(NULL, buf);
			}
			break;

		default:
			// unknown field type
			break;
		};

		// advance to the next field
		inFile->AdvanceField();
	}
}

//-----------------------------------------------------------------------------
// Purpose: comparison function for buddy items
//-----------------------------------------------------------------------------
bool CTrackerDoc::BuddyMapItemLessFunc(const BuddyMapItem_t &r1, const BuddyMapItem_t &r2)
{
	return (r1.buddyID < r2.buddyID);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -