bool physics_file_is_m1(void)
{
    bool m1_physics = false;
    
    // check for M1 physics
    OpenedFile PhysicsFile;
    short SavedType, SavedError = get_game_error(&SavedType);
    if (PhysicsFileSpec.Open(PhysicsFile))
    {
        uint32 tag = SDL_ReadBE32(PhysicsFile.GetRWops());
        switch (tag)
        {
            case M1_MONSTER_PHYSICS_TAG:
            case M1_EFFECTS_PHYSICS_TAG:
            case M1_PROJECTILE_PHYSICS_TAG:
            case M1_PHYSICS_PHYSICS_TAG:
            case M1_WEAPONS_PHYSICS_TAG:
                m1_physics = true;
                break;
            default:
                break;
        }
        
        PhysicsFile.Close();
    }
    set_game_error(SavedType, SavedError);
    return m1_physics;
}
void *get_network_physics_buffer(
	int32 *physics_length)
{
	if (physics_file_is_m1())
	{
		bool success = false;
		uint8 *data = NULL;
		OpenedFile PhysicsFile;
		if (PhysicsFileSpec.Open(PhysicsFile) &&
		    PhysicsFile.GetLength(*physics_length))
		{
			data = (uint8 *)malloc(*physics_length + 4);
            SDL_RWops *ops = SDL_RWFromMem(data, *physics_length + 4);
            success = SDL_WriteBE32(ops, uint32(M1_PHYSICS_MAGIC_COOKIE));
            if (success)
                success = SDL_WriteBE32(ops, uint32(*physics_length));
            SDL_RWclose(ops);
            if (success)
                success = PhysicsFile.Read(*physics_length, &data[8]);
			if (!success)
				free(data);
		}
		if (!success)
		{
			*physics_length = 0;
			return NULL;
		}
		return data;
	}
	
	short SavedType, SavedError = get_game_error(&SavedType);
	void *data= get_flat_data(PhysicsFileSpec, false, 0);
	set_game_error(SavedType, SavedError);
	
	if(data)
	{
		*physics_length= get_flat_data_length(data);
	} else {
		*physics_length= 0;
	}
	
	return data;
}
Exemplo n.º 3
0
/*  preferences pointer.. */
bool w_open_preferences_file(
  char *PrefName,
  Typecode Type)
{
  int error = 0;
  bool success= true;

  /* allocate space for our global structure to keep track of the prefs file */
  prefInfo = NULL;
  try {
    prefInfo = new preferences_info;

#if defined(mac)
    prefInfo->PrefsFile.SetParentToPreferences();
    prefInfo->PrefsFile.SetName(PrefName,Type);
    /* check for the preferences folder using FindFolder, creating it if necessary */
#elif defined(SDL)
    prefInfo->PrefsFile.SetToPreferencesDir();
    prefInfo->PrefsFile += PrefName;
#endif

    /* does the preferences file exist? */
    load_preferences();             /* Uses prefInfo.. */

    if(error_pending()) {
      short type;

      error= get_game_error(&type);
      if(type==systemError) {
        if (!prefInfo->PrefsFile.Exists()) {
          prefInfo->PrefsFile.Create(Type);
          prefInfo->wad = create_empty_wad();
          set_game_error(systemError,prefInfo->PrefsFile.GetError());
          w_write_preferences_file();
        }
      }
      else {
        /* Something was invalid.. */
        if (prefInfo->PrefsFile.Delete()) {
          prefInfo->wad= create_empty_wad();
          set_game_error(systemError, error);
          w_write_preferences_file();
        }
        set_game_error(systemError, errNone);
      }
    }
  } catch (...) {
    dprintf("In \"catch\"");
    set_game_error(systemError, memory_error());
  }

  if (error) {
    /* if something is broken, make sure we at least return valid prefs */
    if(prefInfo && !prefInfo->wad) {
      prefInfo->wad= create_empty_wad();
    }
  }

  /* Gotta bail... */
  if(!prefInfo || !prefInfo->wad) {
    success= false;
  }

// dump_wad(prefInfo->wad);

  return success;
}
Exemplo n.º 4
0
void create_updated_save(QuickSave& save)
{
	// read data from existing save file
	struct wad_header header;
	struct wad_data *game_wad, *orig_meta_wad, *new_meta_wad;
	int32 game_wad_length;
	std::string imagedata;
	
	OpenedFile currentFile;
	if (save.save_file.Open(currentFile))
	{
		if (read_wad_header(currentFile, &header))
		{
			game_wad = read_indexed_wad_from_file(currentFile, &header, 0, false);
			if (game_wad) game_wad_length = calculate_wad_length(&header, game_wad);

			orig_meta_wad = read_indexed_wad_from_file(currentFile, &header, SAVE_GAME_METADATA_INDEX, true);
			
			if (orig_meta_wad)
			{
				size_t data_length;
				char *raw_imagedata = (char *)extract_type_from_wad(orig_meta_wad, SAVE_IMG_TAG, &data_length);
				imagedata = std::string(raw_imagedata, data_length);
			}
		}
		close_wad_file(currentFile);
	}
	
	// create updated save file
	short err = 0;
	int32 offset, meta_wad_length;
	struct directory_entry entries[2];
	
	FileSpecifier TempFile;
	TempFile.SetTempName(save.save_file);
	
	if (create_wadfile(TempFile, _typecode_savegame))
	{
		OpenedFile SaveFile;
		if(open_wad_file_for_writing(TempFile, SaveFile))
		{
			if (write_wad_header(SaveFile, &header))
			{
				offset = SIZEOF_wad_header;
				
				set_indexed_directory_offset_and_length(&header, entries, 0, offset, game_wad_length, 0);
				
				if (write_wad(SaveFile, &header, game_wad, offset))
				{
					offset += game_wad_length;
					header.directory_offset= offset;
					
					new_meta_wad = build_meta_game_wad(build_save_metadata(save), imagedata, &header, &meta_wad_length);
					if (new_meta_wad)
					{
						set_indexed_directory_offset_and_length(&header, entries, 1, offset, meta_wad_length, SAVE_GAME_METADATA_INDEX);
						
						if (write_wad(SaveFile, &header, new_meta_wad, offset))
						{
							offset += meta_wad_length;
							header.directory_offset= offset;
							
							if (write_wad_header(SaveFile, &header) && write_directorys(SaveFile, &header, entries))
							{
							}
						}
						free_wad(new_meta_wad);
					}
				}
				free_wad(game_wad);
				free_wad(orig_meta_wad);
			}

			err = SaveFile.GetError();
			close_wad_file(SaveFile);
		}
		
		if (!err)
		{
			if (!TempFile.Rename(save.save_file))
			{
				err = 1;
			}
		}
	}
	
	if (err || error_pending())
	{
		if (!err) err = get_game_error(NULL);
		alert_user(infoError, strERRORS, fileError, err);
		clear_game_error();
	}
}