/* --------- local code */
static struct wad_data *get_physics_wad_data(
	bool *bungie_physics)
{
	struct wad_data *wad= NULL;
	
//	dprintf("Open is: %d %d %.*s", physics_file.vRefNum, physics_file.parID, physics_file.name[0], physics_file.name+1);

	OpenedFile PhysicsFile;
	if(open_wad_file_for_reading(PhysicsFileSpec,PhysicsFile))
	{
		struct wad_header header;

		if(read_wad_header(PhysicsFile, &header))
		{
			if(header.data_version==BUNGIE_PHYSICS_DATA_VERSION || header.data_version==PHYSICS_DATA_VERSION)
			{
				wad= read_indexed_wad_from_file(PhysicsFile, &header, 0, true);
				if(header.data_version==BUNGIE_PHYSICS_DATA_VERSION)
				{
					*bungie_physics= true;
				} else {
					*bungie_physics= false;
				}
			}
		}

		close_wad_file(PhysicsFile);
	} 
	
	/* Reset any errors that might have occurred.. */
	set_game_error(systemError, errNone);

	return wad;
}
Beispiel #2
0
SDL_Surface *WadImageCache::image_from_desc(WadImageDescriptor& desc)
{
	SDL_Surface *surface = NULL;
	OpenedFile wad_file;
	if (open_wad_file_for_reading(desc.file, wad_file))
	{
		struct wad_header header;
		if (read_wad_header(wad_file, &header))
		{
			struct wad_data *wad;
			wad = read_indexed_wad_from_file(wad_file, &header, desc.index, true);
			if (wad)
			{
				void *data;
				size_t length;
				data = extract_type_from_wad(wad, desc.tag, &length);
				if (data && length)
				{
					SDL_RWops *rwops = SDL_RWFromConstMem(data, length);
#ifdef HAVE_SDL_IMAGE
					surface = IMG_Load_RW(rwops, 1);
#else
					surface = SDL_LoadBMP_RW(rwops, 1);
#endif
				}
				free_wad(wad);
			}
		}
		close_wad_file(wad_file);
	}
	clear_game_error();
	return surface;
}
Beispiel #3
0
static void load_preferences(
  void)
{
  /* If it was already allocated, we are reloading, so free the old one.. */
  if(prefInfo->wad) {
    free_wad(prefInfo->wad);
    prefInfo->wad= NULL;
  }

  OpenedFile PrefsFile;
  if (open_wad_file_for_reading(prefInfo->PrefsFile,PrefsFile)) {
    struct wad_header header;

    /* Read the header from the wad file */
    if(read_wad_header(PrefsFile, &header)) {
      /* Read the indexed wad from the file */
      prefInfo->wad= read_indexed_wad_from_file(PrefsFile, &header, 0, false);
      // LP change: more graceful degradation
      if (!prefInfo->wad) {
        set_game_error(gameError, errUnknownWadVersion);
      }
    }

    /* Close the file.. */
    close_wad_file(PrefsFile);
  }
}
Beispiel #4
0
void w_write_preferences_file(
  void)
{
  struct wad_header header;

  /* We can be called atexit. */
  if(error_pending()) {
    set_game_error(systemError, errNone);
  }

  assert(!error_pending());

  // LP: need to re-create that file to avoid
  // nonexistence-induced errors in the MacOS version
  Typecode Type = prefInfo->PrefsFile.GetType();

  // Setting to default if _typecode_unknown
  if (Type == _typecode_unknown) {
    Type = _typecode_preferences;
  }

  // CB: delete old prefs file, we're overwriting it
  // (writing can go wrong when the old file is still in place)
  prefInfo->PrefsFile.Delete();

  // Re-creating the file
  // CB: is Create() supposed to truncate the file to zero length
  // if it already exists? Then the Delete() call above can go away.
  prefInfo->PrefsFile.Create(Type);

  OpenedFile PrefsFile;
  if (open_wad_file_for_writing(prefInfo->PrefsFile,PrefsFile)) {
    struct directory_entry entry;

    fill_default_wad_header(prefInfo->PrefsFile,
                            CURRENT_WADFILE_VERSION,
                            CURRENT_PREF_WADFILE_VERSION,
                            1, 0l,
                            &header);

    if (write_wad_header(PrefsFile, &header)) {
      int32 wad_length;
      int32 offset= sizeof(struct wad_header);

      /* Length? */
      wad_length= calculate_wad_length(&header, prefInfo->wad);

      /* Set the entry data..... (and put into correct byte-order for writing) */
      set_indexed_directory_offset_and_length(&header,
                                              &entry, 0, offset, wad_length, 0);

      /* Now write it.. */
      if (write_wad(PrefsFile, &header, prefInfo->wad, offset)) {
        offset+= wad_length;
        header.directory_offset= offset;
        if (write_wad_header(PrefsFile, &header) &&
            write_directorys(PrefsFile, &header, &entry)) {
          /* Success! */
        }
        else {
          assert(error_pending());
        }
      }
      else {
        assert(error_pending());
      }

      /* Since we don't free it, it is opened.. */
    }
    else {
      assert(error_pending());
    }
    close_wad_file(PrefsFile);
  }
}
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();
	}
}