Files @ r20758:f233b5e2cdd2
Branch filter:

Location: cpp/openttd-patchpack/source/src/gfxinit.cpp - annotation

translators
(svn r25801) -Update from WebTranslator v3.0:
estonian - 4 changes by notAbot
japanese - 5 changes by guppy
slovenian - 1 changes by matej1245
r5584:545d748cc681
r5584:545d748cc681
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6179:c0508e7aefec
r5584:545d748cc681
r7805:7b5af263f275
r5584:545d748cc681
r12767:3d23682b3fda
r5584:545d748cc681
r8123:dde0a9a84019
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r5584:545d748cc681
r8264:d493cb51fe8a
r8264:d493cb51fe8a
r17637:0d7e49507a12
r10062:9a719b26b56e
r10062:9a719b26b56e
r5584:545d748cc681
r5584:545d748cc681
r18009:875021ab0452
r5584:545d748cc681
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r5584:545d748cc681
r5584:545d748cc681
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r7841:0e193e9f1f5b
r5584:545d748cc681
r5584:545d748cc681
r6908:7e03190d606c
r5584:545d748cc681
r18009:875021ab0452
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r5584:545d748cc681
r6908:7e03190d606c
r5584:545d748cc681
r9470:75edf93b1c69
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r7772:260dc421b905
r7772:260dc421b905
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r18009:875021ab0452
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r7772:260dc421b905
r7772:260dc421b905
r7772:260dc421b905
r8432:f08b2af9e176
r19020:24d71054fe13
r8432:f08b2af9e176
r18009:875021ab0452
r8432:f08b2af9e176
r7772:260dc421b905
r7772:260dc421b905
r7772:260dc421b905
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r12674:8254ab83ecad
r12674:8254ab83ecad
r12668:95f5d46a2c14
r7841:0e193e9f1f5b
r17637:0d7e49507a12
r10062:9a719b26b56e
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r10299:def8dff34f3b
r7841:0e193e9f1f5b
r12761:a366bd42c075
r15594:ba7e42fbae24
r13937:dc065e13f2b2
r12759:edb51729a58e
r19019:2eb1d8782aac
r12761:a366bd42c075
r7841:0e193e9f1f5b
r12759:edb51729a58e
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r12674:8254ab83ecad
r12761:a366bd42c075
r13937:dc065e13f2b2
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r12759:edb51729a58e
r19019:2eb1d8782aac
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r11870:92eb16f6d311
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r17630:7d818445376d
r6247:96e840dbefcc
r5584:545d748cc681
r10062:9a719b26b56e
r7805:7b5af263f275
r12668:95f5d46a2c14
r5584:545d748cc681
r17637:0d7e49507a12
r12668:95f5d46a2c14
r5584:545d748cc681
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r17637:0d7e49507a12
r12668:95f5d46a2c14
r5584:545d748cc681
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r7841:0e193e9f1f5b
r9413:fcf267325763
r17637:0d7e49507a12
r18009:875021ab0452
r12668:95f5d46a2c14
r9413:fcf267325763
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r14667:a7ee51a0cfe3
r15630:3f138941b535
r15630:3f138941b535
r15630:3f138941b535
r17635:2e67e206a89e
r17635:2e67e206a89e
r17636:d099f256ef05
r17635:2e67e206a89e
r17635:2e67e206a89e
r17635:2e67e206a89e
r17635:2e67e206a89e
r18007:5ef6f0cf0861
r15630:3f138941b535
r7929:67846e70abf8
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r7882:cdf03a4ac84a
r14667:a7ee51a0cfe3
r7882:cdf03a4ac84a
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18162:11349b218a54
r18162:11349b218a54
r18162:11349b218a54
r18162:11349b218a54
r18162:11349b218a54
r18162:11349b218a54
r18162:11349b218a54
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r17630:7d818445376d
r6247:96e840dbefcc
r5584:545d748cc681
r9413:fcf267325763
r5584:545d748cc681
r18161:7dcc16c70ffe
r19116:609e37882a23
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15707:d8c7fbf1db5d
r15707:d8c7fbf1db5d
r5584:545d748cc681
r9994:9032cf79748f
r15044:862b76e20a87
r10037:46fc9ca9c918
r18368:c399e53439ff
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r18161:7dcc16c70ffe
r10037:46fc9ca9c918
r15044:862b76e20a87
r10037:46fc9ca9c918
r10037:46fc9ca9c918
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r19020:24d71054fe13
r10037:46fc9ca9c918
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r14055:e4168a875df8
r19019:2eb1d8782aac
r12761:a366bd42c075
r17674:8ba1e586d6d1
r17674:8ba1e586d6d1
r17674:8ba1e586d6d1
r10037:46fc9ca9c918
r19019:2eb1d8782aac
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r14055:e4168a875df8
r10037:46fc9ca9c918
r12761:a366bd42c075
r12761:a366bd42c075
r19019:2eb1d8782aac
r19019:2eb1d8782aac
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r10037:46fc9ca9c918
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r12088:b6616548f47d
r12761:a366bd42c075
r12088:b6616548f47d
r12761:a366bd42c075
r12761:a366bd42c075
r10037:46fc9ca9c918
r10037:46fc9ca9c918
r12668:95f5d46a2c14
r13086:71e929261037
r12676:3124748a27b7
r12676:3124748a27b7
r18368:c399e53439ff
r18368:c399e53439ff
r9994:9032cf79748f
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r11049:6eb2c87ed64a
r12761:a366bd42c075
r12668:95f5d46a2c14
r19944:25a78576fb5e
r12761:a366bd42c075
r12761:a366bd42c075
r12761:a366bd42c075
r14619:fff04644c054
r12761:a366bd42c075
r12761:a366bd42c075
r12668:95f5d46a2c14
r17637:0d7e49507a12
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r11049:6eb2c87ed64a
r11049:6eb2c87ed64a
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r11049:6eb2c87ed64a
r11049:6eb2c87ed64a
r12668:95f5d46a2c14
r12668:95f5d46a2c14
r11934:17c3a38acbd0
r12668:95f5d46a2c14
r11934:17c3a38acbd0
r11934:17c3a38acbd0
r12668:95f5d46a2c14
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 */

