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 char *target;
33};
34
35/* Get 'struct modpack_cache_list' and related functions: */
36#define SPECLIST_TAG modpack_cache
37#define SPECLIST_TYPE struct modpack_cache_item
38#include "speclist.h"
39
40#define modpack_cache_iterate(mplist, item) \
41 TYPED_LIST_ITERATE(struct modpack_cache_item, mplist, item)
42#define modpack_cache_iterate_end LIST_ITERATE_END
43
44 static struct modpack_cache_list *modpack_rulesets;
45 static struct modpack_cache_list *modpack_tilesets;
46
47/************************************************************************/
50 void modpacks_init(void)
51{
52 if (is_server()) {
53 modpack_rulesets = modpack_cache_list_new();
54 } else {
55 modpack_tilesets = modpack_cache_list_new();
56 }
57}
58
59/************************************************************************/
62 void modpacks_free(void)
63{
64 if (is_server()) {
65 modpack_cache_iterate(modpack_rulesets, item) {
66 free(item->modpack_name);
67 free(item->filename);
68 free(item);
69 } modpack_cache_iterate_end;
70
71 modpack_cache_list_destroy(modpack_rulesets);
72 } else {
73 modpack_cache_iterate(modpack_tilesets, item) {
74 free(item->modpack_name);
75 free(item->filename);
76 free(item->target);
77 free(item);
78 } modpack_cache_iterate_end;
79
80 modpack_cache_list_destroy(modpack_tilesets);
81 }
82}
83
84/************************************************************************/
92 bool modpack_check_capabilities(struct section_file *file, const char *us_capstr,
93 const char *filename, bool verbose)
94{
95 enum log_level level = verbose ? LOG_WARN : LOG_DEBUG;
96
97 const char *file_capstr = secfile_lookup_str(file, "datafile.options");
98
99 if (file_capstr == nullptr) {
100 log_base(level, "\"%s\": file doesn't have a capability string",
101 filename);
102 return FALSE;
103 }
104 if (!has_capabilities(us_capstr, file_capstr)) {
105 log_base(level, "\"%s\": file appears incompatible:",
106 filename);
107 log_base(level, " datafile options: %s", file_capstr);
108 log_base(level, " supported options: %s", us_capstr);
109 return FALSE;
110 }
111 if (!has_capabilities(file_capstr, us_capstr)) {
112 log_base(level, "\"%s\": file requires option(s) "
113 "that freeciv doesn't support:", filename);
114 log_base(level, " datafile options: %s", file_capstr);
115 log_base(level, " supported options: %s", us_capstr);
116 return FALSE;
117 }
118
119 return TRUE;
120}
121
122/************************************************************************/
127 struct fileinfo_list *get_modpacks_list(void)
128{
129 struct fileinfo_list *files;
130
131 /* Search for modpack files. */
132 files = fileinfolist_infix(get_data_dirs(), MODPACK_SUFFIX, TRUE);
133
134 return files;
135}
136
137/************************************************************************/
144 const char *modpack_has_ruleset(struct section_file *sf)
145{
146 if (sf != nullptr) {
147 if (!modpack_check_capabilities(sf, MODPACK_CAPSTR, sf->name,
148 FALSE)) {
149 return nullptr;
150 }
151
152 if (secfile_lookup_bool_default(sf, FALSE, "components.ruleset")) {
153 return secfile_lookup_str_default(sf, nullptr, "modpack.name");
154 }
155 }
156
157 return nullptr;
158}
159
160/************************************************************************/
167 const char *modpack_has_tileset(struct section_file *sf)
168{
169 if (sf != nullptr) {
170 if (!modpack_check_capabilities(sf, MODPACK_CAPSTR, sf->name,
171 FALSE)) {
172 return nullptr;
173 }
174
175 if (secfile_lookup_bool_default(sf, FALSE, "components.tileset")) {
176 return secfile_lookup_str_default(sf, nullptr, "modpack.name");
177 }
178 }
179
180 return nullptr;
181}
182
183/************************************************************************/
189 const char *modpack_serv_file(struct section_file *sf)
190{
191 if (sf != nullptr) {
192 return secfile_lookup_str_default(sf, nullptr, "ruleset.serv");
193 }
194
195 return nullptr;
196}
197
198/************************************************************************/
204 const char *modpack_rulesetdir(struct section_file *sf)
205{
206 if (sf != nullptr) {
207 return secfile_lookup_str_default(sf, nullptr, "ruleset.rulesetdir");
208 }
209
210 return nullptr;
211}
212
213/************************************************************************/
219 const char *modpack_tilespec(struct section_file *sf)
220{
221 if (sf != nullptr) {
222 return secfile_lookup_str_default(sf, nullptr, "tileset.tilespec");
223 }
224
225 return nullptr;
226}
227
228/************************************************************************/
235 const char *modpack_cache_ruleset(struct section_file *sf)
236{
237 const char *mp_name = modpack_has_ruleset(sf);
238 struct modpack_cache_item *item;
239
240 if (mp_name == nullptr) {
241 return nullptr;
242 }
243
244 fc_assert(sf->name != nullptr);
245
246 item = fc_malloc(sizeof(struct modpack_cache_item));
247 item->modpack_name = fc_strdup(mp_name);
248 item->filename = fc_strdup(sf->name);
249
250 modpack_cache_list_append(modpack_rulesets, item);
251
252 return mp_name;
253}
254
255/************************************************************************/
262 const char *modpack_cache_tileset(struct section_file *sf)
263{
264 const char *mp_name = modpack_has_tileset(sf);
265 struct modpack_cache_item *item;
266 const char *target;
267
268 if (mp_name == nullptr) {
269 return nullptr;
270 }
271
272 fc_assert(sf->name != nullptr);
273
274 item = fc_malloc(sizeof(struct modpack_cache_item));
275 item->modpack_name = fc_strdup(mp_name);
276 item->filename = fc_strdup(sf->name);
277 target = secfile_lookup_str_default(sf, nullptr, "tileset.tilespec");
278 if (target != nullptr) {
279 item->target = fc_strdup(target);
280 } else {
281 item->target = nullptr;
282 }
283
284 modpack_cache_list_append(modpack_tilesets, item);
285
286 return mp_name;
287}
288
289/************************************************************************/
295 const char *modpack_file_from_ruleset_cache(const char *name)
296{
297 modpack_cache_iterate(modpack_rulesets, item) {
298 if (!fc_strcasecmp(name, item->modpack_name)) {
299 return item->filename;
300 }
301 } modpack_cache_iterate_end;
302
303 return nullptr;
304}
305
306/************************************************************************/
312 const char *modpack_file_from_tileset_cache(const char *name)
313{
314 modpack_cache_iterate(modpack_tilesets, item) {
315 if (!fc_strcasecmp(name, item->modpack_name)) {
316 return item->filename;
317 }
318 } modpack_cache_iterate_end;
319
320 return nullptr;
321}
322
323/************************************************************************/
329 const char *modpack_tileset_target(const char *name)
330{
331 modpack_cache_iterate(modpack_tilesets, item) {
332 if (!fc_strcasecmp(name, item->modpack_name)) {
333 return item->target;
334 }
335 } modpack_cache_iterate_end;
336
337 return nullptr;
338}
339
340/************************************************************************/
346 void modpack_ruleset_cache_iterate(mrc_cb cb, void *data)
347{
348 modpack_cache_iterate(modpack_rulesets, item) {
349 cb(item->modpack_name, item->filename, data);
350 } modpack_cache_iterate_end;
351}
352
353
354/************************************************************************/
360 void modpack_tileset_cache_iterate(mrc_cb cb, void *data)
361{
362 modpack_cache_iterate(modpack_tilesets, item) {
363 cb(item->modpack_name, item->filename, data);
364 } modpack_cache_iterate_end;
365}
has_capabilities
bool has_capabilities(const char *us, const char *them)
Definition capability.c:88
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:44
modpack_file_from_tileset_cache
const char * modpack_file_from_tileset_cache(const char *name)
Definition modpack.c:312
modpacks_free
void modpacks_free(void)
Definition modpack.c:62
modpack_has_ruleset
const char * modpack_has_ruleset(struct section_file *sf)
Definition modpack.c:144
modpack_tileset_target
const char * modpack_tileset_target(const char *name)
Definition modpack.c:329
modpack_tileset_cache_iterate
void modpack_tileset_cache_iterate(mrc_cb cb, void *data)
Definition modpack.c:360
modpack_ruleset_cache_iterate
void modpack_ruleset_cache_iterate(mrc_cb cb, void *data)
Definition modpack.c:346
modpack_check_capabilities
bool modpack_check_capabilities(struct section_file *file, const char *us_capstr, const char *filename, bool verbose)
Definition modpack.c:92
modpack_has_tileset
const char * modpack_has_tileset(struct section_file *sf)
Definition modpack.c:167
modpack_tilesets
static struct modpack_cache_list * modpack_tilesets
Definition modpack.c:45
modpack_file_from_ruleset_cache
const char * modpack_file_from_ruleset_cache(const char *name)
Definition modpack.c:295
modpack_cache_iterate_end
#define modpack_cache_iterate_end
Definition modpack.c:42
modpack_serv_file
const char * modpack_serv_file(struct section_file *sf)
Definition modpack.c:189
modpacks_init
void modpacks_init(void)
Definition modpack.c:50
modpack_rulesetdir
const char * modpack_rulesetdir(struct section_file *sf)
Definition modpack.c:204
get_modpacks_list
struct fileinfo_list * get_modpacks_list(void)
Definition modpack.c:127
modpack_cache_ruleset
const char * modpack_cache_ruleset(struct section_file *sf)
Definition modpack.c:235
modpack_cache_iterate
#define modpack_cache_iterate(mplist, item)
Definition modpack.c:40
modpack_cache_tileset
const char * modpack_cache_tileset(struct section_file *sf)
Definition modpack.c:262
modpack_tilespec
const char * modpack_tilespec(struct section_file *sf)
Definition modpack.c:219
modpack.h
mrc_cb
void(* mrc_cb)(const char *, const char *, void *data)
Definition modpack.h:45
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:2180
secfile_lookup_bool_default
bool secfile_lookup_bool_default(const struct section_file *secfile, bool def, const char *path,...)
Definition registry_ini.c:1902
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:2210
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:886
fileinfolist_infix
struct fileinfo_list * fileinfolist_infix(const struct strvec *dirs, const char *infix, bool nodups)
Definition shared.c:1204
shared.h
speclist.h
item
Definition climisc.h:80
modpack_cache_item
Definition modpack.c:29
modpack_cache_item::target
char * target
Definition modpack.c:32
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