Пример #1
0
void RankSystem::loadRank(const char* filename)
{
	FILE *bfp = fopen(filename , "rb");
	
	if (!bfp)
	{
		MF_Log("Could not load stats file: %s", filename);
		return;
	}
	
	short int i = 0;
	if (fread(&i, sizeof(short int), 1, bfp) != 1)
	{
		fclose(bfp);
		return;
	}

	
	if (i == RANK_VERSION)
	{
		Stats d;
		char unique[64], name[64];
		if (fread(&i, sizeof(short int), 1, bfp) != 1)
		{
			fclose(bfp);
			return;
		}

		while(i && !feof(bfp))
		{
			TRYREAD(name, i, sizeof(char), bfp);
			TRYREAD(&i, 1, sizeof(short int), bfp);
			TRYREAD(unique, i, sizeof(char) , bfp);
			TRYREAD(&d.tks, 1, sizeof(int), bfp);
			TRYREAD(&d.damage, 1, sizeof(int), bfp);
			TRYREAD(&d.deaths, 1, sizeof(int), bfp);
			TRYREAD(&d.kills, 1, sizeof(int), bfp);
			TRYREAD(&d.shots, 1, sizeof(int), bfp);
			TRYREAD(&d.hits, 1, sizeof(int), bfp);
			TRYREAD(&d.hs, 1, sizeof(int), bfp);
			TRYREAD(&d.points, 1, sizeof(int), bfp);
			TRYREAD(d.bodyHits, 1, sizeof(d.bodyHits), bfp);
			TRYREAD(&i, 1, sizeof(short int), bfp);

			RankSystem::RankStats* a = findEntryInRank( unique , name );
			if ( a ) a->updatePosition( &d );
		}
	}
	
	fclose(bfp);
}
Пример #2
0
// Load Rank Routine
void RankSystem::loadRank()
{
	// Wait for saveRank THREAD to Finish (Important)
	if(h_saveRank != NULL)
		WaitForSingleObject(h_saveRank, INFINITE);

	// Open Stats file for Reading
	const char* filename = MF_BuildPathname("%s", get_localinfo("csstats"));	// Path to csstats file
	FILE* bfp = fopen(filename, "rb");
	if(bfp == NULL)
	{
		MF_SyncLog("loadRank: Could not load csstats file: \"%s\"", filename);
		return;
	}

	// Get RANK_VERSION (1 Byte)
	int16_t* buffer = new int16_t;
	if(fread(buffer, sizeof(int16_t), 1, bfp) != 1 || *buffer != RANK_VERSION)
	{
		MF_SyncLog("loadRank: Invalid RANK_VERSION Found!");
		fclose(bfp);
		return;
	}

	// Init
	Stats d;
	char *unique = NULL, *name = NULL;

	// Clear Memory before Load
	g_rank.clear();

	// Load
	if(fread(buffer, sizeof(int16_t), 1, bfp) != 1)	// Pre Check
	{
		fclose(bfp);
		return;
	}
	while(buffer != NULL && *buffer && !feof(bfp))
	{
		name = new char[*buffer];
		if(fread(name, sizeof(char), *buffer, bfp) != *buffer)	break;
		if(fread(buffer, sizeof(int16_t), 1, bfp) != 1)	break;	// unique length
		unique = new char[*buffer];
		if(fread(unique, sizeof(char), *buffer, bfp) != *buffer)	break;
		if(fread(&d.tks, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.damage, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.deaths, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.kills, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.shots, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.hits, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.hs, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.bDefusions, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.bDefused, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.bPlants, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(&d.bExplosions, sizeof(int32_t), 1, bfp) != 1)	break;
		if(fread(d.bodyHits, sizeof(d.bodyHits), 1, bfp) != 1)	break;
		if(fread(buffer, sizeof(int16_t), 1, bfp) != 1)	break;	// name length or NULL Terminator

		// Precise Check: Duplicate Entries ( Defalult: Discard Lower Rank Entries )
		RankSystem::RankStats* temp = findEntryInRank(unique);
		if(temp == NULL)
		{
			temp = newEntryInRank(unique, name);
			if(temp != NULL)
				temp->updatePosition(&d, true);
			else
			{
				MF_SyncLog("loadRank: Unable to load any more Stats on Server");
				fclose(bfp);
				return;
			}
		}

		delete[] name;
		delete[] unique;
	}
	fclose(bfp);
}