/** @file gfxinit.cpp Initializing of the (GRF) graphics. */

#include "stdafx.h"
#include "fios.h"
#include "newgrf.h"
#include "3rdparty/md5/md5.h"
#include "fontcache.h"
#include "gfx_func.h"
#include "blitter/factory.hpp"
#include "video/video_driver.hpp"

/* The type of set we're replacing */
#define SET_TYPE "graphics"
#include "base_media_func.h"

#include "table/sprites.h"

/** Whether the given NewGRFs must get a palette remap from windows to DOS or not. */
bool _palette_remap_grf[MAX_FILE_SLOTS];

#include "table/landscape_sprite.h"

/** Offsets for loading the different "replacement" sprites in the files. */
static const SpriteID * const _landscape_spriteindexes[] = {
	_landscape_spriteindexes_arctic,
	_landscape_spriteindexes_tropic,
	_landscape_spriteindexes_toyland,
};

/**
 * Load an old fashioned GRF file.
 * @param filename   The name of the file to open.
 * @param load_index The offset of the first sprite.
 * @param file_index The Fio offset to load the file in.
 * @return The number of loaded sprites.
 */
static uint LoadGrfFile(const char *filename, uint load_index, int file_index)
{
	uint load_index_org = load_index;
	uint sprite_id = 0;

	FioOpenFile(file_index, filename, BASESET_DIR);

	DEBUG(sprite, 2, "Reading grf-file '%s'", filename);

	byte container_ver = GetGRFContainerVersion();
	if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename);
	ReadGRFSpriteOffsets(container_ver);
	if (container_ver >= 2) {
		/* Read compression. */
		byte compression = FioReadByte();
		if (compression != 0) usererror("Unsupported compression format");
	}

	while (LoadNextSprite(load_index, file_index, sprite_id, container_ver)) {
		load_index++;
		sprite_id++;
		if (load_index >= MAX_SPRITES) {
			usererror("Too many sprites. Recompile with higher MAX_SPRITES value or remove some custom GRF files.");
		}
	}
	DEBUG(sprite, 2, "Currently %i sprites are loaded", load_index);

	return load_index - load_index_org;
}

