(svn r17133) -Codechange: generalise the code that searches for base graphics
parent
cb6b1accc4
commit
cdb969e164
@ -0,0 +1,193 @@
|
||||
/* $Id$ */
|
||||
|
||||
/** @file base_media_base.h Generic functions for replacing base data (graphics, sounds). */
|
||||
|
||||
#ifndef BASE_MEDIA_BASE_H
|
||||
#define BASE_MEDIA_BASE_H
|
||||
|
||||
#include "fileio_func.h"
|
||||
|
||||
/** Structure holding filename and MD5 information about a single file */
|
||||
struct MD5File {
|
||||
const char *filename; ///< filename
|
||||
uint8 hash[16]; ///< md5 sum of the file
|
||||
const char *missing_warning; ///< warning when this file is missing
|
||||
|
||||
bool CheckMD5() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* Information about a single base set.
|
||||
* @tparam T the real class we're going to be
|
||||
* @tparam Tnum_files the number of files in the set
|
||||
*/
|
||||
template <class T, size_t Tnum_files>
|
||||
struct BaseSet {
|
||||
/** Number of files in this set */
|
||||
static const size_t NUM_FILES = Tnum_files;
|
||||
|
||||
/** Internal names of the files in this set. */
|
||||
static const char *file_names[Tnum_files];
|
||||
|
||||
const char *name; ///< The name of the base set
|
||||
const char *description; ///< Description of the base set
|
||||
uint32 shortname; ///< Four letter short variant of the name
|
||||
uint32 version; ///< The version of this base set
|
||||
|
||||
MD5File files[NUM_FILES]; ///< All files part of this set
|
||||
uint found_files; ///< Number of the files that could be found
|
||||
|
||||
T *next; ///< The next base set in this list
|
||||
|
||||
/** Free everything we allocated */
|
||||
~BaseSet()
|
||||
{
|
||||
free((void*)this->name);
|
||||
free((void*)this->description);
|
||||
for (uint i = 0; i < NUM_FILES; i++) {
|
||||
free((void*)this->files[i].filename);
|
||||
free((void*)this->files[i].missing_warning);
|
||||
}
|
||||
|
||||
delete this->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of missing files.
|
||||
* @return the number
|
||||
*/
|
||||
int GetNumMissing() const
|
||||
{
|
||||
return Tnum_files - this->found_files;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the set information from a loaded ini.
|
||||
* @param ini the ini to read from
|
||||
* @param path the path to this ini file (for filenames)
|
||||
* @return true if loading was successful.
|
||||
*/
|
||||
bool FillSetDetails(struct IniFile *ini, const char *path);
|
||||
};
|
||||
|
||||
/**
|
||||
* Base for all base media (graphics, sound)
|
||||
* @tparam Tbase_set the real set we're going to be
|
||||
*/
|
||||
template <class Tbase_set>
|
||||
class BaseMedia : FileScanner {
|
||||
protected:
|
||||
static Tbase_set *available_sets; ///< All available sets
|
||||
static const Tbase_set *used_set; ///< The currently used set
|
||||
|
||||
/* virtual */ bool AddFile(const char *filename, size_t basepath_length);
|
||||
|
||||
/**
|
||||
* Get the extension that is used to identify this set.
|
||||
* @return the extension
|
||||
*/
|
||||
static const char *GetExtension();
|
||||
public:
|
||||
/** The set as saved in the config file. */
|
||||
static const char *ini_set;
|
||||
|
||||
/**
|
||||
* Determine the graphics pack that has to be used.
|
||||
* The one with the most correct files wins.
|
||||
* @return true if a best set has been found.
|
||||
*/
|
||||
static bool DetermineBestSet();
|
||||
|
||||
/** Do the scan for files. */
|
||||
static uint FindSets()
|
||||
{
|
||||
BaseMedia<Tbase_set> fs;
|
||||
return fs.Scan(GetExtension(), DATA_DIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the set to be used.
|
||||
* @param name of the set to use
|
||||
* @return true if it could be loaded
|
||||
*/
|
||||
static bool SetSet(const char *name);
|
||||
|
||||
/**
|
||||
* Returns a list with the sets.
|
||||
* @param p where to print to
|
||||
* @param last the last character to print to
|
||||
* @return the last printed character
|
||||
*/
|
||||
static char *GetSetsList(char *p, const char *last);
|
||||
|
||||
/**
|
||||
* Count the number of available graphics sets.
|
||||
* @return the number of sets
|
||||
*/
|
||||
static int GetNumSets();
|
||||
|
||||
/**
|
||||
* Get the index of the currently active graphics set
|
||||
* @return the current set's index
|
||||
*/
|
||||
static int GetIndexOfUsedSet();
|
||||
|
||||
/**
|
||||
* Get the name of the graphics set at the specified index
|
||||
* @return the name of the set
|
||||
*/
|
||||
static const Tbase_set *GetSet(int index);
|
||||
/**
|
||||
* Return the used set.
|
||||
* @return the used set.
|
||||
*/
|
||||
static const Tbase_set *GetUsedSet();
|
||||
|
||||
/**
|
||||
* Check whether we have an set with the exact characteristics as ci.
|
||||
* @param ci the characteristics to search on (shortname and md5sum)
|
||||
* @param md5sum whether to check the MD5 checksum
|
||||
* @return true iff we have an set matching.
|
||||
*/
|
||||
static bool HasSet(const struct ContentInfo *ci, bool md5sum);
|
||||
};
|
||||
|
||||
|
||||
/** Types of graphics in the base graphics set */
|
||||
enum GraphicsFileType {
|
||||
GFT_BASE, ///< Base sprites for all climates
|
||||
GFT_LOGOS, ///< Logos, landscape icons and original terrain generator sprites
|
||||
GFT_ARCTIC, ///< Landscape replacement sprites for arctic
|
||||
GFT_TROPICAL, ///< Landscape replacement sprites for tropical
|
||||
GFT_TOYLAND, ///< Landscape replacement sprites for toyland
|
||||
GFT_EXTRA, ///< Extra sprites that were not part of the original sprites
|
||||
MAX_GFT ///< We are looking for this amount of GRFs
|
||||
};
|
||||
|
||||
/** All data of a graphics set. */
|
||||
struct GraphicsSet : BaseSet<GraphicsSet, MAX_GFT> {
|
||||
PaletteType palette; ///< Palette of this graphics set
|
||||
|
||||
/**
|
||||
* Is this set useable? Are enough files found to think it exists.
|
||||
* @return true if it's useable.
|
||||
*/
|
||||
bool IsUseable() const
|
||||
{
|
||||
/* Do not find 'only' openttd[dw].grf */
|
||||
return this->found_files > 1;
|
||||
}
|
||||
|
||||
bool FillSetDetails(struct IniFile *ini, const char *path);
|
||||
};
|
||||
|
||||
/** All data/functions related with replacing the base graphics. */
|
||||
class BaseGraphics : public BaseMedia<GraphicsSet> {
|
||||
public:
|
||||
/**
|
||||
* Determine the palette of the current graphics set.
|
||||
*/
|
||||
static void DeterminePalette();
|
||||
};
|
||||
|
||||
#endif /* BASE_MEDIA_BASE_H */
|
@ -0,0 +1,301 @@
|
||||
/* $Id$ */
|
||||
|
||||
/** @file base_media_func.h Generic function implementations for base data (graphics, sounds). */
|
||||
|
||||
#include "base_media_base.h"
|
||||
#include "string_func.h"
|
||||
#include "ini_type.h"
|
||||
|
||||
template <class Tbase_set> /* static */ const char *BaseMedia<Tbase_set>::ini_set;
|
||||
template <class Tbase_set> /* static */ const Tbase_set *BaseMedia<Tbase_set>::used_set;
|
||||
template <class Tbase_set> /* static */ Tbase_set *BaseMedia<Tbase_set>::available_sets;
|
||||
|
||||
/**
|
||||
* Try to read a single piece of metadata and return false if it doesn't exist.
|
||||
* @param name the name of the item to fetch.
|
||||
*/
|
||||
#define fetch_metadata(name) \
|
||||
item = metadata->GetItem(name, false); \
|
||||
if (item == NULL || strlen(item->value) == 0) { \
|
||||
DEBUG(grf, 0, "Base " SET_TYPE "set detail loading: %s field missing", name); \
|
||||
return false; \
|
||||
}
|
||||
|
||||
template <class T, size_t Tnum_files>
|
||||
bool BaseSet<T, Tnum_files>::FillSetDetails(IniFile *ini, const char *path)
|
||||
{
|
||||
memset(this, 0, sizeof(*this));
|
||||
|
||||
IniGroup *metadata = ini->GetGroup("metadata");
|
||||
IniItem *item;
|
||||
|
||||
fetch_metadata("name");
|
||||
this->name = strdup(item->value);
|
||||
|
||||
fetch_metadata("description");
|
||||
this->description = strdup(item->value);
|
||||
|
||||
fetch_metadata("shortname");
|
||||
for (uint i = 0; item->value[i] != '\0' && i < 4; i++) {
|
||||
this->shortname |= ((uint8)item->value[i]) << (i * 8);
|
||||
}
|
||||
|
||||
fetch_metadata("version");
|
||||
this->version = atoi(item->value);
|
||||
|
||||
/* For each of the file types we want to find the file, MD5 checksums and warning messages. */
|
||||
IniGroup *files = ini->GetGroup("files");
|
||||
IniGroup *md5s = ini->GetGroup("md5s");
|
||||
IniGroup *origin = ini->GetGroup("origin");
|
||||
for (uint i = 0; i < Tnum_files; i++) {
|
||||
MD5File *file = &this->files[i];
|
||||
/* Find the filename first. */
|
||||
item = files->GetItem(BaseSet<T, Tnum_files>::file_names[i], false);
|
||||
if (item == NULL) {
|
||||
DEBUG(grf, 0, "No " SET_TYPE " file for: %s", BaseSet<T, Tnum_files>::file_names[i]);
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *filename = item->value;
|
||||
file->filename = str_fmt("%s%s", path, filename);
|
||||
|
||||
/* Then find the MD5 checksum */
|
||||
item = md5s->GetItem(filename, false);
|
||||
if (item == NULL) {
|
||||
DEBUG(grf, 0, "No MD5 checksum specified for: %s", filename);
|
||||
return false;
|
||||
}
|
||||
char *c = item->value;
|
||||
for (uint i = 0; i < sizeof(file->hash) * 2; i++, c++) {
|
||||
uint j;
|
||||
if ('0' <= *c && *c <= '9') {
|
||||
j = *c - '0';
|
||||
} else if ('a' <= *c && *c <= 'f') {
|
||||
j = *c - 'a' + 10;
|
||||
} else if ('A' <= *c && *c <= 'F') {
|
||||
j = *c - 'A' + 10;
|
||||
} else {
|
||||
DEBUG(grf, 0, "Malformed MD5 checksum specified for: %s", filename);
|
||||
return false;
|
||||
}
|
||||
if (i % 2 == 0) {
|
||||
file->hash[i / 2] = j << 4;
|
||||
} else {
|
||||
file->hash[i / 2] |= j;
|
||||
}
|
||||
}
|
||||
|
||||
/* Then find the warning message when the file's missing */
|
||||
item = origin->GetItem(filename, false);
|
||||
if (item == NULL) item = origin->GetItem("default", false);
|
||||
if (item == NULL) {
|
||||
DEBUG(grf, 1, "No origin warning message specified for: %s", filename);
|
||||
file->missing_warning = strdup("");
|
||||
} else {
|
||||
file->missing_warning = strdup(item->value);
|
||||
}
|
||||
|
||||
if (file->CheckMD5()) this->found_files++;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class Tbase_set>
|
||||
bool BaseMedia<Tbase_set>::AddFile(const char *filename, size_t basepath_length)
|
||||
{
|
||||
bool ret = false;
|
||||
DEBUG(grf, 1, "Checking %s for base " SET_TYPE " set", filename);
|
||||
|
||||
Tbase_set *set = new Tbase_set();;
|
||||
IniFile *ini = new IniFile();
|
||||
ini->LoadFromDisk(filename);
|
||||
|
||||
char *path = strdup(filename + basepath_length);
|
||||
char *psep = strrchr(path, PATHSEPCHAR);
|
||||
if (psep != NULL) {
|
||||
psep[1] = '\0';
|
||||
} else {
|
||||
*path = '\0';
|
||||
}
|
||||
|
||||
if (set->FillSetDetails(ini, path)) {
|
||||
Tbase_set *duplicate = NULL;
|
||||
for (Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
|
||||
if (strcmp(c->name, set->name) == 0 || c->shortname == set->shortname) {
|
||||
duplicate = c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (duplicate != NULL) {
|
||||
/* The more complete set takes precedence over the version number. */
|
||||
if ((duplicate->found_files == set->found_files && duplicate->version >= set->version) ||
|
||||
duplicate->found_files > set->found_files) {
|
||||
DEBUG(grf, 1, "Not adding %s (%i) as base " SET_TYPE " set (duplicate)", set->name, set->version);
|
||||
delete set;
|
||||
} else {
|
||||
Tbase_set **prev = &BaseMedia<Tbase_set>::available_sets;
|
||||
while (*prev != duplicate) prev = &(*prev)->next;
|
||||
|
||||
*prev = set;
|
||||
set->next = duplicate->next;
|
||||
/* don't allow recursive delete of all remaining items */
|
||||
duplicate->next = NULL;
|
||||
|
||||
DEBUG(grf, 1, "Removing %s (%i) as base " SET_TYPE " set (duplicate)", duplicate->name, duplicate->version);
|
||||
delete duplicate;
|
||||
ret = true;
|
||||
}
|
||||
} else {
|
||||
Tbase_set **last = &BaseMedia<Tbase_set>::available_sets;
|
||||
while (*last != NULL) last = &(*last)->next;
|
||||
|
||||
*last = set;
|
||||
ret = true;
|
||||
}
|
||||
if (ret) {
|
||||
DEBUG(grf, 1, "Adding %s (%i) as base " SET_TYPE " set", set->name, set->version);
|
||||
}
|
||||
} else {
|
||||
delete set;
|
||||
}
|
||||
free(path);
|
||||
|
||||
delete ini;
|
||||
return ret;
|
||||
}
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ bool BaseMedia<Tbase_set>::SetSet(const char *name)
|
||||
{
|
||||
extern void CheckExternalFiles();
|
||||
|
||||
if (StrEmpty(name)) {
|
||||
if (!BaseMedia<Tbase_set>::DetermineBestSet()) return false;
|
||||
CheckExternalFiles();
|
||||
return true;
|
||||
}
|
||||
|
||||
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
|
||||
if (strcmp(name, s->name) == 0) {
|
||||
BaseMedia<Tbase_set>::used_set = s;
|
||||
CheckExternalFiles();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ char *BaseMedia<Tbase_set>::GetSetsList(char *p, const char *last)
|
||||
{
|
||||
p += seprintf(p, last, "List of " SET_TYPE " sets:\n");
|
||||
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
|
||||
if (!s->IsUseable()) continue;
|
||||
|
||||
p += seprintf(p, last, "%18s: %s", s->name, s->description);
|
||||
int missing = s->GetNumMissing();
|
||||
if (missing != 0) {
|
||||
p += seprintf(p, last, " (missing %i file%s)\n", missing, missing == 1 ? "" : "s");
|
||||
} else {
|
||||
p += seprintf(p, last, "\n");
|
||||
}
|
||||
}
|
||||
p += seprintf(p, last, "\n");
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
#if defined(ENABLE_NETWORK)
|
||||
#include "network/network_content.h"
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
|
||||
{
|
||||
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
|
||||
if (!s->IsUseable()) continue;
|
||||
|
||||
if (s->shortname != ci->unique_id) continue;
|
||||
if (!md5sum) return true;
|
||||
|
||||
byte md5[16];
|
||||
memset(md5, 0, sizeof(md5));
|
||||
for (uint i = 0; i < Tbase_set::NUM_FILES; i++) {
|
||||
for (uint j = 0; j < sizeof(md5); j++) {
|
||||
md5[j] ^= s->files[i].hash[j];
|
||||
}
|
||||
}
|
||||
if (memcmp(md5, ci->md5sum, sizeof(md5)) == 0) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ bool BaseMedia<Tbase_set>::HasSet(const ContentInfo *ci, bool md5sum)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif /* ENABLE_NETWORK */
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ int BaseMedia<Tbase_set>::GetNumSets()
|
||||
{
|
||||
int n = 0;
|
||||
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
|
||||
if (s != BaseMedia<Tbase_set>::used_set && !s->IsUseable()) continue;
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ int BaseMedia<Tbase_set>::GetIndexOfUsedSet()
|
||||
{
|
||||
int n = 0;
|
||||
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
|
||||
if (s == BaseMedia<Tbase_set>::used_set) return n;
|
||||
if (!s->IsUseable()) continue;
|
||||
n++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ const Tbase_set *BaseMedia<Tbase_set>::GetSet(int index)
|
||||
{
|
||||
for (const Tbase_set *s = BaseMedia<Tbase_set>::available_sets; s != NULL; s = s->next) {
|
||||
if (s != BaseMedia<Tbase_set>::used_set && !s->IsUseable()) continue;
|
||||
if (index == 0) return s;
|
||||
index--;
|
||||
}
|
||||
error("Base" SET_TYPE "::GetSet(): index %d out of range", index);
|
||||
}
|
||||
|
||||
template <class Tbase_set>
|
||||
/* static */ const Tbase_set *BaseMedia<Tbase_set>::GetUsedSet()
|
||||
{
|
||||
return BaseMedia<Tbase_set>::used_set;
|
||||
}
|
||||
|
||||
/**
|
||||
* Force instantiation of methods so we don't get linker errors.
|
||||
* @param repl_type the type of the BaseMedia to instantiate
|
||||
* @param set_type the type of the BaseSet to instantiate
|
||||
*/
|
||||
#define INSTANTIATE_BASE_MEDIA_METHODS(repl_type, set_type) \
|
||||
template const char *repl_type::ini_set; \
|
||||
template const char *repl_type::GetExtension(); \
|
||||
template bool repl_type::AddFile(const char *filename, size_t pathlength); \
|
||||
template bool repl_type::HasSet(const struct ContentInfo *ci, bool md5sum); \
|
||||
template bool repl_type::SetSet(const char *name); \
|
||||
template char *repl_type::GetSetsList(char *p, const char *last); \
|
||||
template int repl_type::GetNumSets(); \
|
||||
template int repl_type::GetIndexOfUsedSet(); \
|
||||
template const set_type *repl_type::GetSet(int index); \
|
||||
template const set_type *repl_type::GetUsedSet(); \
|
||||
template bool repl_type::DetermineBestSet();
|
||||
|
Loading…
Reference in New Issue