2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure, Markus Armbruster
6 * Empire is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * See files README, COPYING and CREDITS in the root of the source
22 * tree for related information and legal notices. It is expected
23 * that future projects/authors will amend these files as needed.
27 * file.h: Describes Empire tables (`files' for historical reasons)
29 * Known contributors to this file:
30 * Markus Armbruster, 2005-2010
39 /* Members with immutable values */
40 int uid; /* Table ID */
41 char *name; /* Empire name (e.g., "treaty") */
42 char *file; /* file name, relative to gamedir for
43 game state, to builtindir for config */
44 struct castr *cadef; /* table column selectors (column meta-data) */
45 int size; /* size of a table entry */
46 int flags; /* only EFF_IMMUTABLE immutable, see below
47 for use of remaining bits */
49 /* Members whose values are fixed when the cache is mapped */
50 char *cache; /* pointer to cache */
51 int csize; /* cache size, in entries */
52 /* flags bits EFF_MEM, EFF_PRIVATE, EFF_NOTIME also fixed then */
54 /* Members whose values may vary throughout operation */
55 int baseid; /* id of first entry in cache */
56 int cids; /* # entries in cache */
57 int fids; /* # entries in table */
58 int fd; /* file descriptor, -1 if not open */
59 /* flags bit EFF_CUSTOM also varies */
61 /* User callbacks, may all be null */
63 * Called after element initialization. ELT is the element.
64 * May modify the element.
66 void (*oninit)(void *elt);
68 * Called after read. ID is the element id, and ELT is the
69 * element read. May modify the element. Modifications are
70 * visible to caller of ef_read(), but have no effect on the file.
72 void (*postread)(int id, void *elt);
74 * Called before write. ID is the element id, OLD is the element
75 * being updated (null unless it is cached) and ELT is the element
76 * being written. May modify the element. Modifications will be
77 * visible to caller of ef_write() and are written to the file.
79 void (*prewrite)(int id, void *old, void *elt);
81 * Called after table size changed, with file type as argument.
82 * Return -1 and set errno to make the operation fail.
84 int (*onresize)(int type);
90 unsigned generation: 12;
96 * Flag bits for struct empfile member flags
97 * Immutable flags are properties of the table and thus cannot change.
98 * The remaining flags record how the table is being used.
100 /* Immutable flags, fixed at compile-time */
101 /* Dereferencing entry address cast to struct emptypedstr * is safe */
102 #define EFF_TYPED bit(0)
104 * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner /
105 * group of such a table's entries can be safely obtained by
106 * dereferencing the entry's address cast to struct empobj *.
108 #define EFF_XY bit(1)
109 #define EFF_OWNER bit(2)
110 #define EFF_GROUP bit(3)
111 /* Table cache is allocated statically */
112 #define EFF_STATIC bit(4)
113 /* Table has a sentinel (all zero, not counted as elt), implies EFF_MEM */
114 #define EFF_SENTINEL bit(5)
115 /* All the immutable flags */
116 #define EFF_IMMUTABLE \
117 (EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
118 /* Flags set when table contents is mapped */
119 /* Table is entirely in memory */
120 #define EFF_MEM bit(8)
121 /* Table is privately mapped: changes don't affect the underlying file */
122 #define EFF_PRIVATE bit(9)
123 /* Table is customized (configuration tables only) */
124 #define EFF_CUSTOM bit(10)
125 /* Don't update timestamps */
126 #define EFF_NOTIME bit(11)
127 /* Transient flags, only occur in argument of ef_open() */
128 /* Create table file, clobbering any existing file */
129 #define EFF_CREATE bit(16)
132 * Empire `file types'
133 * These are really table IDs. Some tables are backed by files, some
134 * are compiled into the server, some initialized from configuration
140 /* Dynamic game data tables */
159 /* Static game data (configuration) */
160 /* Order is relevant; see read_builtin_tables() */
170 EF_UPDATES, /* not actually static */
173 EF_META, /* not really configuration */
193 EF_SECTOR_NAVIGATION,
198 /* Number of types: */
202 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
203 #define EF_IS_VIEW(type) (EF_COUNTRY <= (type) && (type) < EF_MAX)
205 extern struct castr *ef_cadef(int);
206 extern int ef_read(int, int, void *);
207 extern void ef_make_stale(void);
208 extern void ef_mark_fresh(int, void *);
209 extern void *ef_ptr(int, int);
210 extern char *ef_nameof(int);
211 extern time_t ef_mtime(int);
212 extern int ef_open(int, int, int);
213 extern int ef_open_view(int, int);
214 extern int ef_close(int);
215 extern int ef_flush(int);
216 extern void ef_blank(int, int, void *);
217 extern int ef_write(int, int, void *);
218 extern void ef_set_uid(int, void *, int);
219 extern int ef_extend(int, int);
220 extern int ef_ensure_space(int, int, int);
221 extern int ef_truncate(int, int);
222 extern int ef_nelem(int);
223 extern int ef_flags(int);
224 extern int ef_byname(char *);
225 extern int ef_byname_from(char *, int *);
226 extern int ef_verify(void);
227 extern int ef_elt_byname(int, char *);
229 extern struct empfile empfile[EF_MAX + 1];
230 extern void empfile_init(void);
231 extern void empfile_fixup(void);