/**
 * Load an old fashioned GRF file to replace already loaded sprites.
 * @param filename   The name of the file to open.
 * @param index_tlb  The offsets of each of the sprites.
 * @param file_index The Fio offset to load the file in.
 * @return The number of loaded sprites.
 */
static void LoadGrfFileIndexed(const char *filename, const SpriteID *index_tbl, int file_index)
{
	uint start;
	uint sprite_id = 0;

	FioOpenFile(file_index, filename, BASESET_DIR);

	DEBUG(sprite, 2, "Reading indexed grf-file '%s'", filename);

	byte container_ver = GetGRFContainerVersion();
	if (container_ver == 0) usererror("Base grf '%s' is corrupt", filename);
	ReadGRFSpriteOffsets(container_ver);
	if (container_ver >= 2) {
		/* Read compression. */
		byte compression = FioReadByte();
		if (compression != 0) usererror("Unsupported compression format");
	}

	while ((start = *index_tbl++) != END) {
		uint end = *index_tbl++;

		do {
			bool b = LoadNextSprite(start, file_index, sprite_id, container_ver);
			assert(b);
			sprite_id++;
		} while (++start <= end);
	}
}

/**
 * Checks whether the MD5 checksums of the files are correct.
 *
 * @note Also checks sample.cat and other required non-NewGRF GRFs for corruption.
 */
void CheckExternalFiles()
{
	if (BaseGraphics::GetUsedSet() == NULL || BaseSounds::GetUsedSet() == NULL) return;

	const GraphicsSet *used_set = BaseGraphics::GetUsedSet();

	DEBUG(grf, 1, "Using the %s base graphics set", used_set->name);

	static const size_t ERROR_MESSAGE_LENGTH = 256;
	static const size_t MISSING_FILE_MESSAGE_LENGTH = 128;

	/* Allocate for a message for each missing file and for one error
	 * message per set.
	 */
	char error_msg[MISSING_FILE_MESSAGE_LENGTH * (GraphicsSet::NUM_FILES + SoundsSet::NUM_FILES) + 2 * ERROR_MESSAGE_LENGTH];
	error_msg[0] = '\0';
	char *add_pos = error_msg;
	const char *last = lastof(error_msg);

	if (used_set->GetNumInvalid() != 0) {
		/* Not all files were loaded successfully, see which ones */
		add_pos += seprintf(add_pos, last, "Trying to load graphics set '%s', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of readme.txt.\n\nThe following files are corrupted or missing:\n", used_set->name);
		for (uint i = 0; i < GraphicsSet::NUM_FILES; i++) {
			MD5File::ChecksumResult res = GraphicsSet::CheckMD5(&used_set->files[i], BASESET_DIR);
			if (res != MD5File::CR_MATCH) add_pos += seprintf(add_pos, last, "\t%s is %s (%s)\n", used_set->files[i].filename, res == MD5File::CR_MISMATCH ? "corrupt" : "missing", used_set->files[i].missing_warning);
		}
		add_pos += seprintf(add_pos, last, "\n");
	}

	const SoundsSet *sounds_set = BaseSounds::GetUsedSet();
	if (sounds_set->GetNumInvalid() != 0) {
		add_pos += seprintf(add_pos, last, "Trying to load sound set '%s', but it is incomplete. The game will probably not run correctly until you properly install this set or select another one. See section 4.1 of readme.txt.\n\nThe following files are corrupted or missing:\n", sounds_set->name);

		assert_compile(SoundsSet::NUM_FILES == 1);
		/* No need to loop each file, as long as there is only a single
		 * sound file. */
		add_pos += seprintf(add_pos, last, "\t%s is %s (%s)\n", sounds_set->files->filename, SoundsSet::CheckMD5(sounds_set->files, BASESET_DIR) == MD5File::CR_MISMATCH ? "corrupt" : "missing", sounds_set->files->missing_warning);
	}

	if (add_pos != error_msg) ShowInfoF("%s", error_msg);
}

