Crossfire Server, Trunk
account_char.c
Go to the documentation of this file.
1 /*
2  * Crossfire -- cooperative multi-player graphical RPG and adventure game
3  *
4  * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5  * Copyright (c) 1992 Frank Tore Johansen
6  *
7  * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8  * welcome to redistribute it under certain conditions. For details, please
9  * see COPYING and LICENSE.
10  *
11  * The authors can be reached via e-mail at <crossfire@metalforge.org>.
12  */
13 
46 #include "global.h"
47 
48 #include <assert.h>
49 #include <errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "account_char.h"
54 #include "object.h"
55 #include "output_file.h"
56 #include "sproto.h"
57 #include "player.h"
58 
60 #define NUM_ACCOUNT_CHAR_FIELDS 8
61 
69 #define ACCOUNT_DIR "account"
70 
71 static Account_Chars **chars_loaded = NULL;
72 static size_t chars_loaded_count = 0;
73 static size_t chars_loaded_allocated = 0;
80  char fname[MAX_BUF], buf[VERY_BIG_BUF];
81  FILE *fp;
82  Account_Char *ac, *last = NULL;
83 
84  snprintf(fname, MAX_BUF, "%s/%s/%s", settings.localdir, ACCOUNT_DIR, chars->account_name);
85  fp = fopen(fname, "r");
86  if (!fp) {
87  /* This may not in fact be a critical error - for a new account, there
88  * may not be any data associated with it.
89  */
90  LOG(llevInfo, "Warning: Unable to open %s: %s\n", fname, strerror(errno));
91  return;
92  }
93  while (fgets(buf, VERY_BIG_BUF, fp)) {
94  char *tmp[NUM_ACCOUNT_CHAR_FIELDS], *cp;
95 
96  /* Ignore any comment lines */
97  if (buf[0] == '#') continue;
98 
99  /* remove newline */
100  cp = strchr(buf, '\n');
101  if (cp) *cp = '\0';
102 
104  if (!tmp[7]) {
105  LOG(llevInfo, "Outdated entry in %s: %s\n", fname, buf);
106  tmp[7] = (char *) add_string("0");
107  } else {
108  LOG(llevError, "Corrupt entry in %s: %s\n", fname, buf);
109  continue;
110  }
111  }
112  ac = malloc(sizeof (Account_Char));
113  ac->name = add_string(tmp[0]);
114  ac->character_class = add_string(tmp[1]);
115  ac->race = add_string(tmp[2]);
116  ac->level = strtoul(tmp[3], (char**) NULL, 10);
117  ac->face = add_string(tmp[4]);
118  ac->party = add_string(tmp[5]);
119  ac->map = add_string(tmp[6]);
120  ac->isDead = strtoul(tmp[7], (char**) NULL, 10);
121 
122  ac->next = NULL;
123 
124  /* We tack on to the end of the list - in this way,
125  * the order of the file remains the same.
126  */
127  if (last)
128  last->next = ac;
129  else
130  chars->chars = ac;
131  last = ac;
132 
133  }
134  fclose(fp);
135 }
136 
147 Account_Chars *account_char_load(const char *account_name) {
148  for (size_t loaded = 0; loaded < chars_loaded_count; loaded++) {
149  if (strcmp(chars_loaded[loaded]->account_name, account_name) == 0) {
150  chars_loaded[loaded]->ref_count++;
151  return chars_loaded[loaded];
152  }
153  }
154 
155  Account_Chars *ac = calloc(1, sizeof(*ac));
156  ac->ref_count++;
157  ac->account_name = add_string(account_name);
159 
163  if (!chars_loaded) {
164  LOG(llevError, "Out of memory allocating %d account chars\n", chars_loaded_allocated);
166  }
167  }
170 
171  return ac;
172 }
173 
180  char fname[MAX_BUF];
181  FILE *fp;
182  OutputFile of;
183  Account_Char *ac;
184 
185  snprintf(fname, MAX_BUF, "%s/%s/%s", settings.localdir, ACCOUNT_DIR, chars->account_name);
186 
187  /* It is certanly possibly that all characters for an account have
188  * been removed/deleted - in that case, we just want to remove this
189  * file.
190  */
191  if (chars->chars == NULL) {
192  unlink(fname);
193  return;
194  }
195 
196  fp = of_open(&of, fname);
197  if (fp == NULL)
198  return;
199 
200  fprintf(fp, "# IMPORTANT: Do not edit this file while the server is running. This file is\n"
201  "# only read when the server starts, and any changes will be overwritten when\n"
202  "# the server exits.\n");
203  for (ac = chars->chars; ac; ac = ac->next) {
204  fprintf(fp, "%s:%s:%s:%d:%s:%s:%s:%d\n",
205  ac->name, ac->character_class, ac->race, ac->level,
206  ac->face, ac->party, ac->map, ac->isDead);
207  }
208  of_close(&of);
209 }
210 
226 
227  Account_Char *ap, *last = NULL;
228 
229  for (ap = chars->chars; ap; ap = ap->next) {
230  if (!strcmp(ap->name, pl->ob->name)) break;
231  last = ap;
232  }
233  /* If ap is not NULL, it means we found a match.
234  * Rather than checking to see if values have changed, just
235  * update them.
236  */
237  if (ap) {
238  /* We know the name can not be changing, as otherwise
239  * we wouldn't have gotten a match. So no need to
240  * update that.
241  */
242 #if 0
243  /* As of right now, the class of the character is not stored
244  * anyplace, so we don't know what it is. Keep this code here
245  * until it can be determined.
246  */
248  ap->character_class = add_string();
249 #else
250  ap->character_class = add_string("");
251 #endif
252 
253  free_string(ap->race);
254  /* This looks pretty nasty. Basically, the player object is
255  * the race archetype, but its name has been changed to the player
256  * name. So we have to go back to the actual original archetype,
257  * the clone object in there, to get the name.
258  */
259  ap->race = add_string(pl->ob->arch->clone.name);
260 
261  ap->level = pl->ob->level;
262 
263  /* We should try and get the best face (front view) of
264  * the character - right now, we just get whatever view the character
265  * happens to be facing. Unfortunately, the animation code is such
266  * that it isn't a simple matter as most of that logic is not
267  * conveniently exposed.
268  */
269  free_string(ap->face);
270  ap->face = add_string(pl->ob->face->name);
271 
272  free_string(ap->party);
273  if (pl->party)
274  ap->party = add_string(pl->party->partyname);
275  else
276  ap->party = add_string("");
277 
278  free_string(ap->map);
279 
280  /* If there is a real name set for the map, use that instead
281  * of the pathname, which is what maplevel holds. This is
282  * more friendly (eg, Scorn Inn vs /scorn/inns/....)
283  */
284  if (pl->ob->map && pl->ob->map->name) {
285  ap->map = add_string(pl->ob->map->name);
286  } else {
287  /* Use the stored value - this may not be as up to date, but is
288  * probably more reliable, as depending when this charapter is added,
289  * it may not really be on any map.
290  */
291  ap->map = add_string(pl->maplevel);
292  }
293  } else {
294  /* In this case, we are adding a new entry */
295  ap = malloc(sizeof (Account_Char));
296  ap->name = add_string(pl->ob->name);
297  ap->character_class = add_string("");
298  ap->race = add_string(pl->ob->arch->clone.name);
299  ap->level = pl->ob->level;
300  ap->face = add_string(pl->ob->face->name);
301  if (pl->party)
302  ap->party = add_string(pl->party->partyname);
303  else
304  ap->party = add_string("");
305  ap->map = add_string(pl->maplevel);
306  /* The character cannot be dead already */
307  ap->isDead = 0;
308 
309  ap->next = NULL;
310  if (last)
311  last->next = ap;
312  else
313  chars->chars = ap;
314  }
315 }
316 
328 void account_char_remove(Account_Chars *chars, const char *pl_name) {
329  Account_Char *ap, *last = NULL;
330 
331  for (ap = chars->chars; ap; ap = ap->next) {
332  if (!strcmp(ap->name, pl_name)) break;
333  last = ap;
334  }
335  /* If we didn't find this character, nothing to do */
336  if (!ap) return;
337 
338  /* As per previous notes, these should never be NULL */
339  free_string(ap->name);
341  free_string(ap->race);
342  free_string(ap->face);
343  free_string(ap->party);
344  free_string(ap->map);
345 
346  /* remove this link, or update head of list as appropriate */
347  if (last) {
348  last->next = ap->next;
349  } else {
350  chars->chars = ap->next;
351  }
352  free(ap);
353 }
354 
363  if (chars->ref_count > 1) {
364  chars->ref_count--;
365  return;
366  }
367 
368  for (size_t p = 0; p < chars_loaded_count; p++) {
369  if (chars_loaded[p] == chars) {
370  if (p < chars_loaded_count - 1) {
372  }
374  break;
375  }
376  }
377 
378  Account_Char *ap, *next;
379 
380  for (ap = chars->chars; ap; ap = next) {
381  next = ap->next;
382 
383  free_string(ap->name);
385  free_string(ap->race);
386  free_string(ap->face);
387  free_string(ap->party);
388  free_string(ap->map);
389  free(ap);
390  }
391  free_string(chars->account_name);
392  free(chars);
393 }
394 
404 int make_perma_dead(object *op) {
405  player *pl = op->contr;
406  Account_Chars *chars;
407  Account_Char *ac;
408 
409  if (!pl) {
410  return 1;
411  }
412  /* Is this necessary? I'm not sure. It was in the code I found to use as an example */
413  pl = get_player(pl);
414  /* Make sure there is an account name to do things to */
415  if (!pl->socket.account_name) {
416  return 1;
417  }
418 
419  /* Load the appropriate account for the action. */
421 
422  /* Find the right character. */
423  for (ac = chars->chars; ac; ac = ac->next) {
424  if (strcmp(ac->name, op->name) == 0)
425  break;
426  }
427 
428  /* This character is dead */
429  ac->isDead = 1;
430  account_char_save(chars);
431  account_char_free(chars);
432  return 0;
433 }
434 
446 int unmake_perma_dead(char *account, char *player) {
447  Account_Chars *chars;
448  Account_Char *ac;
449 
450  /*
451  * If no account name, then there is nothing to do here.
452  * The character was dead before the account was kept track of.
453  */
454  if (!account) {
455  return 1;
456  }
457 
458  chars = account_char_load(account);
459 
460  /* Find the right character. */
461  for (ac = chars->chars; ac; ac = ac->next) {
462  if (strcmp(ac->name, player) == 0)
463  break;
464  }
465 
466  /* This character is alive */
467  ac->isDead = 0;
468  account_char_save(chars);
469  account_char_free(chars);
470  return 0;
471 }
Face::name
sstring name
Definition: face.h:19
give.next
def next
Definition: give.py:44
output_file.h
global.h
add_string
sstring add_string(const char *str)
Definition: shstr.c:124
account_char_struct::isDead
uint8_t isDead
Definition: account_char.h:20
obj::face
const Face * face
Definition: object.h:336
llevError
@ llevError
Definition: logger.h:11
account_char_load
Account_Chars * account_char_load(const char *account_name)
Definition: account_char.c:147
obj::map
struct mapdef * map
Definition: object.h:300
account_char_struct::level
uint8_t level
Definition: account_char.h:16
account_char_struct::map
sstring map
Definition: account_char.h:19
pl::socket
socket_struct socket
Definition: player.h:107
pl
Definition: player.h:105
pl::ob
object * ob
Definition: player.h:176
get_player
player * get_player(player *p)
Definition: player.c:282
account_char_struct::name
sstring name
Definition: account_char.h:13
Ice.tmp
int tmp
Definition: Ice.py:207
ACCOUNT_DIR
#define ACCOUNT_DIR
Definition: account_char.c:69
curse_on_apply.ac
ac
Definition: curse_on_apply.py:4
account_chars_struct
Definition: account_char.h:28
pl::maplevel
char maplevel[MAX_BUF]
Definition: player.h:109
account_char_struct
Definition: account_char.h:12
party_struct::partyname
char * partyname
Definition: party.h:14
settings
struct Settings settings
Definition: init.c:39
free_string
void free_string(sstring str)
Definition: shstr.c:280
chars_loaded
static Account_Chars ** chars_loaded
Definition: account_char.c:71
account_char_save
void account_char_save(Account_Chars *chars)
Definition: account_char.c:179
obj::name
sstring name
Definition: object.h:314
of_close
int of_close(OutputFile *of)
Definition: output_file.c:61
fatal
void fatal(enum fatal_error err)
Definition: utils.c:580
chars_loaded_allocated
static size_t chars_loaded_allocated
Definition: account_char.c:73
socket_struct::account_name
char * account_name
Definition: newserver.h:126
NUM_ACCOUNT_CHAR_FIELDS
#define NUM_ACCOUNT_CHAR_FIELDS
Definition: account_char.c:60
account_char_struct::race
sstring race
Definition: account_char.h:15
unmake_perma_dead
int unmake_perma_dead(char *account, char *player)
Definition: account_char.c:446
account_char_struct::next
struct account_char_struct * next
Definition: account_char.h:21
sproto.h
mapdef::name
char * name
Definition: map.h:320
MAX_BUF
#define MAX_BUF
Definition: define.h:35
split_string
size_t split_string(char *str, char *array[], size_t array_size, char sep)
Definition: utils.c:483
account_char_add
void account_char_add(Account_Chars *chars, player *pl)
Definition: account_char.c:225
obj::arch
struct archt * arch
Definition: object.h:417
llevInfo
@ llevInfo
Definition: logger.h:12
archt::clone
object clone
Definition: object.h:473
LOG
void LOG(LogLevel logLevel, const char *format,...)
Definition: logger.c:51
player.h
of_open
FILE * of_open(OutputFile *of, const char *fname)
Definition: output_file.c:30
account_char_free
void account_char_free(Account_Chars *chars)
Definition: account_char.c:362
give.op
op
Definition: give.py:33
account_char_struct::party
sstring party
Definition: account_char.h:18
account_char_struct::character_class
sstring character_class
Definition: account_char.h:14
account_char_load_from_file
static void account_char_load_from_file(Account_Chars *chars)
Definition: account_char.c:79
account_chars_struct::chars
Account_Char * chars
Definition: account_char.h:31
make_perma_dead
int make_perma_dead(object *op)
Definition: account_char.c:404
buf
StringBuffer * buf
Definition: readable.c:1610
account_char_struct::face
sstring face
Definition: account_char.h:17
account_char.h
account_char_remove
void account_char_remove(Account_Chars *chars, const char *pl_name)
Definition: account_char.c:328
account_chars_struct::account_name
sstring account_name
Definition: account_char.h:29
VERY_BIG_BUF
#define VERY_BIG_BUF
Definition: define.h:36
chars_loaded_count
static size_t chars_loaded_count
Definition: account_char.c:72
pl::party
partylist * party
Definition: player.h:202
account_chars_struct::ref_count
uint8_t ref_count
Definition: account_char.h:30
OUT_OF_MEMORY
@ OUT_OF_MEMORY
Definition: define.h:48
object.h
obj::level
int16_t level
Definition: object.h:356
OutputFile
Definition: output_file.h:41
Settings::localdir
const char * localdir
Definition: global.h:244