File: attribute.c

package info (click to toggle)
freeciv 2.6.2-1~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 212,500 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (547 lines) | stat: -rw-r--r-- 18,529 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/********************************************************************** 
 Freeciv - Copyright (C) 2001 - R. Falke
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

/* utility */
#include "dataio.h"
#include "fcintl.h"
#include "genhash.h"    /* genhash_val_t */
#include "log.h"
#include "mem.h"

/* common */
#include "packets.h"

/* client */
#include "client_main.h"

#include "attribute.h"

#define log_attribute           log_debug

enum attribute_serial {
  A_SERIAL_FAIL,
  A_SERIAL_OK,
  A_SERIAL_OLD,
};

struct attr_key {
  int key, id, x, y;
};

static genhash_val_t attr_key_val(const struct attr_key *pkey);
static bool attr_key_comp(const struct attr_key *pkey1,
                          const struct attr_key *pkey2);
static struct attr_key *attr_key_dup(const struct attr_key *pkey);
static void attr_key_destroy(struct attr_key *pkey);

/* 'struct attribute_hash' and related functions. */
#define SPECHASH_TAG attribute
#define SPECHASH_IKEY_TYPE struct attr_key *
#define SPECHASH_IDATA_TYPE void *
#define SPECHASH_IKEY_VAL attr_key_val
#define SPECHASH_IKEY_COMP attr_key_comp
#define SPECHASH_IKEY_COPY attr_key_dup
#define SPECHASH_IKEY_FREE attr_key_destroy
#define SPECHASH_IDATA_FREE free
#include "spechash.h"
#define attribute_hash_values_iterate(hash, pvalue)                         \
  TYPED_HASH_DATA_ITERATE(void *, hash, pvalue)
#define attribute_hash_values_iterate_end HASH_DATA_ITERATE_END
#define attribute_hash_iterate(hash, pkey, pvalue)                          \
  TYPED_HASH_ITERATE(const struct attr_key *, void *, hash, pkey, pvalue)
#define attribute_hash_iterate_end HASH_ITERATE_END


static struct attribute_hash *attribute_hash = NULL;

/****************************************************************************
  Hash function for attribute_hash.
****************************************************************************/
static genhash_val_t attr_key_val(const struct attr_key *pkey)
{
  return (genhash_val_t) pkey->id ^ pkey->x ^ pkey->y ^ pkey->key;
}

/****************************************************************************
  Compare-function for the keys in the hash table.
****************************************************************************/
static bool attr_key_comp(const struct attr_key *pkey1,
                          const struct attr_key *pkey2)
{
  return pkey1->key == pkey2->key
      && pkey1->id  == pkey2->id
      && pkey1->x   == pkey2->x
      && pkey1->y   == pkey2->y;
}

/****************************************************************************
  Duplicate an attribute key.
****************************************************************************/
static struct attr_key *attr_key_dup(const struct attr_key *pkey)
{
  struct attr_key *pnew_key = fc_malloc(sizeof(*pnew_key));

  *pnew_key = *pkey;
  return pnew_key;
}

/****************************************************************************
  Free an attribute key.
****************************************************************************/
static void attr_key_destroy(struct attr_key *pkey)
{
  fc_assert_ret(NULL != pkey);
  free(pkey);
}

/****************************************************************************
  Initializes the attribute module.
****************************************************************************/
void attribute_init(void)
{
  fc_assert(NULL == attribute_hash);
  attribute_hash = attribute_hash_new();
}

/****************************************************************************
  Frees the attribute module.
****************************************************************************/
void attribute_free(void)
{
  fc_assert_ret(NULL != attribute_hash);
  attribute_hash_destroy(attribute_hash);
  attribute_hash = NULL;
}