/** Actually load the sprite tables. */
static void LoadSpriteTables()
{
	memset(_palette_remap_grf, 0, sizeof(_palette_remap_grf));
	uint i = FIRST_GRF_SLOT;
	const GraphicsSet *used_set = BaseGraphics::GetUsedSet();

	_palette_remap_grf[i] = (PAL_DOS != used_set->palette);
	LoadGrfFile(used_set->files[GFT_BASE].filename, 0, i++);

	/*
	 * The second basic file always starts at the given location and does
	 * contain a different amount of sprites depending on the "type"; DOS
	 * has a few sprites less. However, we do not care about those missing
	 * sprites as they are not shown anyway (logos in intro game).
	 */
	_palette_remap_grf[i] = (PAL_DOS != used_set->palette);
	LoadGrfFile(used_set->files[GFT_LOGOS].filename, 4793, i++);

	/*
	 * Load additional sprites for climates other than temperate.
	 * This overwrites some of the temperate sprites, such as foundations
	 * and the ground sprites.
	 */
	if (_settings_game.game_creation.landscape != LT_TEMPERATE) {
		_palette_remap_grf[i] = (PAL_DOS != used_set->palette);
		LoadGrfFileIndexed(
			used_set->files[GFT_ARCTIC + _settings_game.game_creation.landscape - 1].filename,
			_landscape_spriteindexes[_settings_game.game_creation.landscape - 1],
			i++
		);
	}

	/* Initialize the unicode to sprite mapping table */
	InitializeUnicodeGlyphMap();

	/*
	 * Load the base NewGRF with OTTD required graphics as first NewGRF.
	 * However, we do not want it to show up in the list of used NewGRFs,
	 * so we have to manually add it, and then remove it later.
	 */
	GRFConfig *top = _grfconfig;
	GRFConfig *master = new GRFConfig(used_set->files[GFT_EXTRA].filename);

	/* We know the palette of the base set, so if the base NewGRF is not
	 * setting one, use the palette of the base set and not the global
	 * one which might be the wrong palette for this base NewGRF.
	 * The value set here might be overridden via action14 later. */
	switch (used_set->palette) {
		case PAL_DOS:     master->palette |= GRFP_GRF_DOS;     break;
		case PAL_WINDOWS: master->palette |= GRFP_GRF_WINDOWS; break;
		default: break;
	}
	FillGRFDetails(master, false, BASESET_DIR);

	ClrBit(master->flags, GCF_INIT_ONLY);
	master->next = top;
	_grfconfig = master;

	LoadNewGRF(SPR_NEWGRFS_BASE, i);

	/* Free and remove the top element. */
	delete master;
	_grfconfig = top;
}


/**
 * Check blitter needed by NewGRF config and switch if needed.
 */
static void SwitchNewGRFBlitter()
{
	/* Get blitter of base set. */
	bool is_32bpp = BaseGraphics::GetUsedSet()->blitter == BLT_32BPP;

	/* Get combined blitter mode of all NewGRFs. */
	for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
		if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND || HasBit(c->flags, GCF_INIT_ONLY)) continue;

		if (c->palette & GRFP_BLT_32BPP) is_32bpp = true;
	}

	/* A GRF would like a 32 bpp blitter, switch blitter if needed. Never switch if the blitter was specified by the user. */
	if (_blitter_autodetected && is_32bpp && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 0 && BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() < 16) {
		const char *cur_blitter = BlitterFactoryBase::GetCurrentBlitter()->GetName();
		if (BlitterFactoryBase::SelectBlitter("32bpp-anim") != NULL) {
			if (!_video_driver->AfterBlitterChange()) {
				/* Failed to switch blitter, let's hope we can return to the old one. */
				if (BlitterFactoryBase::SelectBlitter(cur_blitter) == NULL || !_video_driver->AfterBlitterChange()) usererror("Failed to reinitialize video driver for 32 bpp blitter. Specify a fixed blitter in the config");
			}
		}
	}
}

/** Initialise and load all the sprites. */
void GfxLoadSprites()
{
	DEBUG(sprite, 2, "Loading sprite set %d", _settings_game.game_creation.landscape);

	SwitchNewGRFBlitter();
	ClearFontCache();
	GfxInitSpriteMem();
	LoadSpriteTables();
	GfxInitPalettes();

	UpdateCursorSize();
}

