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