]> git.pond.sub.org Git - empserver/blob - include/file.h
Split ef_verify() into ef_verify_config(), ef_verify_state()
[empserver] / include / file.h
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  *
19  *  ---
20  *
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.
24  *
25  *  ---
26  *
27  *  file.h: Describes Empire tables (`files' for historical reasons)
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2005-2010
31  */
32
33 #ifndef FILE_H
34 #define FILE_H
35
36 #include <time.h>
37
38 struct empfile {
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 base;                   /* view's base table, else EF_BAD */
46     int size;                   /* size of a table entry */
47     int nent;                   /* #table entries, -1 if variable */
48     int flags;                  /* only EFF_IMMUTABLE immutable, see below
49                                    for use of remaining bits */
50
51     /* Members whose values are fixed when the cache is mapped */
52     char *cache;                /* pointer to cache */
53     int csize;                  /* cache size, in entries */
54     /* flags bits EFF_MEM, EFF_PRIVATE, EFF_NOTIME also fixed then */
55
56     /* Members whose values may vary throughout operation */
57     int baseid;                 /* id of first entry in cache */
58     int cids;                   /* # entries in cache */
59     int fids;                   /* # entries in table */
60     int fd;                     /* file descriptor, -1 if not open */
61     /* flags bit EFF_CUSTOM also varies */
62
63     /* User callbacks, may all be null */
64     /*
65      * Called after element initialization.  ELT is the element.
66      * May modify the element.
67      */
68     void (*oninit)(void *elt);
69     /*
70      * Called after read.  ID is the element id, and ELT is the
71      * element read.  May modify the element.  Modifications are
72      * visible to caller of ef_read(), but have no effect on the file.
73      */
74     void (*postread)(int id, void *elt);
75     /*
76      * Called before write.  ID is the element id, OLD is the element
77      * being updated (null unless it is cached) and ELT is the element
78      * being written.  May modify the element.  Modifications will be
79      * visible to caller of ef_write() and are written to the file.
80      */
81     void (*prewrite)(int id, void *old, void *elt);
82     /*
83      * Called after table size changed, with file type as argument.
84      */
85     void (*onresize)(int type);
86 };
87
88 struct emptypedstr {
89     signed ef_type: 8;
90     unsigned seqno: 12;
91     unsigned generation: 12;
92     int uid;
93     time_t timestamp;
94 };
95
96 /*
97  * Flag bits for struct empfile member flags
98  * Immutable flags are properties of the table and thus cannot change.
99  * The remaining flags record how the table is being used.
100  */
101 /* Immutable flags, fixed at compile-time */
102 /* Dereferencing entry address cast to struct emptypedstr * is safe */
103 #define EFF_TYPED       bit(0)
104 /*
105  * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner /
106  * group of such a table's entries can be safely obtained by
107  * dereferencing the entry's address cast to struct empobj *.
108  */
109 #define EFF_XY          bit(1)
110 #define EFF_OWNER       bit(2)
111 #define EFF_GROUP       bit(3)
112 /* Table cache is allocated statically */
113 #define EFF_STATIC      bit(4)
114 /* Table has a sentinel (all zero, not counted as elt), implies EFF_MEM */
115 #define EFF_SENTINEL    bit(5)
116 /* All the immutable flags */
117 #define EFF_IMMUTABLE \
118     (EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC | EFF_SENTINEL)
119 /* Flags set when table contents is mapped */
120 /* Table is entirely in memory */
121 #define EFF_MEM         bit(8)
122 /* Table is privately mapped: changes don't affect the underlying file */
123 #define EFF_PRIVATE     bit(9)
124 /* Table is customized (configuration tables only) */
125 #define EFF_CUSTOM      bit(10)
126 /* Don't update timestamps */
127 #define EFF_NOTIME      bit(11)
128 /* Transient flags, only occur in argument of ef_open() */
129 /* Create table file, clobbering any existing file */
130 #define EFF_CREATE      bit(16)
131
132 /*
133  * Empire `file types'
134  * These are really table IDs.  Some tables are backed by files, some
135  * are compiled into the server, some initialized from configuration
136  * files.
137  */
138 enum {
139     /* Error value */
140     EF_BAD = -1,
141     /* Dynamic game data tables */
142     EF_SECTOR,
143     EF_SHIP,
144     EF_PLANE,
145     EF_LAND,
146     EF_NUKE,
147     EF_NEWS,
148     EF_TREATY,
149     EF_TRADE,
150     EF_POWER,
151     EF_NATION,
152     EF_LOAN,
153     EF_MAP,
154     EF_BMAP,
155     EF_COMM,
156     EF_LOST,
157     EF_REALM,
158     EF_GAME,
159     EF_DYNMAX = EF_GAME,
160     /* Static game data (configuration) */
161     /* Order is relevant; see read_builtin_tables() */
162     EF_ITEM,
163     EF_PRODUCT,
164     EF_SECTOR_CHR,
165     EF_SHIP_CHR,
166     EF_PLANE_CHR,
167     EF_LAND_CHR,
168     EF_NUKE_CHR,
169     EF_NEWS_CHR,
170     EF_INFRASTRUCTURE,
171     EF_UPDATES,                 /* not actually static */
172     EF_TABLE,
173     EF_VERSION,
174     EF_META,                    /* not really configuration */
175     /* Symbol tables */
176     EF_AGREEMENT_STATUS,
177     EF_LAND_CHR_FLAGS,
178     EF_LEVEL,
179     EF_META_FLAGS,
180     EF_META_TYPE,
181     EF_MISSIONS,
182     EF_NATION_FLAGS,
183     EF_NATION_REJECTS,
184     EF_NATION_RELATIONS,
185     EF_NATION_STATUS,
186     EF_NUKE_CHR_FLAGS,
187     EF_PACKING,
188     EF_PAGE_HEADINGS,
189     EF_PLAGUE_STAGES,
190     EF_PLANE_CHR_FLAGS,
191     EF_PLANE_FLAGS,
192     EF_RESOURCES,
193     EF_RETREAT_FLAGS,
194     EF_SECTOR_NAVIGATION,
195     EF_SHIP_CHR_FLAGS,
196     EF_TREATY_FLAGS,
197     /* Views */
198     EF_COUNTRY,
199     /* Number of types: */
200     EF_MAX
201 };
202
203 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
204 #define EF_IS_VIEW(type) (empfile[(type)].base != EF_BAD)
205
206 extern struct castr *ef_cadef(int);
207 extern int ef_read(int, int, void *);
208 extern void ef_make_stale(void);
209 extern void ef_mark_fresh(int, void *);
210 extern void *ef_ptr(int, int);
211 extern char *ef_nameof(int);
212 extern time_t ef_mtime(int);
213 extern int ef_open(int, int);
214 extern int ef_open_view(int);
215 extern int ef_close(int);
216 extern int ef_flush(int);
217 extern void ef_blank(int, int, void *);
218 extern int ef_write(int, int, void *);
219 extern void ef_set_uid(int, void *, int);
220 extern int ef_extend(int, int);
221 extern int ef_ensure_space(int, int, int);
222 extern int ef_id_limit(int);
223 extern int ef_truncate(int, int);
224 extern int ef_nelem(int);
225 extern int ef_flags(int);
226 extern int ef_byname(char *);
227 extern int ef_byname_from(char *, int *);
228 extern int ef_verify_config(void);
229 extern int ef_verify_state(int);
230 extern int ef_elt_byname(int, char *);
231
232 extern struct empfile empfile[EF_MAX + 1];
233 extern void empfile_init(void);
234 extern void empfile_fixup(void);
235
236 #endif