bool GraphicsSet::FillSetDetails(IniFile *ini, const char *path, const char *full_filename)
{
	bool ret = this->BaseSet<GraphicsSet, MAX_GFT, true>::FillSetDetails(ini, path, full_filename, false);
	if (ret) {
		IniGroup *metadata = ini->GetGroup("metadata");
		IniItem *item;

		fetch_metadata("palette");
		this->palette = (*item->value == 'D' || *item->value == 'd') ? PAL_DOS : PAL_WINDOWS;

		/* Get optional blitter information. */
		item = metadata->GetItem("blitter", false);
		this->blitter = (item != NULL && *item->value == '3') ? BLT_32BPP : BLT_8BPP;
	}
	return ret;
}

/**
 * Calculate and check the MD5 hash of the supplied GRF.
 * @param file The file get the hash of.
 * @param subdir The sub directory to get the files from.
 * @return
 * - #CR_MATCH if the MD5 hash matches
 * - #CR_MISMATCH if the MD5 does not match
 * - #CR_NO_FILE if the file misses
 */
/* static */ MD5File::ChecksumResult GraphicsSet::CheckMD5(const MD5File *file, Subdirectory subdir)
{
	size_t size = 0;
	FILE *f = FioFOpenFile(file->filename, "rb", subdir, &size);
	if (f == NULL) return MD5File::CR_NO_FILE;

	size_t max = GRFGetSizeOfDataSection(f);

	FioFCloseFile(f);

	return file->CheckMD5(subdir, max);
}


/**
 * Calculate and check the MD5 hash of the supplied filename.
 * @param subdir The sub directory to get the files from
 * @param max_size Only calculate the hash for this many bytes from the file start.
 * @return
 * - #CR_MATCH if the MD5 hash matches
 * - #CR_MISMATCH if the MD5 does not match
 * - #CR_NO_FILE if the file misses
 */
MD5File::ChecksumResult MD5File::CheckMD5(Subdirectory subdir, size_t max_size) const
{
	size_t size;
	FILE *f = FioFOpenFile(this->filename, "rb", subdir, &size);

	if (f == NULL) return CR_NO_FILE;

	size = min(size, max_size);

	Md5 checksum;
	uint8 buffer[1024];
	uint8 digest[16];
	size_t len;

	while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
		size -= len;
		checksum.Append(buffer, len);
	}

	FioFCloseFile(f);

	checksum.Finish(digest);
	return memcmp(this->hash, digest, sizeof(this->hash)) == 0 ? CR_MATCH : CR_MISMATCH;
}

/** Names corresponding to the GraphicsFileType */
static const char * const _graphics_file_names[] = { "base", "logos", "arctic", "tropical", "toyland", "extra" };

/** Implementation */
template <class T, size_t Tnum_files, bool Tsearch_in_tars>
/* static */ const char * const *BaseSet<T, Tnum_files, Tsearch_in_tars>::file_names = _graphics_file_names;

template <class Tbase_set>
/* static */ bool BaseMedia<Tbase_set>::DetermineBestSet()
{
	if (BaseMedia<Tbase_set>::used_set != NULL) return true;

	const Tbase_set *best = NULL;
	for (const Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
		/* Skip unusable sets */
		if (c->GetNumMissing() != 0) continue;

		if (best == NULL ||
				(best->fallback && !c->fallback) ||
				best->valid_files < c->valid_files ||
				(best->valid_files == c->valid_files && (
					(best->shortname == c->shortname && best->version < c->version) ||
					(best->palette != PAL_DOS && c->palette == PAL_DOS)))) {
			best = c;
		}
	}

	BaseMedia<Tbase_set>::used_set = best;
	return BaseMedia<Tbase_set>::used_set != NULL;
}

template <class Tbase_set>
/* static */ const char *BaseMedia<Tbase_set>::GetExtension()
{
	return ".obg"; // OpenTTD Base Graphics
}

INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<GraphicsSet>, GraphicsSet)