]> git.pond.sub.org Git - empserver/blob - include/file.h
New struct empfile callback onresize
[empserver] / include / file.h
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program 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 2 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, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  file.h: Describes Empire tables (`files' for historical reasons)
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2005-2008
32  */
33
34 #ifndef FILE_H
35 #define FILE_H
36
37 #include <time.h>
38
39 struct empfile {
40     /* Members with immutable values */
41     int uid;                    /* Table ID */
42     char *name;                 /* Empire name (e.g., "treaty") */
43     char *file;                 /* file name, relative to gamedir for
44                                    game state, to builtindir for config */
45     struct castr *cadef;        /* table column selectors (column meta-data) */
46     int size;                   /* size of a table entry */
47     int flags;                  /* only EFF_IMMUTABLE immutable, see below
48                                    for use of remaining bits */
49
50     /* Members whose values are fixed when the cache is mapped */
51     char *cache;                /* pointer to cache */
52     int csize;                  /* cache size, in entries */
53     /* flags bits EFF_MEM, EFF_PRIVATE, EFF_NOTIME also fixed then */
54
55     /* Members whose values may vary throughout operation */
56     int baseid;                 /* id of first entry in cache */
57     int cids;                   /* # entries in cache */
58     int fids;                   /* # entries in table */
59     int fd;                     /* file descriptor, -1 if not open */
60     /* flags bit EFF_CUSTOM also varies */
61
62     /* User callbacks, may all be null */
63     /*
64      * Called after element initialization.  ELT is the element.
65      */
66     void (*oninit)(void *elt);
67     /*
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.
71      */
72     void (*postread)(int id, void *elt);
73     /*
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.
78      */
79     void (*prewrite)(int id, void *old, void *elt);
80     /*
81      * Called after table size changed, with file type as argument.
82      * Return -1 and set errno to make the operation fail.
83      */
84     int (*onresize)(int type);
85 };
86
87 struct emptypedstr {
88     short ef_type;
89     short uid;
90     unsigned seqno;
91     time_t timestamp;
92 };
93
94 /*
95  * Flag bits for struct empfile member flags
96  * Immutable flags are properties of the table and thus cannot change.
97  * The remaining flags record how the table is being used.
98  */
99 /* Immutable flags, fixed at compile-time */
100 /* Dereferencing entry address cast to struct emptypedstr * is safe */
101 #define EFF_TYPED       bit(0)
102 /*
103  * EFF_XY / EFF_OWNER / EFF_GROUP assert that coordinates / owner /
104  * group of such a table's entries can be safely obtained by
105  * dereferencing the entry's address cast to struct empobj *.
106  */
107 #define EFF_XY          bit(1)
108 #define EFF_OWNER       bit(2)
109 #define EFF_GROUP       bit(3)
110 /* Table is allocated statically */
111 #define EFF_STATIC      bit(4)
112 /* Table has a sentinel (all zero, not counted as elt), implies EFF_MEM */
113 #define EFF_SENTINEL    bit(5)
114 /* All the immutable flags */
115 #define EFF_IMMUTABLE \
116     (EFF_TYPED | EFF_XY | EFF_OWNER | EFF_GROUP | EFF_STATIC)
117 /* Flags set when table contents is mapped */
118 /* Table is entirely in memory */
119 #define EFF_MEM         bit(8)
120 /* Table is privately mapped: changes don't affect the underlying file */
121 #define EFF_PRIVATE     bit(9)
122 /* Table is customized (configuration tables only) */
123 #define EFF_CUSTOM      bit(10)
124 /* Don't update timestamps */
125 #define EFF_NOTIME      bit(11)
126 /* Transient flags, only occur in argument of ef_open() */
127 /* Create table file, clobbering any existing file */
128 #define EFF_CREATE      bit(16)
129
130 /*
131  * Empire `file types'
132  * These are really table IDs.  Some tables are backed by files, some
133  * are compiled into the server, some initialized from configuration
134  * files.
135  */
136 enum {
137     /* Error value */
138     EF_BAD = -1,
139     /* Dynamic game data tables */
140     EF_SECTOR,
141     EF_SHIP,
142     EF_PLANE,
143     EF_LAND,
144     EF_NUKE,
145     EF_NEWS,
146     EF_TREATY,
147     EF_TRADE,
148     EF_POWER,
149     EF_NATION,
150     EF_LOAN,
151     EF_MAP,
152     EF_BMAP,
153     EF_COMM,
154     EF_LOST,
155     EF_REALM,
156     EF_GAME,
157     EF_DYNMAX = EF_GAME,
158     /* Static game data (configuration) */
159     /* Order is relevant; see read_builtin_tables() */
160     EF_ITEM,
161     EF_PRODUCT,
162     EF_SECTOR_CHR,
163     EF_SHIP_CHR,
164     EF_PLANE_CHR,
165     EF_LAND_CHR,
166     EF_NUKE_CHR,
167     EF_NEWS_CHR,
168     EF_INFRASTRUCTURE,
169     EF_UPDATES,                 /* not actually static */
170     EF_TABLE,
171     EF_VERSION,
172     EF_META,                    /* not really configuration */
173     /* Symbol tables */
174     EF_AGREEMENT_STATUS,
175     EF_LAND_CHR_FLAGS,
176     EF_LEVEL,
177     EF_META_FLAGS,
178     EF_META_TYPE,
179     EF_MISSIONS,
180     EF_NATION_FLAGS,
181     EF_NATION_REJECTS,
182     EF_NATION_RELATIONS,
183     EF_NATION_STATUS,
184     EF_NUKE_CHR_FLAGS,
185     EF_PACKING,
186     EF_PAGE_HEADINGS,
187     EF_PLAGUE_STAGES,
188     EF_PLANE_CHR_FLAGS,
189     EF_PLANE_FLAGS,
190     EF_RESOURCES,
191     EF_RETREAT_FLAGS,
192     EF_SECTOR_NAVIGATION,
193     EF_SHIP_CHR_FLAGS,
194     EF_TREATY_FLAGS,
195     /* Views */
196     EF_COUNTRY,
197     /* Number of types: */
198     EF_MAX
199 };
200
201 #define EF_IS_GAME_STATE(type) (EF_SECTOR <= (type) && (type) <= EF_DYNMAX)
202 #define EF_IS_VIEW(type) (EF_COUNTRY <= (type) && (type) < EF_MAX)
203
204 extern struct castr *ef_cadef(int);
205 extern int ef_read(int, int, void *);
206 extern void *ef_ptr(int, int);
207 extern char *ef_nameof(int);
208 extern time_t ef_mtime(int);
209 extern int ef_open(int, int, int);
210 extern int ef_open_view(int, int);
211 extern int ef_close(int);
212 extern int ef_flush(int);
213 extern void ef_blank(int, int, void *);
214 extern int ef_write(int, int, void *);
215 extern void ef_set_uid(int, void *, int);
216 extern int ef_extend(int, int);
217 extern int ef_ensure_space(int, int, int);
218 extern int ef_truncate(int, int);
219 extern int ef_nelem(int);
220 extern int ef_flags(int);
221 extern int ef_byname(char *);
222 extern int ef_byname_from(char *, int *);
223 extern int ef_verify(void);
224 extern int ef_elt_byname(int, char *);
225
226 extern struct empfile empfile[EF_MAX + 1];
227 extern void empfile_init(void);
228 extern void empfile_fixup(void);
229
230 #endif