From b1f8bb8189b3f2feacb3002cf7b08d43b2d80a6d Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 2 Jun 2021 02:08:14 +0300 Subject: [PATCH 65/65] modpack.[ch]: Add initial version See osdn #42445 Signed-off-by: Marko Lindqvist --- common/Makefile.am | 2 + common/modpack.c | 115 +++++++++++++++++++++++++++++++++++++++++++++ common/modpack.h | 37 +++++++++++++++ meson.build | 1 + 4 files changed, 155 insertions(+) create mode 100644 common/modpack.c create mode 100644 common/modpack.h diff --git a/common/Makefile.am b/common/Makefile.am index e346323237..dea42192b0 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -73,6 +73,8 @@ libfreeciv_la_SOURCES = \ mapimg.h \ metaknowledge.c \ metaknowledge.h \ + modpack.c \ + modpack.h \ movement.c \ movement.h \ multipliers.c \ diff --git a/common/modpack.c b/common/modpack.c new file mode 100644 index 0000000000..23b55d2d98 --- /dev/null +++ b/common/modpack.c @@ -0,0 +1,115 @@ +/**************************************************************************** + Freeciv - Copyright (C) 2004 - The Freeciv Team + This program 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; either version 2, or (at your option) + any later version. + + This program 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. +****************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +/* utility */ +#include "capability.h" +#include "registry.h" +#include "section_file.h" +#include "shared.h" + +#include "modpack.h" + + +/************************************************************************//** + Check modpack file capabilties. +****************************************************************************/ +bool modpack_check_capabilities(struct section_file *file, const char *us_capstr, + const char *filename, bool verbose) +{ + enum log_level level = verbose ? LOG_WARN : LOG_DEBUG; + + const char *file_capstr = secfile_lookup_str(file, "datafile.options"); + + if (NULL == file_capstr) { + log_base(level, "\"%s\": file doesn't have a capability string", + filename); + return FALSE; + } + if (!has_capabilities(us_capstr, file_capstr)) { + log_base(level, "\"%s\": file appears incompatible:", + filename); + log_base(level, " datafile options: %s", file_capstr); + log_base(level, " supported options: %s", us_capstr); + return FALSE; + } + if (!has_capabilities(file_capstr, us_capstr)) { + log_base(level, "\"%s\": file requires option(s) " + "that freeciv doesn't support:", filename); + log_base(level, " datafile options: %s", file_capstr); + log_base(level, " supported options: %s", us_capstr); + return FALSE; + } + + return TRUE; +} + +/************************************************************************//** + Get list of modpack meta info files. +****************************************************************************/ +struct fileinfo_list *get_modpacks_list(void) +{ + struct fileinfo_list *files; + + /* search for modpack files. */ + files = fileinfolist_infix(get_data_dirs(), MODPACK_SUFFIX, TRUE); + + return files; +} + +/************************************************************************//** + Return name of the modpack if contains a ruleset. If it does not contain + ruleset, return NULL. +****************************************************************************/ +const char *modpack_has_ruleset(struct section_file *sf) +{ + if (sf != NULL) { + if (!modpack_check_capabilities(sf, MODPACK_CAPSTR, sf->name, + FALSE)) { + return NULL; + } + + if (secfile_lookup_bool_default(sf, FALSE, "components.ruleset")) { + return secfile_lookup_str_default(sf, NULL, "modpack.name"); + } + } + + return NULL; +} + +/************************************************************************//** + Return .serv file name for the modpack, if any. +****************************************************************************/ +const char *modpack_serv_file(struct section_file *sf) +{ + if (sf != NULL) { + return secfile_lookup_str_default(sf, NULL, "ruleset.serv"); + } + + return NULL; +} + +/************************************************************************//** + Return rulesetdir for the modpack, if any. +****************************************************************************/ +const char *modpack_rulesetdir(struct section_file *sf) +{ + if (sf != NULL) { + return secfile_lookup_str_default(sf, NULL, "ruleset.rulesetdir"); + } + + return NULL; +} diff --git a/common/modpack.h b/common/modpack.h new file mode 100644 index 0000000000..772565da9f --- /dev/null +++ b/common/modpack.h @@ -0,0 +1,37 @@ +/*********************************************************************** + Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold + This program 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; either version 2, or (at your option) + any later version. + + This program 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. +***********************************************************************/ +#ifndef FC__MODPACK_H +#define FC__MODPACK_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define MODPACK_CAPSTR "+Freeciv-modpack-3.2-Devel-2021.Jun.01" + +#define MODPACK_SUFFIX ".modpack" + +struct fileinfo_list *get_modpacks_list(void); +const char *modpack_has_ruleset(struct section_file *sf); + +bool modpack_check_capabilities(struct section_file *file, const char *us_capstr, + const char *filename, bool verbose); + +const char *modpack_serv_file(struct section_file *sf); +const char *modpack_rulesetdir(struct section_file *sf); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* FC__MODPACK_H */ diff --git a/meson.build b/meson.build index d02fa768cf..413d7d199f 100644 --- a/meson.build +++ b/meson.build @@ -551,6 +551,7 @@ common_lib = library('freeciv', 'common/map.c', 'common/mapimg.c', 'common/metaknowledge.c', + 'common/modpack.c', 'common/movement.c', 'common/multipliers.c', 'common/nation.c', -- 2.30.2