Freeciv-3.3: /fast/freeciv/nightly/main/common/modpack.c Source File

Freeciv-3.3

Loading...
Searching...
No Matches
modpack.c
Go to the documentation of this file.
1/****************************************************************************
2 Freeciv - Copyright (C) 2004 - The Freeciv Team
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12****************************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18/* utility */
19#include "capability.h"
20#include "registry.h"
21#include "section_file.h"
22#include "shared.h"
23
24/* common */
25#include "game.h"
26
27#include "modpack.h"
28
29 struct modpack_cache_item {
30 char *modpack_name;
31 char *filename;
32};
33
34/* get 'struct modpack_cache_list' and related functions: */
35#define SPECLIST_TAG modpack_cache
36#define SPECLIST_TYPE struct modpack_cache_item
37#include "speclist.h"
38
39#define modpack_cache_iterate(mplist, item) \
40 TYPED_LIST_ITERATE(struct modpack_cache_item, mplist, item)
41#define modpack_cache_iterate_end LIST_ITERATE_END
42
43 static struct modpack_cache_list *modpack_rulesets;
44
45/************************************************************************/
48 void modpacks_init(void)
49{
50 if (is_server()) {
51 modpack_rulesets = modpack_cache_list_new();
52 }
53}
54
55/************************************************************************/
58 void modpacks_free(void)
59{
60 if (is_server()) {
61 modpack_cache_iterate(modpack_rulesets, item) {
62 free(item->modpack_name);
63 free(item->filename);
64 free(item);
65 } modpack_cache_iterate_end;
66
67 modpack_cache_list_destroy(modpack_rulesets);
68 }
69}
70
71/************************************************************************/
74 bool modpack_check_capabilities(struct section_file *file, const char *us_capstr,
75 const char *filename, bool verbose)
76{
77 enum log_level level = verbose ? LOG_WARN : LOG_DEBUG;
78
79 const char *file_capstr = secfile_lookup_str(file, "datafile.options");
80
81 if (NULL == file_capstr) {
82 log_base(level, "\"%s\": file doesn't have a capability string",
83 filename);
84 return FALSE;
85 }
86 if (!has_capabilities(us_capstr, file_capstr)) {
87 log_base(level, "\"%s\": file appears incompatible:",
88 filename);
89 log_base(level, " datafile options: %s", file_capstr);
90 log_base(level, " supported options: %s", us_capstr);
91 return FALSE;
92 }
93 if (!has_capabilities(file_capstr, us_capstr)) {
94 log_base(level, "\"%s\": file requires option(s) "
95 "that freeciv doesn't support:", filename);
96 log_base(level, " datafile options: %s", file_capstr);
97 log_base(level, " supported options: %s", us_capstr);
98 return FALSE;
99 }
100
101 return TRUE;
102}
103
104/************************************************************************/
107 struct fileinfo_list *get_modpacks_list(void)
108{
109 struct fileinfo_list *files;
110
111 /* search for modpack files. */
112 files = fileinfolist_infix(get_data_dirs(), MODPACK_SUFFIX, TRUE);
113
114 return files;
115}
116
117/************************************************************************/
121 const char *modpack_has_ruleset(struct section_file *sf)
122{
123 if (sf != NULL) {
124 if (!modpack_check_capabilities(sf, MODPACK_CAPSTR, sf->name,
125 FALSE)) {
126 return NULL;
127 }
128
129 if (secfile_lookup_bool_default(sf, FALSE, "components.ruleset")) {
130 return secfile_lookup_str_default(sf, NULL, "modpack.name");
131 }
132 }
133
134 return NULL;
135}
136
137/************************************************************************/
140 const char *modpack_serv_file(struct section_file *sf)
141{
142 if (sf != NULL) {
143 return secfile_lookup_str_default(sf, NULL, "ruleset.serv");
144 }
145
146 return NULL;
147}
148
149/************************************************************************/
152 const char *modpack_rulesetdir(struct section_file *sf)
153{
154 if (sf != NULL) {
155 return secfile_lookup_str_default(sf, NULL, "ruleset.rulesetdir");
156 }
157
158 return NULL;
159}
160
161/************************************************************************/
165 const char *modpack_cache_ruleset(struct section_file *sf)
166{
167 const char *mp_name = modpack_has_ruleset(sf);
168 struct modpack_cache_item *item;
169
170 if (mp_name == NULL) {
171 return NULL;
172 }
173
174 fc_assert(sf->name != NULL);
175
176 item = fc_malloc(sizeof(struct modpack_cache_item));
177 item->modpack_name = fc_strdup(mp_name);
178 item->filename = fc_strdup(sf->name);
179
180 modpack_cache_list_append(modpack_rulesets, item);
181
182 return mp_name;
183}
184
185/************************************************************************/
188 const char *modpack_file_from_ruleset_cache(const char *name)
189{
190 modpack_cache_iterate(modpack_rulesets, item) {
191 if (!fc_strcasecmp(name, item->modpack_name)) {
192 return item->filename;
193 }
194 } modpack_cache_iterate_end;
195
196 return NULL;
197}
198
199/************************************************************************/
202 void modpack_ruleset_cache_iterate(mrc_cb cb, void *data)
203{
204 modpack_cache_iterate(modpack_rulesets, item) {
205 cb(item->modpack_name, item->filename, data);
206 } modpack_cache_iterate_end;
207}
has_capabilities
bool has_capabilities(const char *us, const char *them)
Definition capability.c:86
capability.h
incite_cost
char * incite_cost
Definition comments.c:74
is_server
static bool is_server(void)
Definition fc_interface.h:69
game.h
name
const char * name
Definition inputfile.c:127
fc_assert
#define fc_assert(condition)
Definition log.h:176
log_base
#define log_base(level, message,...)
Definition log.h:94
log_level
log_level
Definition log.h:28
LOG_DEBUG
@ LOG_DEBUG
Definition log.h:34
LOG_WARN
@ LOG_WARN
Definition log.h:31
fc_strdup
#define fc_strdup(str)
Definition mem.h:43
fc_malloc
#define fc_malloc(sz)
Definition mem.h:34
modpack_rulesets
static struct modpack_cache_list * modpack_rulesets
Definition modpack.c:43
modpacks_free
void modpacks_free(void)
Definition modpack.c:58
modpack_has_ruleset
const char * modpack_has_ruleset(struct section_file *sf)
Definition modpack.c:121
modpack_ruleset_cache_iterate
void modpack_ruleset_cache_iterate(mrc_cb cb, void *data)
Definition modpack.c:202
modpack_check_capabilities
bool modpack_check_capabilities(struct section_file *file, const char *us_capstr, const char *filename, bool verbose)
Definition modpack.c:74
modpack_file_from_ruleset_cache
const char * modpack_file_from_ruleset_cache(const char *name)
Definition modpack.c:188
modpack_cache_iterate_end
#define modpack_cache_iterate_end
Definition modpack.c:41
modpack_serv_file
const char * modpack_serv_file(struct section_file *sf)
Definition modpack.c:140
modpacks_init
void modpacks_init(void)
Definition modpack.c:48
modpack_rulesetdir
const char * modpack_rulesetdir(struct section_file *sf)
Definition modpack.c:152
get_modpacks_list
struct fileinfo_list * get_modpacks_list(void)
Definition modpack.c:107
modpack_cache_ruleset
const char * modpack_cache_ruleset(struct section_file *sf)
Definition modpack.c:165
modpack_cache_iterate
#define modpack_cache_iterate(mplist, item)
Definition modpack.c:39
modpack.h
mrc_cb
void(* mrc_cb)(const char *, const char *, void *data)
Definition modpack.h:39
MODPACK_SUFFIX
#define MODPACK_SUFFIX
Definition modpack.h:22
MODPACK_CAPSTR
#define MODPACK_CAPSTR
Definition modpack.h:20
registry.h
secfile_lookup_str
const char * secfile_lookup_str(const struct section_file *secfile, const char *path,...)
Definition registry_ini.c:2175
secfile_lookup_bool_default
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
Definition registry_ini.c:1900
secfile_lookup_str_default
const char * secfile_lookup_str_default(const struct section_file *secfile, const char *def, const char *path,...)
Definition registry_ini.c:2205
section_file.h
level
struct setting_list * level[OLEVELS_NUM]
Definition settings.c:190
get_data_dirs
const struct strvec * get_data_dirs(void)
Definition shared.c:893
fileinfolist_infix
struct fileinfo_list * fileinfolist_infix(const struct strvec *dirs, const char *infix, bool nodups)
Definition shared.c:1211
shared.h
speclist.h
item
Definition climisc.h:80
modpack_cache_item
Definition modpack.c:29
modpack_cache_item::filename
char * filename
Definition modpack.c:31
modpack_cache_item::modpack_name
char * modpack_name
Definition modpack.c:30
section_file
Definition section_file.h:36
section_file::name
char * name
Definition section_file.h:37
fc_strcasecmp
int fc_strcasecmp(const char *str0, const char *str1)
Definition support.c:186
TRUE
#define TRUE
Definition support.h:46
FALSE
#define FALSE
Definition support.h:47