bool fs2netd_check_mission(char *mission_name)
{
	int rc = 0;
	char popup_string[256];

	if ( !Logged_in ) {
		return 0;
	}

	strcpy_s(Chk_mission_name, mission_name);
	cf_chksum_long(Chk_mission_name, &Chk_mission_crc);

	do_full_packet = true;

	In_process = true;

	memset(popup_string, 0, sizeof(popup_string));
	sprintf(popup_string, XSTR("Validating mission %s", 1074), mission_name);

	if (Is_standalone) {
		do { rc = fs2netd_check_mission_do(); } while (!rc);
	} else {
		rc = popup_till_condition(fs2netd_check_mission_do, XSTR("&Cancel", 779), popup_string);
	}

	In_process = false;
	Local_timeout = -1;

	switch (rc) {
		// operation canceled, or invalid
		case 0:
			return false;

		// successful, but invalid
		case 1:
			return false;

		// successful and valid
		case 2:
			return true;

		// failed to send request packet
		case 3:
			if ( !Is_standalone ) {
				popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR("Server request failed!",1580));
			}

			return false;

		// it timed out
		case 4:
			if ( !Is_standalone ) {
				popup(PF_USE_AFFIRMATIVE_ICON, 1, POPUP_OK, XSTR("Server request timed out!", 1581));
			}

			return false;
	}

	return false;
}
void fs2netd_add_table_validation(char *tblname)
{
	uint chksum = 0;

	// if the tbl name isn't valid then just assume that the tbl is too
	if ( (tblname == NULL) || !strlen(tblname) ) {
		return;
	}

	CFILE *tbl = cfopen(tblname, "rt", CFILE_NORMAL, CF_TYPE_TABLES);

	if (tbl == NULL) {
		return;
	}

	cf_chksum_long(tbl, &chksum);

	cfclose(tbl);

	crc_valid_status tbl_crc;

	strncpy(tbl_crc.name, tblname, NAME_LENGTH);
	tbl_crc.crc32 = chksum;
	tbl_crc.valid = 0;

	Table_valid_status.push_back( tbl_crc );
}
int fs2netd_get_valid_missions_do()
{
	if (Local_timeout == -1) {
		Local_timeout = timer_get_seconds() + 30;
	}

	// get the available CRCs from the server if we need to
	if ( FS2NetD_file_list.empty() ) {
		int rc = FS2NetD_GetMissionsList(FS2NetD_file_list, do_full_packet);

		do_full_packet = false;

		// communications error
		if (rc < 0) {
			Local_timeout = -1;
			return 4;
		}

		// no missions
		if ( rc && FS2NetD_file_list.empty() ) {
			Local_timeout = -1;
			return 2;
		}

		// if timeout passes then bail on crc failure
		if ( timer_get_seconds() > Local_timeout ) {
			Local_timeout = -1;
			return 1;
		}
	}
	// we should have the CRCs, or there were no missions, so process them
	else {
		static char **file_names = NULL;
		static int idx = 0, count = 0;

		bool found = false;
		int file_index = 0;
		char valid_status = MVALID_STATUS_UNKNOWN;
		char full_name[MAX_FILENAME_LEN], wild_card[10];
		char val_text[MAX_FILENAME_LEN+15];
		uint checksum = 0;

		if (file_names == NULL) {
			// allocate filename space	
			file_names = (char**) vm_malloc_q( sizeof(char*) * 1024 ); // 1024 files should be safe!

			if (file_names == NULL) {
				Local_timeout = -1;
				return 3;
			}

			memset( wild_card, 0, sizeof(wild_card) );
			strcpy_s( wild_card, NOX("*") );
			strcat_s( wild_card, FS_MISSION_FILE_EXT );

			idx = count = cf_get_file_list(1024, file_names, CF_TYPE_MISSIONS, wild_card);
		}

		// drop idx first thing
		idx--;

		// we should be done validating, or just not have nothing to validate
		if (idx < 0) {
			for (idx = 0; idx < count; idx++) {
				if (file_names[idx] != NULL) {
					vm_free(file_names[idx]);
					file_names[idx] = NULL;
				}
			}

			vm_free(file_names);
			file_names = NULL;

			idx = count = 0;

			Local_timeout = -1;
			return 4;
		}


		// verify all filenames that we know about with their CRCs
		// NOTE: that this is done for one file per frame, since this is inside of a popup
		memset( full_name, 0, MAX_FILENAME_LEN );
		strncpy( full_name, cf_add_ext(file_names[idx], FS_MISSION_FILE_EXT), sizeof(full_name) - 1 );

		memset( val_text, 0, sizeof(val_text) );
		snprintf( val_text, sizeof(val_text) - 1, "Validating:  %s", full_name );

		if (Is_standalone) {
			if ( std_gen_is_active() ) {
				std_gen_set_text(val_text, 1);
			}
		} else {
			popup_change_text(val_text);
		}

		cf_chksum_long(full_name, &checksum);

		// try and find the file
		file_index = multi_create_lookup_mission(full_name);

		found = false;

		if (file_index >= 0) {
			for (SCP_vector<file_record>::iterator fr = FS2NetD_file_list.begin(); fr != FS2NetD_file_list.end() && !found; ++fr) {
				if ( !stricmp(full_name, fr->name) ) {
					if (fr->crc32 == checksum) {
						found = true;
						valid_status = MVALID_STATUS_VALID;
					} else {
						valid_status = MVALID_STATUS_INVALID;
					}

					Multi_create_mission_list[file_index].valid_status = valid_status;
				}
			}

			if (found) {
				ml_printf("FS2NetD Mission Validation: %s  =>  Valid!", full_name);
			} else {
				ml_printf("FS2NetD Mission Validation: %s  =>  INVALID! -- 0x%08x", full_name, checksum);
			}
		}
	}

	return 0;
}
void fs2netd_debrief_init()
{
	if ( !(Game_mode & GM_MULTIPLAYER) ) {
		return;
	}

	if ( !Om_tracker_flag ) {
		return;
	}

	if ( !Is_connected ) {
		return;
	}


	uint CurrentMissionChsum;
	bool mValidStatus = false;

	cf_chksum_long(Netgame.mission_name, &CurrentMissionChsum);

	mValidStatus = FS2NetD_CheckSingleMission(Netgame.mission_name, CurrentMissionChsum);

	if ( ((multi_num_players() > 1) || (Multi_num_players_at_start > 1)) && !game_hacked_data() && mValidStatus ) {
		// verify that we are logged in before doing anything else
		fs2netd_login();

		int spd_ret = FS2NetD_SendPlayerData(PXO_SID, Players[Player_num].callsign, Multi_tracker_login, &Players[Player_num]);

		switch (spd_ret) { // 0 = pilot updated, 1  = invalid pilot, 2 = invalid (expired?) sid
			case -1:
				multi_display_chat_msg( XSTR("<Did not receive response from server within timeout period>", -1), 0, 0 );
				multi_display_chat_msg( XSTR("<Your stats may not have been stored>", -1), 0, 0 );
				multi_display_chat_msg( XSTR("<This is not a critical error>", -1), 0, 0 );
				Multi_debrief_stats_accept_code = 1;
				break;

			case 0:
				multi_display_chat_msg( XSTR("<stats have been accepted>", 850), 0, 0 );
				Multi_debrief_stats_accept_code = 1;
				break;

			case 1:
				multi_display_chat_msg( XSTR("<stats have been tossed>", 850), 0, 0 );
				multi_display_chat_msg( XSTR("WARNING: Your pilot was invalid, this is a serious error, possible data corruption", -1), 0, 0 );
				Multi_debrief_stats_accept_code = 0;
				break;

			case 2:
				// we really shouldn't be here with the new code, but handle it just in case
				Int3();
			
				fs2netd_login();

				if (PXO_SID != -1) {
					if ( !FS2NetD_SendPlayerData(PXO_SID, Players[Player_num].callsign, Multi_tracker_login, &Players[Player_num]) ) {
						multi_display_chat_msg( XSTR("<stats have been accepted>", 850), 0, 0 );
						Multi_debrief_stats_accept_code = 1;
						break;
					 }
				}

				multi_display_chat_msg( XSTR("<stats have been tossed>", 851), 0, 0 );
				Multi_debrief_stats_accept_code = 0;
				break;

			default:
				multi_display_chat_msg( XSTR("Unknown Stats Store Request Reply", -1), 0, 0 );
				break;
		}
	} else {
		multi_display_chat_msg( XSTR("<stats have been tossed>", 851), 0, 0 );
		Multi_debrief_stats_accept_code = 0;
	}
}