/****************************************************************************
  Serialize an attribute hash for network/storage.
****************************************************************************/
static enum attribute_serial
serialize_hash(const struct attribute_hash *hash,
               void **pdata, int *pdata_length)
{
  /*
   * Layout of version 2:
   *
   * struct {
   *   uint32 0;   always != 0 in version 1
   *   uint8 2;
   *   uint32 entries;
   *   uint32 total_size_in_bytes;
   * } preamble;
   * 
   * struct {
   *   uint32 value_size;
   *   char key[], char value[];
   * } body[entries];
   */
  const size_t entries = attribute_hash_size(hash);
  int total_length, value_lengths[entries];
  void *result;
  struct data_out dout;
  int i;

  /*
   * Step 1: loop through all keys and fill value_lengths and calculate
   * the total_length.
   */
  /* preamble */
  total_length = 4 + 1 + 4 + 4;
  /* body */
  total_length += entries * (4 + 4 + 4 + 2 + 2); /* value_size + key */
  i = 0;
  attribute_hash_values_iterate(hash, pvalue) {
    struct data_in din;

    dio_input_init(&din, pvalue, 4);
    dio_get_uint32(&din, &value_lengths[i]);

    total_length += value_lengths[i];
    i++;
  } attribute_hash_values_iterate_end;

  /*
   * Step 2: allocate memory.
   */
  result = fc_malloc(total_length);
  dio_output_init(&dout, result, total_length);

  /*
   * Step 3: fill out the preamble.
   */
  dio_put_uint32(&dout, 0);
  dio_put_uint8(&dout, 2);
  dio_put_uint32(&dout, attribute_hash_size(hash));
  dio_put_uint32(&dout, total_length);

  /*
   * Step 4: fill out the body.
   */
  i = 0;
  attribute_hash_iterate(hash, pkey, pvalue) {
    dio_put_uint32(&dout, value_lengths[i]);

    dio_put_uint32(&dout, pkey->key);
    dio_put_uint32(&dout, pkey->id);
    dio_put_sint16(&dout, pkey->x);
    dio_put_sint16(&dout, pkey->y);

    dio_put_memory(&dout, ADD_TO_POINTER(pvalue, 4), value_lengths[i]);
    i++;
  } attribute_hash_iterate_end;

  fc_assert(!dout.too_short);
  fc_assert_msg(dio_output_used(&dout) == total_length,
                "serialize_hash() total_length = %lu, actual = %lu",
                (long unsigned)total_length,
                (long unsigned)dio_output_used(&dout));

  /*
   * Step 5: return.
   */
  *pdata = result;
  *pdata_length = total_length;
  log_attribute("attribute.c serialize_hash() "
                "serialized %lu entries in %lu bytes",
                (long unsigned) entries, (long unsigned) total_length);
  return A_SERIAL_OK;
}

/****************************************************************************
  This data was serialized (above), sent as an opaque data packet to the 
  server, stored in a savegame, retrieved from the savegame, sent as an 
  opaque data packet back to the client, and now is ready to be restored.
  Check everything!
****************************************************************************/
static enum attribute_serial unserialize_hash(struct attribute_hash *hash,
                                              const void *data,
                                              size_t data_length)
{
  int entries, i, dummy;
  struct data_in din;

  attribute_hash_clear(hash);

  dio_input_init(&din, data, data_length);

  dio_get_uint32(&din, &dummy);
  if (dummy != 0) {
    log_verbose("attribute.c unserialize_hash() preamble, uint32 %lu != 0",
                (long unsigned) dummy);
    return A_SERIAL_OLD;
  }
  dio_get_uint8(&din, &dummy);
  if (dummy != 2) {
    log_verbose("attribute.c unserialize_hash() preamble, "
                "uint8 %lu != 2 version", (long unsigned) dummy);
    return A_SERIAL_OLD;
  }
  dio_get_uint32(&din, &entries);
  dio_get_uint32(&din, &dummy);
  if (dummy != data_length) {
    log_verbose("attribute.c unserialize_hash() preamble, "
                "uint32 %lu != %lu data_length",
                (long unsigned) dummy, (long unsigned) data_length);
    return A_SERIAL_FAIL;
  }

  log_attribute("attribute.c unserialize_hash() "
                "uint32 %lu entries, %lu data_length",
                (long unsigned) entries, (long unsigned) data_length);

  for (i = 0; i < entries; i++) {
    struct attr_key key;
    void *pvalue;
    int value_length;
    struct data_out dout;

    if (!dio_get_uint32(&din, &value_length)) {
      log_verbose("attribute.c unserialize_hash() "
                  "uint32 value_length dio_input_too_short");
      return A_SERIAL_FAIL;
    }
    log_attribute("attribute.c unserialize_hash() "
                  "uint32 %lu value_length", (long unsigned) value_length);

    /* next 12 bytes */
    if (!dio_get_uint32(&din, &key.key)
        || !dio_get_uint32(&din, &key.id)
        || !dio_get_sint16(&din, &key.x)
        || !dio_get_sint16(&din, &key.y)) {
      log_verbose("attribute.c unserialize_hash() "
                  "uint32 key dio_input_too_short");
      return A_SERIAL_FAIL;
    }
    pvalue = fc_malloc(value_length + 4);

    dio_output_init(&dout, pvalue, value_length + 4);
    dio_put_uint32(&dout, value_length);
    if (!dio_get_memory(&din, ADD_TO_POINTER(pvalue, 4), value_length)) {
      log_verbose("attribute.c unserialize_hash() "
                  "memory dio_input_too_short");
      return A_SERIAL_FAIL;
    }

    if (!attribute_hash_insert(hash, &key, pvalue)) {
      /* There are some untraceable attribute bugs caused by the CMA that
       * can cause this to happen. I think the only safe thing to do is
       * to delete all attributes. Another symptom of the bug is the
       * value_length (above) is set to a random value, which can also
       * cause a bug. */
      free(pvalue);
      attribute_hash_clear(hash);
      return A_SERIAL_FAIL;
    }
  }

  if (dio_input_remaining(&din) > 0) {
    /* This is not an error, as old clients sent overlong serialized
     * attributes pre gna bug #21295, and these will be hanging around
     * in savefiles forever. */
    log_attribute("attribute.c unserialize_hash() "
                  "ignored %lu trailing octets",
                  (long unsigned) dio_input_remaining(&din));
  }

  return A_SERIAL_OK;
}

