2010-08-08 10:06:34 +02:00
|
|
|
#include <platform.h>
|
|
|
|
#include <kernel/config.h>
|
|
|
|
#include "key.h"
|
|
|
|
|
2018-09-29 11:37:17 +02:00
|
|
|
#include <kernel/attrib.h>
|
2018-09-29 13:21:46 +02:00
|
|
|
#include <kernel/gamedata.h>
|
2017-08-20 12:58:05 +02:00
|
|
|
#include <util/log.h>
|
2016-02-09 06:43:19 +01:00
|
|
|
#include <storage.h>
|
|
|
|
|
2018-09-15 18:35:27 +02:00
|
|
|
#include <errno.h>
|
2016-02-09 06:43:19 +01:00
|
|
|
#include <stdlib.h>
|
2016-02-09 13:56:57 +01:00
|
|
|
#include <string.h>
|
2016-02-09 06:43:19 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static void a_writekeys(const variant *var, const void *o, storage *store) {
|
|
|
|
int i, *keys = (int *)var->v;
|
2017-08-20 12:58:05 +02:00
|
|
|
int n = 0;
|
|
|
|
if (keys) {
|
|
|
|
assert(keys[0] < 4096 && keys[0]>0);
|
|
|
|
n = keys[0];
|
|
|
|
}
|
|
|
|
WRITE_INT(store, n);
|
|
|
|
for (i = 0; i < n; ++i) {
|
2017-02-13 18:48:38 +01:00
|
|
|
WRITE_INT(store, keys[i * 2 + 1]);
|
|
|
|
WRITE_INT(store, keys[i * 2 + 2]);
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-20 12:58:05 +02:00
|
|
|
static int keys_lower_bound(int *base, int k, int l, int r) {
|
|
|
|
int km = k;
|
|
|
|
int *p = base + 1;
|
|
|
|
|
|
|
|
while (l != r) {
|
|
|
|
int m = (l + r) / 2;
|
|
|
|
km = p[m * 2];
|
|
|
|
if (km < k) {
|
|
|
|
if (l == m) l = r;
|
|
|
|
else l = m;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (r == m) r = l;
|
|
|
|
else r = m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2017-08-20 17:45:03 +02:00
|
|
|
static int keys_size(int n) {
|
|
|
|
/* TODO maybe use log2 from https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog */
|
|
|
|
assert(n > 0 && n <= 4096);
|
|
|
|
if (n <= 1) return 1;
|
|
|
|
if (n <= 4) return 4;
|
2017-09-17 21:09:23 +02:00
|
|
|
if (n <= 8) return 8;
|
2017-08-20 17:45:03 +02:00
|
|
|
if (n <= 16) return 16;
|
|
|
|
if (n <= 256) return 256;
|
2017-09-17 21:09:23 +02:00
|
|
|
if (n <= 512) return 512;
|
|
|
|
if (n <= 1024) return 1024;
|
|
|
|
if (n <= 2048) return 2048;
|
2017-08-20 17:45:03 +02:00
|
|
|
return 4096;
|
|
|
|
}
|
|
|
|
|
2018-09-15 18:35:27 +02:00
|
|
|
static int read_flags(gamedata *data, int *keys, int n) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
int key;
|
|
|
|
READ_INT(data->store, &key);
|
|
|
|
keys[i * 2] = key;
|
|
|
|
keys[i * 2 + 1] = 1;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef KEYVAL_VERSION
|
|
|
|
static int read_keyval(gamedata *data, int *keys, int n) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
int key, val;
|
|
|
|
READ_INT(data->store, &key);
|
|
|
|
READ_INT(data->store, &val);
|
|
|
|
keys[i * 2] = key;
|
|
|
|
keys[i * 2 + 1] = val;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FIXATKEYS_VERSION
|
|
|
|
static int read_keyval_orig(gamedata *data, int *keys, int n) {
|
|
|
|
int i, j = 0, dk = -1;
|
|
|
|
for (i = 0; i != n; ++i) {
|
|
|
|
int key, val;
|
|
|
|
READ_INT(data->store, &key);
|
|
|
|
READ_INT(data->store, &val);
|
|
|
|
if (key > dk) {
|
|
|
|
keys[j * 2] = key;
|
|
|
|
keys[j * 2 + 1] = val;
|
|
|
|
dk = key;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static int a_readkeys(variant *var, void *owner, gamedata *data) {
|
2018-12-02 11:13:58 +01:00
|
|
|
int n, ksn, *keys;
|
2017-08-20 12:58:05 +02:00
|
|
|
|
|
|
|
READ_INT(data->store, &n);
|
|
|
|
assert(n < 4096 && n >= 0);
|
|
|
|
if (n == 0) {
|
2017-02-12 19:57:02 +01:00
|
|
|
return AT_READ_FAIL;
|
|
|
|
}
|
2018-09-15 18:35:27 +02:00
|
|
|
ksn = keys_size(n);
|
|
|
|
keys = malloc((ksn * 2 + 1) * sizeof(int));
|
|
|
|
if (data->version >= FIXATKEYS_VERSION) {
|
|
|
|
n = read_keyval(data, keys + 1, n);
|
|
|
|
}
|
|
|
|
else if (data->version >= KEYVAL_VERSION) {
|
|
|
|
int m = read_keyval_orig(data, keys + 1, n);
|
|
|
|
if (n != m) {
|
|
|
|
int ksm = keys_size(m);
|
|
|
|
if (ksm != ksn) {
|
|
|
|
int *nkeys = (int *)realloc(keys, (ksm * 2 + 1) * sizeof(int));
|
|
|
|
if (nkeys != NULL) {
|
|
|
|
keys = nkeys;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
log_error("a_readkeys allocation failed: %s", strerror(errno));
|
|
|
|
return AT_READ_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n = m;
|
2017-02-13 18:48:38 +01:00
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
2018-09-15 18:35:27 +02:00
|
|
|
else {
|
|
|
|
n = read_flags(data, keys + 1, n);
|
|
|
|
}
|
|
|
|
keys[0] = n;
|
2017-08-20 12:58:05 +02:00
|
|
|
if (data->version < SORTKEYS_VERSION) {
|
2018-12-02 11:13:58 +01:00
|
|
|
int i, e = 1;
|
2017-08-20 12:58:05 +02:00
|
|
|
for (i = 1; i != n; ++i) {
|
|
|
|
int k = keys[i * 2 + 1];
|
|
|
|
int v = keys[i * 2 + 2];
|
|
|
|
int l = keys_lower_bound(keys, k, 0, e);
|
|
|
|
if (l != e) {
|
|
|
|
int km = keys[l * 2 + 1];
|
|
|
|
if (km == k) {
|
|
|
|
int vm = keys[l * 2 + 2];
|
|
|
|
if (v != vm) {
|
|
|
|
log_error("key %d has values %d and %d", k, v, vm);
|
|
|
|
}
|
|
|
|
--e;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (e > l) {
|
|
|
|
memmove(keys + 2 * l + 3, keys + 2 * l + 1, (e - l) * 2 * sizeof(int));
|
|
|
|
}
|
|
|
|
keys[2 * l + 1] = k;
|
|
|
|
keys[2 * l + 2] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++e;
|
|
|
|
}
|
|
|
|
if (e != n) {
|
2017-08-20 17:45:03 +02:00
|
|
|
int sz = keys_size(n);
|
|
|
|
if (e > sz) {
|
2017-11-20 14:44:02 +01:00
|
|
|
int *k;
|
2017-08-20 17:45:03 +02:00
|
|
|
sz = keys_size(e);
|
2017-11-20 14:44:02 +01:00
|
|
|
k = realloc(keys, sizeof(int)*(2 * sz + 1));
|
|
|
|
if (!k) {
|
|
|
|
free(keys);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
keys = k;
|
2017-08-20 17:45:03 +02:00
|
|
|
keys[0] = e;
|
|
|
|
}
|
2017-08-20 12:58:05 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-09 21:20:43 +01:00
|
|
|
var->v = keys;
|
2016-02-09 06:43:19 +01:00
|
|
|
return AT_READ_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-09 21:20:43 +01:00
|
|
|
static int a_readkey(variant *var, void *owner, struct gamedata *data) {
|
|
|
|
int res = a_readint(var, owner, data);
|
2017-08-20 12:58:05 +02:00
|
|
|
if (data->version >= KEYVAL_VERSION) {
|
2017-08-16 21:48:04 +02:00
|
|
|
return AT_READ_FAIL;
|
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
return (res != AT_READ_FAIL) ? AT_READ_DEPR : res;
|
|
|
|
}
|
|
|
|
|
|
|
|
attrib_type at_keys = {
|
|
|
|
"keys",
|
|
|
|
NULL,
|
2018-02-09 21:20:43 +01:00
|
|
|
a_free_voidptr,
|
2016-02-09 06:43:19 +01:00
|
|
|
NULL,
|
|
|
|
a_writekeys,
|
|
|
|
a_readkeys,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-02-11 22:15:21 +01:00
|
|
|
static void a_upgradekeys(attrib **alist, attrib *abegin) {
|
2017-08-16 21:33:02 +02:00
|
|
|
attrib *a, *ak;
|
|
|
|
|
|
|
|
ak = a_find(*alist, &at_keys);
|
|
|
|
if (ak) alist = &ak;
|
2016-02-09 06:43:19 +01:00
|
|
|
for (a = abegin; a && a->type == abegin->type; a = a->next) {
|
2017-08-16 21:33:02 +02:00
|
|
|
key_set(alist, a->data.i, 1);
|
2017-02-14 12:15:36 +01:00
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
2010-08-08 10:06:34 +02:00
|
|
|
|
|
|
|
attrib_type at_key = {
|
2015-01-30 20:37:14 +01:00
|
|
|
"key",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2017-08-16 21:33:02 +02:00
|
|
|
NULL,
|
2016-02-09 06:43:19 +01:00
|
|
|
a_readkey,
|
|
|
|
a_upgradekeys
|
2010-08-08 10:06:34 +02:00
|
|
|
};
|
|
|
|
|
2017-08-20 12:58:05 +02:00
|
|
|
static int* keys_get(int *base, int i)
|
|
|
|
{
|
|
|
|
int n = base[0];
|
|
|
|
assert(i >= 0 && i < n);
|
|
|
|
return base + 1 + i * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int *keys_update(int *base, int key, int val)
|
|
|
|
{
|
|
|
|
int *kv;
|
|
|
|
int n = base[0];
|
|
|
|
int l = keys_lower_bound(base, key, 0, n);
|
|
|
|
if (l < n) {
|
|
|
|
kv = keys_get(base, l);
|
|
|
|
if (kv[0] == key) {
|
|
|
|
kv[1] = val;
|
|
|
|
}
|
|
|
|
else {
|
2017-08-20 17:45:03 +02:00
|
|
|
int sz = keys_size(n);
|
2017-08-20 12:58:05 +02:00
|
|
|
assert(kv[0] > key);
|
2017-08-20 17:45:03 +02:00
|
|
|
if (n + 1 > sz) {
|
2018-12-15 20:01:51 +01:00
|
|
|
int *tmp;
|
2017-08-20 17:45:03 +02:00
|
|
|
ptrdiff_t diff = kv - base;
|
|
|
|
sz = keys_size(n + 1);
|
2018-12-15 20:01:51 +01:00
|
|
|
tmp = realloc(base, (sz * 2 + 1) * sizeof(int));
|
|
|
|
if (!tmp) abort();
|
|
|
|
base = tmp;
|
2017-08-20 17:45:03 +02:00
|
|
|
kv = base + diff;
|
|
|
|
}
|
2017-08-20 12:58:05 +02:00
|
|
|
base[0] = n + 1;
|
|
|
|
memmove(kv + 2, kv, 2 * sizeof(int) * (n - l));
|
|
|
|
kv[0] = key;
|
|
|
|
kv[1] = val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-08-20 17:45:03 +02:00
|
|
|
int sz = keys_size(n);
|
|
|
|
if (n + 1 > sz) {
|
2018-11-26 22:07:55 +01:00
|
|
|
void * tmp;
|
2017-08-20 17:45:03 +02:00
|
|
|
sz = keys_size(n + 1);
|
2018-11-26 22:07:55 +01:00
|
|
|
tmp = realloc(base, (sz * 2 + 1) * sizeof(int));
|
|
|
|
if (!tmp) abort();
|
|
|
|
base = (int *)tmp;
|
2017-08-20 17:45:03 +02:00
|
|
|
}
|
2017-08-20 12:58:05 +02:00
|
|
|
base[0] = n + 1;
|
|
|
|
kv = keys_get(base, l);
|
|
|
|
kv[0] = key;
|
|
|
|
kv[1] = val;
|
|
|
|
}
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:48:38 +01:00
|
|
|
void key_set(attrib ** alist, int key, int val)
|
2016-02-09 06:43:19 +01:00
|
|
|
{
|
2017-08-20 12:58:05 +02:00
|
|
|
int *keys;
|
2016-02-09 06:43:19 +01:00
|
|
|
attrib *a;
|
|
|
|
assert(key != 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_find(*alist, &at_keys);
|
2016-02-09 06:43:19 +01:00
|
|
|
if (!a) {
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_add(alist, a_new(&at_keys));
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
2016-02-09 07:03:11 +01:00
|
|
|
keys = (int *)a->data.v;
|
2017-08-20 12:58:05 +02:00
|
|
|
if (!keys) {
|
2017-08-20 17:45:03 +02:00
|
|
|
int sz = keys_size(1);
|
|
|
|
a->data.v = keys = malloc((2 * sz + 1) * sizeof(int));
|
2018-12-15 20:01:51 +01:00
|
|
|
if (!keys) abort();
|
2017-08-20 12:58:05 +02:00
|
|
|
keys[0] = 1;
|
|
|
|
keys[1] = key;
|
|
|
|
keys[2] = val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
a->data.v = keys = keys_update(keys, key, val);
|
|
|
|
assert(keys[0] < 4096 && keys[0] >= 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void key_unset(attrib ** alist, int key)
|
|
|
|
{
|
|
|
|
attrib *a;
|
|
|
|
assert(key != 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_find(*alist, &at_keys);
|
2016-02-09 06:43:19 +01:00
|
|
|
if (a) {
|
2017-08-20 12:58:05 +02:00
|
|
|
int *keys = (int *)a->data.v;
|
2016-02-09 07:03:11 +01:00
|
|
|
if (keys) {
|
2017-02-13 18:48:38 +01:00
|
|
|
int n = keys[0];
|
2017-08-20 12:58:05 +02:00
|
|
|
int l = keys_lower_bound(keys, key, 0, n);
|
|
|
|
if (l < n) {
|
|
|
|
int *kv = keys_get(keys, l);
|
|
|
|
if (kv[0] == key) {
|
2017-08-20 17:45:03 +02:00
|
|
|
kv[1] = 0; /* do not delete, just set to 0 */
|
2016-02-09 07:03:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 18:48:38 +01:00
|
|
|
int key_get(attrib *alist, int key) {
|
2016-02-09 06:43:19 +01:00
|
|
|
attrib *a;
|
|
|
|
assert(key != 0);
|
2016-02-09 07:03:11 +01:00
|
|
|
a = a_find(alist, &at_keys);
|
|
|
|
if (a) {
|
2017-08-20 12:58:05 +02:00
|
|
|
int *keys = (int *)a->data.v;
|
2016-02-09 07:03:11 +01:00
|
|
|
if (keys) {
|
2017-08-20 12:58:05 +02:00
|
|
|
int n = keys[0];
|
|
|
|
int l = keys_lower_bound(keys, key, 0, n);
|
|
|
|
if (l < n) {
|
|
|
|
int * kv = keys_get(keys, l);
|
|
|
|
if (kv[0] == key) {
|
|
|
|
return kv[1];
|
2016-02-09 07:03:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-13 18:48:38 +01:00
|
|
|
return 0;
|
2016-02-09 06:43:19 +01:00
|
|
|
}
|