Exemple #1
0
//-------- Begin of function GameFile::save_game --------//
//
// return : <int> 1 - saved successfully.
//                0 - not saved.
//
int GameFile::save_game(const char* fileName)
{
	char full_path[MAX_PATH+1];
	File   file;
	String errStr;

	power.win_opened=1;				// to disable power.mouse_handler()

	if( fileName )
		strcpy( file_name, fileName );

	int rc = 1;

	if (!misc.path_cat(full_path, sys.dir_config, file_name, MAX_PATH))
	{
		rc = 0;
		errStr = "Path too long to the saved game";
	}

	char lowDiskSpaceFlag = 0;
#ifndef NO_WINDOWS  // FIXME
	DWORD sectorPerCluster = 0;
	DWORD bytePerSector = 0;
	DWORD freeCluster = 0;
	DWORD totalCluster = 0;
	if( GetDiskFreeSpace( NULL,	// address of root path, NULL means the current root directory
		&sectorPerCluster, &bytePerSector, &freeCluster, &totalCluster))
	{
		DWORD freeSpace = DWORD( (double)freeCluster * sectorPerCluster * bytePerSector / 1024.0);

		if( misc.is_file_exist(file_name) )
		{	
			// if overwritting existing file, count the file size of the existing file
			file.file_open(file_name);
			freeSpace += file.file_size() / 1024;		// count the existing space
			file.file_close();
		}
		if( !(rc = freeSpace >= MIN_FREE_SPACE) )
		{
			errStr = "Insufficient disk space ! The game is not saved.";
			lowDiskSpaceFlag = 1;
		}
	}
#endif

	if( rc )
	{
		rc = file.file_create(full_path, 0, 1); // 0=tell File don't handle error itself
																   // 1=allow the writing size and the read size to be different
		if( !rc )
			errStr = "Error creating saved game file.";
	}

	if( rc )
	{
		save_process();      // process game data before saving the game

		rc = write_game_header(&file);    // write saved game header information

		if( !rc )
			errStr = "Error creating saved game header.";

		if( rc )
		{
			rc = write_file(&file);

			if( !rc )
				errStr = "Error writing saved game data.";
		}
	}

	file.file_close();

	power.win_opened=0;

	//------- when saving error ---------//

	if( !rc )
	{
		if( !lowDiskSpaceFlag )
			remove( file_name );         // delete the file as it is not complete

		#ifndef DEBUG
			errStr = "Insufficient disk space ! The game is not saved.";		// use this message for all types of error message in the release version
		#endif

		box.msg( errStr );
	}

	return rc;
}
Exemple #2
0
//-------- Begin of function GameFile::save_game --------//
//
// return : <int> 1 - saved successfully.
//                0 - not saved.
//
int GameFile::save_game(const char* fileName)
{
	char full_path[MAX_PATH+1];
	File   file;
	String errStr;

	power.win_opened=1;				// to disable power.mouse_handler()

	if( fileName )
		strcpy( file_name, fileName );

	int rc = 1;

	if (!misc.path_cat(full_path, sys.dir_config, file_name, MAX_PATH))
	{
		rc = 0;
		errStr = _("Path too long to the saved game");
	}

	if( rc )
	{
		rc = file.file_create(full_path, 0, 1); // 0=tell File don't handle error itself
																   // 1=allow the writing size and the read size to be different
		if( !rc )
			errStr = _("Error creating saved game file.");
	}

	if( rc )
	{
		save_process();      // process game data before saving the game

		rc = write_game_header(&file);    // write saved game header information

		if( !rc )
			errStr = _("Error creating saved game header.");

		if( rc )
		{
			rc = write_file(&file);

			if( !rc )
				errStr = _("Error writing saved game data.");
		}
	}

	file.file_close();

	power.win_opened=0;

	//------- when saving error ---------//

	if( !rc )
	{
		remove( file_name );         // delete the file as it is not complete

		#ifndef DEBUG
			errStr = _("Insufficient disk space ! The game is not saved.");		// use this message for all types of error message in the release version
		#endif

		box.msg( errStr );
	}

	return rc;
}
Exemple #3
0
//-------- Begin of function GameFile::save_game --------//
//
// <char *> pathName    save game path, without '\' at the end
// <char *> fileName    save game filename, null for unchange, use file_name
// <char *> gameDesc		save game description : null for unchange
//
// return : <int> 1 - saved successfully.
//                0 - not saved.
//
// ###### begin Gilbert 26/5 ########//
int GameFile::save_game(const char *path, const char* fileName, const char *gameDesc)
// ###### end Gilbert 26/5 ########//
{
	File   file;
	String errStr;

	power.win_opened=1;				// to disable power.mouse_handler()

	if( fileName )
	{
		if( path && path[0] != '\0' )		// non empty string
		{
			strcpy(file_name, path);
			strcat(file_name, PATH_DELIM);
			strcat(file_name, fileName);
		}
		else
		{
			strcpy(file_name, fileName);
		}
	}

	// #### begin Gilbert 20/1 ######//
	if( gameDesc )
		strncpy( game_desc, gameDesc, SAVE_GAME_DESC_LEN );
	game_desc[SAVE_GAME_DESC_LEN] = '\0';
	// #### end Gilbert 20/1 ######//

	// ----- test disk space --------//

	int rc = 1;
	char lowDiskSpaceFlag = 0;
#ifndef NO_WINDOWS  // FIXME
	DWORD sectorPerCluster = 0;
	DWORD bytePerSector = 0;
	DWORD freeCluster = 0;
	DWORD totalCluster = 0;
	if( GetDiskFreeSpace( NULL,	// address of root path, NULL means the current root directory
		&sectorPerCluster, &bytePerSector, &freeCluster, &totalCluster))
	{
		DWORD freeSpace = DWORD( (double)freeCluster * sectorPerCluster * bytePerSector / 1024.0);

		if( misc.is_file_exist(file_name) )
		{	
			// if overwritting existing file, count the file size of the existing file
			file.file_open(file_name);
			freeSpace += file.file_size() / 1024;		// count the existing space
			file.file_close();
		}
		if( !(rc = freeSpace >= MIN_FREE_SPACE) )
		{
			errStr = text_game_menu.str_out_of_disk_space(); // "Insufficient disk space ! The game is not saved.";
			lowDiskSpaceFlag = 1;
		}
	}
#endif

	// ##### begin Gilbert 2/11 ######//
	if( rc )
	{
		// create directory
		char *lastSlash = NULL;
		char *s = file_name;
		while( (s = strchr( s, '\\')) )
		{
			lastSlash = s;
			++s;		// start again at next character
		}

		if( lastSlash )
		{
			char backupChar = *lastSlash;
			err_when( backupChar != '\\' );
			*lastSlash = '\0';		// truncate to and not including the last '\\'
			misc.mkpath( file_name );
			*lastSlash = backupChar;		// res
		}
	}
	// ##### end Gilbert 2/11 ######//

	if( rc )
	{
		rc = file.file_create( file_name, 0, 1 );	// 0=tell File don't handle error itself
																   // 1=allow the writing size and the read size to be different
		if( !rc )
			// errStr = "Error creating saved game file.";
			errStr = text_game_menu.str_save_error_create();
	}

	if( rc )
	{
		save_process();      // process game data before saving the game

		rc = write_game_header(&file);    // write saved game header information

		if( !rc )
			errStr = text_game_menu.str_save_error_header(); // "Error creating saved game header.";

		if( rc )
		{
			rc = write_file(&file);

			if( !rc )
				errStr = text_game_menu.str_save_error_general(); // "Error writing saved game data.";
		}
	}

	file.file_close();

	power.win_opened=0;

	//------- when saving error ---------//

	if( !rc )
	{
		if( !lowDiskSpaceFlag )
			remove( file_name );         // delete the file as it is not complete

		#ifndef DEBUG
			// errStr = "Insufficient disk space ! The game is not saved.";		// use this message for all types of error message in the release version
			errStr = text_game_menu.str_out_of_disk_space();
		#endif

		box.msg( errStr );
	}

	return rc;
}