/****************************************************************************
  Send current state to the server. Note that the current
  implementation will send all attributes to the server.
****************************************************************************/
void attribute_flush(void)
{
  struct player *pplayer = client_player();

  if (!pplayer || client_is_observer() || !pplayer->is_alive) {
    return;
  }

  fc_assert_ret(NULL != attribute_hash);

  if (0 == attribute_hash_size(attribute_hash))
    return;

  if (pplayer->attribute_block.data) {
    free(pplayer->attribute_block.data);
    pplayer->attribute_block.data = NULL;
  }

  serialize_hash(attribute_hash, &pplayer->attribute_block.data,
                 &pplayer->attribute_block.length);
  send_attribute_block(pplayer, &client.conn);
}

/****************************************************************************
  Recreate the attribute set from the player's
  attribute_block. Shouldn't be used by normal code.
****************************************************************************/
void attribute_restore(void)
{
  struct player *pplayer = client_player();

  if (!pplayer) {
    return;
  }

  fc_assert_ret(attribute_hash != NULL);

  switch (unserialize_hash(attribute_hash,
                           pplayer->attribute_block.data,
                           pplayer->attribute_block.length)) {
  case A_SERIAL_FAIL:
    log_error(_("There has been a CMA error. "
                "Your citizen governor settings may be broken."));
    break;
  case A_SERIAL_OLD:
    log_normal(_("Old attributes detected and removed."));
    break;
  default:
    break;
  };
}

/****************************************************************************
  Low-level function to set an attribute.  If data_length is zero the
  attribute is removed.
****************************************************************************/
void attribute_set(int key, int id, int x, int y, size_t data_length,
                   const void *const data)
{
  struct attr_key akey = { .key = key, .id = id, .x = x, .y = y };

  log_attribute("attribute_set(key = %d, id = %d, x = %d, y = %d, "
                "data_length = %lu, data = %p)", key, id, x, y,
                (long unsigned) data_length, data);

  fc_assert_ret(NULL != attribute_hash);

  if (0 != data_length) {
    void *pvalue = fc_malloc(data_length + 4);
    struct data_out dout;

    dio_output_init(&dout, pvalue, data_length + 4);
    dio_put_uint32(&dout, data_length);
    dio_put_memory(&dout, data, data_length);

    attribute_hash_replace(attribute_hash, &akey, pvalue);
  } else {
    attribute_hash_remove(attribute_hash, &akey);
  }
}

/****************************************************************************
  Low-level function to get an attribute. If data hasn't enough space
  to hold the attribute data isn't set to the attribute. Returns the
  actual size of the attribute. Can be zero if the attribute is
  unset. To get the size of an attribute use 
    size = attribute_get(key, id, x, y, 0, NULL)
*****************************************************************************/
size_t attribute_get(int key, int id, int x, int y, size_t max_data_length,
                     void *data)
{
  struct attr_key akey = { .key = key, .id = id, .x = x, .y = y };
  void *pvalue;
  int length;
  struct data_in din;

  log_attribute("attribute_get(key = %d, id = %d, x = %d, y = %d, "
                "max_data_length = %lu, data = %p)", key, id, x, y,
                (long unsigned) max_data_length, data);

  fc_assert_ret_val(NULL != attribute_hash, 0);

  if (!attribute_hash_lookup(attribute_hash, &akey, &pvalue)) {
    log_attribute("  not found");
    return 0;
  }

  dio_input_init(&din, pvalue, 0xffffffff);
  dio_get_uint32(&din, &length);

  if (length <= max_data_length) {
    dio_get_memory(&din, data, length);
  }

  log_attribute("  found length = %d", length);
  return length;
}

/****************************************************************************
  Set unit related attribute
*****************************************************************************/
void attr_unit_set(enum attr_unit what, int unit_id, size_t data_length,
		   const void *const data)
{
  attribute_set(what, unit_id, -1, -2, data_length, data);
}

/****************************************************************************
  Get unit related attribute
*****************************************************************************/
size_t attr_unit_get(enum attr_unit what, int unit_id, size_t max_data_length,
		  void *data)
{
  return attribute_get(what, unit_id, -1, -2, max_data_length, data);
}

/****************************************************************************
  Set unit related integer attribute
*****************************************************************************/
void attr_unit_set_int(enum attr_unit what, int unit_id, int data)
{
  attr_unit_set(what, unit_id, sizeof(int), &data);
}

/****************************************************************************
  Get unit related integer attribute
*****************************************************************************/
size_t attr_unit_get_int(enum attr_unit what, int unit_id, int *data)
{
  return attr_unit_get(what, unit_id, sizeof(int), data);
}

/****************************************************************************
  Set city related attribute
*****************************************************************************/
void attr_city_set(enum attr_city what, int city_id, size_t data_length,
		   const void *const data)
{
  attribute_set(what, city_id, -1, -1, data_length, data);
}

/****************************************************************************
  Get city related attribute
*****************************************************************************/
size_t attr_city_get(enum attr_city what, int city_id, size_t max_data_length,
		  void *data)
{
  return attribute_get(what, city_id, -1, -1, max_data_length, data);
}

/****************************************************************************
  Set city related integer attribute
*****************************************************************************/
void attr_city_set_int(enum attr_city what, int city_id, int data)
{
  attr_city_set(what, city_id, sizeof(int), &data);
}

/****************************************************************************
  Get city related integer attribute
*****************************************************************************/
size_t attr_city_get_int(enum attr_city what, int city_id, int *data)
{
  return attr_city_get(what, city_id, sizeof(int), data);
}

/****************************************************************************
  Set player related attribute
*****************************************************************************/
void attr_player_set(enum attr_player what, int player_id, size_t data_length,
		     const void *const data)
{
  attribute_set(what, player_id, -1, -1, data_length, data);
}

/****************************************************************************
  Get player related attribute
*****************************************************************************/
size_t attr_player_get(enum attr_player what, int player_id,
		    size_t max_data_length, void *data)
{
  return attribute_get(what, player_id, -1, -1, max_data_length, data);
}

/****************************************************************************
  Set tile related attribute
*****************************************************************************/
void attr_tile_set(enum attr_tile what, int x, int y, size_t data_length,
		   const void *const data)
{
  attribute_set(what, -1, x, y, data_length, data);
}

/****************************************************************************
  Get tile related attribute
*****************************************************************************/
size_t attr_tile_get(enum attr_tile what, int x, int y, size_t max_data_length,
		  void *data)
{
  return attribute_get(what, -1, x, y, max_data_length, data);
}