From 828ca5f5f6c7b109c5e3c2eae05360e46a7b2d18 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 8 Oct 2017 12:39:41 +0200 Subject: [PATCH 1/9] refactor: split order_data from order, force use of ids (to be replaced with db backend). --- src/kernel/order.c | 79 +++++++++++++++++++++++++++++++++-------- src/kernel/order.h | 4 +-- src/kernel/order.test.c | 10 +++--- src/laws.test.c | 2 +- 4 files changed, 72 insertions(+), 23 deletions(-) diff --git a/src/kernel/order.c b/src/kernel/order.c index a48825333..2f209da24 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -31,8 +31,8 @@ #include # define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF) -# define ORD_LOCALE(ord) locale_array[(ord)->data->_lindex]->lang -# define ORD_STRING(ord) (ord)->data->_str +# define OD_LOCALE(odata) locale_array[(odata)->_lindex]->lang +# define OD_STRING(odata) (odata)->_str typedef struct locale_data { struct order_data *short_orders; @@ -48,6 +48,22 @@ typedef struct order_data { int _lindex; } order_data; +#include + +static selist * orders; + +order_data *load_data(int id) { + order_data * od = (order_data *)selist_get(orders, id - 1); + ++od->_refcount; + return od; +} + +int add_data(order_data *od) { + ++od->_refcount; + selist_push(&orders, od); + return selist_length(orders); +} + static void release_data(order_data * data) { if (data) { @@ -57,6 +73,20 @@ static void release_data(order_data * data) } } +void free_data_cb(void *entry) { + order_data *od = (order_data *)entry; + if (od->_refcount > 1) { + log_error("refcount=%d for order %s", od->_refcount, od->_str); + } + release_data(od); +} + +void free_data(void) { + selist_foreach(orders, free_data_cb); + selist_free(orders); + orders = NULL; +} + void replace_order(order ** dlist, order * orig, const order * src) { assert(src); @@ -64,7 +94,7 @@ void replace_order(order ** dlist, order * orig, const order * src) assert(dlist); while (*dlist != NULL) { order *dst = *dlist; - if (dst->data == orig->data) { + if (dst->id == orig->id) { order *cpy = copy_order(src); *dlist = cpy; cpy->next = dst->next; @@ -89,7 +119,8 @@ keyword_t getkeyword(const order * ord) */ char* get_command(const order *ord, char *sbuffer, size_t size) { char *bufp = sbuffer; - const char *text = ORD_STRING(ord); + order_data *od; + const char * text; keyword_t kwd = ORD_KEYWORD(ord); int bytes; @@ -111,8 +142,11 @@ char* get_command(const order *ord, char *sbuffer, size_t size) { WARN_STATIC_BUFFER(); } } + + od = load_data(ord->id); + text = OD_STRING(od); if (kwd != NOKEYWORD) { - const struct locale *lang = ORD_LOCALE(ord); + const struct locale *lang = OD_LOCALE(od); if (size > 0) { const char *str = (const char *)LOC(lang, keyword(kwd)); assert(str); @@ -142,6 +176,7 @@ char* get_command(const order *ord, char *sbuffer, size_t size) { } } } + release_data(od); if (size > 0) *bufp = 0; return sbuffer; } @@ -150,8 +185,6 @@ void free_order(order * ord) { if (ord != NULL) { assert(ord->next == 0); - - release_data(ord->data); free(ord); } } @@ -162,8 +195,7 @@ order *copy_order(const order * src) order *ord = (order *)malloc(sizeof(order)); ord->next = NULL; ord->command = src->command; - ord->data = src->data; - ++ord->data->_refcount; + ord->id = src->id; return ord; } return NULL; @@ -277,12 +309,14 @@ void close_orders(void) { locale_array[i] = 0; } } + free_data(); } static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, bool noerror, const struct locale *lang) { int lindex; + order_data *od; assert(ord); if (kwd == NOKEYWORD || keyword_disabled(kwd)) { @@ -317,7 +351,10 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p ord->next = NULL; while (isspace(*(unsigned char *)sptr)) ++sptr; - ord->data = create_data(kwd, sptr, lindex); + + od = create_data(kwd, sptr, lindex); + ord->id = add_data(od); + release_data(od); return ord; } @@ -573,9 +610,11 @@ char *write_order(const order * ord, char *buffer, size_t size) else { keyword_t kwd = ORD_KEYWORD(ord); if (kwd == NOKEYWORD) { - const char *text = ORD_STRING(ord); + order_data *od = load_data(ord->id); + const char *text = OD_STRING(od); if (text) strlcpy(buffer, (const char *)text, size); else buffer[0] = 0; + release_data(od); } else { get_command(ord, buffer, size); @@ -593,7 +632,19 @@ void push_order(order ** ordp, order * ord) keyword_t init_order(const struct order *ord) { - assert(ord && ord->data); - init_tokens_str(ord->data->_str); - return ORD_KEYWORD(ord); + static order_data *od; + + if (!ord) { + release_data(od); + return NOKEYWORD; + } + else { + if (od) { + // TODO: warning + release_data(od); + } + od = load_data(ord->id); + init_tokens_str(od->_str); + return ORD_KEYWORD(ord); + } } diff --git a/src/kernel/order.h b/src/kernel/order.h index b9d235c88..f26174ee4 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -30,8 +30,6 @@ extern "C" { * implemented yet) saving approx. 50% of all string-related memory. */ - struct order_data; - #define CMD_QUIET 0x010000 #define CMD_PERSIST 0x020000 #define CMD_DEFAULT 0x040000 @@ -39,7 +37,7 @@ extern "C" { typedef struct order { struct order *next; /* do not access this data: */ - struct order_data *data; + int id; int command; } order; diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c index 71938ca7e..e1656efc9 100644 --- a/src/kernel/order.test.c +++ b/src/kernel/order.test.c @@ -48,35 +48,35 @@ static void test_parse_order(CuTest *tc) { ord = parse_order("!MOVE NORTH", lang); CuAssertPtrNotNull(tc, ord); - CuAssertPtrNotNull(tc, ord->data); + CuAssertTrue(tc, ord->id > 0); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE | CMD_QUIET, ord->command); free_order(ord); ord = parse_order("@MOVE NORTH", lang); CuAssertPtrNotNull(tc, ord); - CuAssertPtrNotNull(tc, ord->data); + CuAssertTrue(tc, ord->id > 0); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST, ord->command); free_order(ord); ord = parse_order("!@MOVE NORTH", lang); CuAssertPtrNotNull(tc, ord); - CuAssertPtrNotNull(tc, ord->data); + CuAssertTrue(tc, ord->id > 0); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command); free_order(ord); ord = parse_order("@!MOVE NORTH", lang); CuAssertPtrNotNull(tc, ord); - CuAssertPtrNotNull(tc, ord->data); + CuAssertTrue(tc, ord->id > 0); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command); free_order(ord); ord = parse_order(" !@MOVE NORTH", lang); CuAssertPtrNotNull(tc, ord); - CuAssertPtrNotNull(tc, ord->data); + CuAssertTrue(tc, ord->id > 0); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertIntEquals(tc, K_MOVE | CMD_PERSIST | CMD_QUIET, ord->command); free_order(ord); diff --git a/src/laws.test.c b/src/laws.test.c index ee5c55f19..f11ca458d 100644 --- a/src/laws.test.c +++ b/src/laws.test.c @@ -1005,7 +1005,7 @@ static void test_long_order_normal(CuTest *tc) { fset(u, UFL_LONGACTION); unit_addorder(u, ord = create_order(K_MOVE, u->faction->locale, 0)); update_long_order(u); - CuAssertPtrEquals(tc, ord->data, u->thisorder->data); + CuAssertIntEquals(tc, ord->id, u->thisorder->id); CuAssertIntEquals(tc, 0, fval(u, UFL_MOVED)); CuAssertIntEquals(tc, 0, fval(u, UFL_LONGACTION)); CuAssertPtrNotNull(tc, u->orders); From 97ff152f1722d9f543671c58f4891e8c0e7afaa9 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 8 Oct 2017 15:34:40 +0200 Subject: [PATCH 2/9] remove order de-duplication for short and study orders. --- src/kernel/order.c | 144 +++++++++++++-------------------------------- src/spy.c | 2 +- src/study.c | 3 +- 3 files changed, 43 insertions(+), 106 deletions(-) diff --git a/src/kernel/order.c b/src/kernel/order.c index 2f209da24..bbb211cb3 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -31,21 +31,13 @@ #include # define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF) -# define OD_LOCALE(odata) locale_array[(odata)->_lindex]->lang -# define OD_STRING(odata) (odata)->_str - -typedef struct locale_data { - struct order_data *short_orders; - struct order_data *study_orders[MAXSKILLS]; - const struct locale *lang; -} locale_data; - -static struct locale_data *locale_array[MAXLOCALES]; +# define OD_LOCALE(odata) ((odata) ? (odata)->lang : NULL) +# define OD_STRING(odata) ((odata) ? (odata)->_str : NULL) typedef struct order_data { const char *_str; int _refcount; - int _lindex; + const struct locale *lang; } order_data; #include @@ -53,15 +45,21 @@ typedef struct order_data { static selist * orders; order_data *load_data(int id) { - order_data * od = (order_data *)selist_get(orders, id - 1); - ++od->_refcount; - return od; + if (id > 0) { + order_data * od = (order_data *)selist_get(orders, id - 1); + ++od->_refcount; + return od; + } + return NULL; } int add_data(order_data *od) { - ++od->_refcount; - selist_push(&orders, od); - return selist_length(orders); + if (od->_str) { + ++od->_refcount; + selist_push(&orders, od); + return selist_length(orders); + } + return 0; } static void release_data(order_data * data) @@ -219,103 +217,42 @@ void free_orders(order ** olist) } } -static char *mkdata(order_data **pdata, size_t len, int lindex, const char *str) +static char *mkdata(order_data **pdata, size_t len, const struct locale *lang, const char *str) { order_data *data; char *result; data = malloc(sizeof(order_data) + len + 1); result = (char *)(data + 1); - data->_lindex = lindex; + data->lang = lang; data->_refcount = 0; - data->_str = 0; - data->_str = (len > 0) ? result : 0; + data->_str = (len > 0) ? result : NULL; if (str) strcpy(result, str); if (pdata) *pdata = data; return result; } -static order_data *create_data(keyword_t kwd, const char *sptr, int lindex) +static order_data *create_data(keyword_t kwd, const char *sptr, const struct locale *lang) { const char *s = sptr; order_data *data; - const struct locale *lang = locale_array[lindex]->lang; if (kwd != NOKEYWORD) s = (*sptr) ? sptr : NULL; - /* learning, only one order_data per skill required */ - if (kwd == K_STUDY) { - skill_t sk = get_skill(parse_token_depr(&sptr), lang); - switch (sk) { - case NOSKILL: /* fehler */ - break; - case SK_MAGIC: /* kann parameter haben */ - if (*sptr != 0) - break; - default: /* nur skill als Parameter, keine extras */ - data = locale_array[lindex]->study_orders[sk]; - if (data == NULL) { - const char *skname = skillname(sk, lang); - const char *spc = strchr(skname, ' '); - size_t len = strlen(skname); - char *dst = mkdata(&data, len + (spc ? 3 : 0), lindex, spc ? 0 : skname); - locale_array[lindex]->study_orders[sk] = data; - if (spc) { - dst[0] = '\"'; - memcpy(dst + 1, skname, len); - dst[len + 1] = '\"'; - dst[len + 2] = '\0'; - } - data->_refcount = 1; - } - ++data->_refcount; - return data; - } - } - /* orders with no parameter, only one order_data per order required */ - else if (kwd != NOKEYWORD && *sptr == 0) { - data = locale_array[lindex]->short_orders; - if (data == NULL) { - mkdata(&data, 0, lindex, 0); - data->_refcount = 1; - locale_array[lindex]->short_orders = data; - } - ++data->_refcount; + if (kwd != NOKEYWORD && *sptr == 0) { + mkdata(&data, 0, lang, NULL); + data->_refcount = 1; return data; } - mkdata(&data, s ? strlen(s) : 0, lindex, s); + mkdata(&data, s ? strlen(s) : 0, lang, s); data->_refcount = 1; return data; } -static void clear_localedata(int lindex) { - int i; - release_data(locale_array[lindex]->short_orders); - locale_array[lindex]->short_orders = NULL; - for (i = 0; i != MAXSKILLS; ++i) { - release_data(locale_array[lindex]->study_orders[i]); - locale_array[lindex]->study_orders[i] = 0; - } - locale_array[lindex]->lang = 0; -} - -void close_orders(void) { - int i; - for (i = 0; i != MAXLOCALES; ++i) { - if (locale_array[i]){ - clear_localedata(i); - free(locale_array[i]); - locale_array[i] = 0; - } - } - free_data(); -} - static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, bool noerror, const struct locale *lang) { - int lindex; order_data *od; assert(ord); @@ -335,16 +272,6 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p } } - lindex = locale_index(lang); - assert(lindex < MAXLOCALES); - if (!locale_array[lindex]) { - locale_array[lindex] = (locale_data *)calloc(1, sizeof(locale_data)); - } - else if (locale_array[lindex]->lang != lang) { - clear_localedata(lindex); - } - locale_array[lindex]->lang = lang; - ord->command = (int)kwd; if (persistent) ord->command |= CMD_PERSIST; if (noerror) ord->command |= CMD_QUIET; @@ -352,7 +279,7 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p while (isspace(*(unsigned char *)sptr)) ++sptr; - od = create_data(kwd, sptr, lindex); + od = create_data(kwd, sptr, lang); ord->id = add_data(od); release_data(od); @@ -630,21 +557,30 @@ void push_order(order ** ordp, order * ord) *ordp = ord; } +static order_data *parser_od; + keyword_t init_order(const struct order *ord) { - static order_data *od; - if (!ord) { - release_data(od); + release_data(parser_od); + parser_od = NULL; return NOKEYWORD; } else { - if (od) { + if (parser_od) { // TODO: warning - release_data(od); + release_data(parser_od); } - od = load_data(ord->id); - init_tokens_str(od->_str); + parser_od = load_data(ord->id); + init_tokens_str(OD_STRING(parser_od)); return ORD_KEYWORD(ord); } } + +void close_orders(void) { + if (parser_od) { + init_order(NULL); + } + free_data(); +} + diff --git a/src/spy.c b/src/spy.c index 210be63e4..ae01656bf 100644 --- a/src/spy.c +++ b/src/spy.c @@ -496,8 +496,8 @@ int sabotage_cmd(unit * u, struct order *ord) init_order(ord); s = getstrtoken(); - p = findparam(s, u->faction->locale); + init_order(NULL); switch (p) { case P_SHIP: diff --git a/src/study.c b/src/study.c index 9d37a7648..a66efd662 100644 --- a/src/study.c +++ b/src/study.c @@ -448,6 +448,7 @@ int teach_cmd(unit * teacher, struct order *ord) if (academy_students > 0 && sk_academy!=NOSKILL) { academy_teaching_bonus(teacher, sk_academy, academy_students); } + init_order(NULL); return 0; } @@ -784,7 +785,7 @@ int study_cmd(unit * u, order * ord) mage = create_mage(u, u->faction->magiegebiet); } } - + init_order(NULL); return 0; } From 28d75f7cfc30cd556f00b7e9f7b21600b0fa82a1 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 8 Oct 2017 17:07:42 +0200 Subject: [PATCH 3/9] remove the locale from orders. --- scripts/tests/orders.lua | 3 +- src/bind_unit.c | 121 +++++++++++++++++++++---------------- src/bindings.c | 14 ----- src/bindings.h | 1 - src/creport.c | 35 +++++------ src/kernel/command.c | 3 +- src/kernel/jsonconf.test.c | 2 +- src/kernel/order.c | 26 ++++---- src/kernel/order.h | 8 ++- src/kernel/order.test.c | 18 +++--- src/kernel/save.c | 2 +- src/keyword.test.c | 2 +- src/laws.c | 2 +- src/monsters.test.c | 6 +- src/report.c | 21 ++++--- src/reports.c | 53 ++++++++-------- 16 files changed, 163 insertions(+), 154 deletions(-) diff --git a/scripts/tests/orders.lua b/scripts/tests/orders.lua index af0919640..63abaed00 100644 --- a/scripts/tests/orders.lua +++ b/scripts/tests/orders.lua @@ -47,7 +47,8 @@ function test_give() assert_not_equal(5, u2:get_item("money")) end -function test_make_temp() +function disable_test_make_temp() + -- disabled because of TOLUA_ORDERS_CLOSURE u:add_order("MACHE TEMP 123 'Herpderp'") u:add_order("// this comment will be copied") u:add_order("ENDE") diff --git a/src/bind_unit.c b/src/bind_unit.c index 1e1aee00e..00c2b9e09 100644 --- a/src/bind_unit.c +++ b/src/bind_unit.c @@ -763,6 +763,22 @@ static int tolua_unit_get_spells(lua_State * L) return tolua_selist_push(L, "spellbook", "spell_entry", slist); } +#ifdef TOLUA_ORDERS_CLOSURE +/* TODO: this requires that the locale for write_order is included in the closure */ +static int tolua_orderlist_next(lua_State * L) +{ + order **order_ptr = (order **)lua_touserdata(L, lua_upvalueindex(1)); + order *ord = *order_ptr; + if (ord != NULL) { + char cmd[8192]; + write_order(ord, cmd, sizeof(cmd)); + tolua_pushstring(L, cmd); + *order_ptr = ord->next; + return 1; + } + return 0; +} + static int tolua_unit_get_orders(lua_State * L) { unit *self = (unit *)tolua_tousertype(L, 1, 0); @@ -778,7 +794,7 @@ static int tolua_unit_get_orders(lua_State * L) return 1; } - +#endif static int tolua_unit_get_curse(lua_State *L) { unit *self = (unit *)tolua_tousertype(L, 1, 0); const char *name = tolua_tostring(L, 2, 0); @@ -960,95 +976,96 @@ void tolua_unit_open(lua_State * L) NULL); tolua_beginmodule(L, TOLUA_CAST "event"); { - tolua_function(L, TOLUA_CAST "get_type", &tolua_event_gettype); - tolua_function(L, TOLUA_CAST "get", &tolua_event_get); + tolua_function(L, TOLUA_CAST "get_type", tolua_event_gettype); + tolua_function(L, TOLUA_CAST "get", tolua_event_get); } tolua_endmodule(L); tolua_cclass(L, TOLUA_CAST "unit", TOLUA_CAST "unit", TOLUA_CAST "", NULL); tolua_beginmodule(L, TOLUA_CAST "unit"); { - tolua_function(L, TOLUA_CAST "__tostring", &tolua_unit_tostring); - tolua_function(L, TOLUA_CAST "create", &tolua_unit_create); - tolua_function(L, TOLUA_CAST "destroy", &tolua_unit_destroy); + tolua_function(L, TOLUA_CAST "__tostring", tolua_unit_tostring); + tolua_function(L, TOLUA_CAST "create", tolua_unit_create); + tolua_function(L, TOLUA_CAST "destroy", tolua_unit_destroy); - tolua_variable(L, TOLUA_CAST "name", &tolua_unit_get_name, + tolua_variable(L, TOLUA_CAST "name", tolua_unit_get_name, tolua_unit_set_name); - tolua_variable(L, TOLUA_CAST "faction", &tolua_unit_get_faction, + tolua_variable(L, TOLUA_CAST "faction", tolua_unit_get_faction, tolua_unit_set_faction); tolua_variable(L, TOLUA_CAST "id", tolua_unit_get_id, tolua_unit_set_id); tolua_variable(L, TOLUA_CAST "group", tolua_unit_get_group, tolua_unit_set_group); tolua_variable(L, TOLUA_CAST "info", tolua_unit_get_info, tolua_unit_set_info); - tolua_variable(L, TOLUA_CAST "hp", &tolua_unit_get_hp, tolua_unit_set_hp); - tolua_variable(L, TOLUA_CAST "status", &tolua_unit_get_status, + tolua_variable(L, TOLUA_CAST "hp", tolua_unit_get_hp, tolua_unit_set_hp); + tolua_variable(L, TOLUA_CAST "status", tolua_unit_get_status, tolua_unit_set_status); - tolua_variable(L, TOLUA_CAST "familiar", &tolua_unit_get_familiar, + tolua_variable(L, TOLUA_CAST "familiar", tolua_unit_get_familiar, tolua_unit_set_familiar); - tolua_variable(L, TOLUA_CAST "weight", &tolua_unit_get_weight, 0); - tolua_variable(L, TOLUA_CAST "capacity", &tolua_unit_get_capacity, 0); + tolua_variable(L, TOLUA_CAST "weight", tolua_unit_get_weight, 0); + tolua_variable(L, TOLUA_CAST "capacity", tolua_unit_get_capacity, 0); - tolua_function(L, TOLUA_CAST "add_order", &tolua_unit_add_order); - tolua_function(L, TOLUA_CAST "clear_orders", &tolua_unit_clear_orders); - tolua_variable(L, TOLUA_CAST "orders", &tolua_unit_get_orders, 0); - - tolua_function(L, TOLUA_CAST "get_curse", &tolua_unit_get_curse); - tolua_function(L, TOLUA_CAST "has_attrib", &tolua_unit_has_attrib); + tolua_function(L, TOLUA_CAST "add_order", tolua_unit_add_order); + tolua_function(L, TOLUA_CAST "clear_orders", tolua_unit_clear_orders); +#ifdef TOLUA_ORDERS_CLOSURE + tolua_variable(L, TOLUA_CAST "orders", tolua_unit_get_orders, 0); +#endif + tolua_function(L, TOLUA_CAST "get_curse", tolua_unit_get_curse); + tolua_function(L, TOLUA_CAST "has_attrib", tolua_unit_has_attrib); /* key-attributes for named flags: */ - tolua_function(L, TOLUA_CAST "set_flag", &tolua_unit_set_flag); - tolua_function(L, TOLUA_CAST "get_flag", &tolua_unit_get_flag); - tolua_variable(L, TOLUA_CAST "guard", &tolua_unit_get_guard, - &tolua_unit_set_guard); - tolua_variable(L, TOLUA_CAST "flags", &tolua_unit_get_flags, - &tolua_unit_set_flags); - tolua_variable(L, TOLUA_CAST "age", &tolua_unit_get_age, + tolua_function(L, TOLUA_CAST "set_flag", tolua_unit_set_flag); + tolua_function(L, TOLUA_CAST "get_flag", tolua_unit_get_flag); + tolua_variable(L, TOLUA_CAST "guard", tolua_unit_get_guard, + tolua_unit_set_guard); + tolua_variable(L, TOLUA_CAST "flags", tolua_unit_get_flags, + tolua_unit_set_flags); + tolua_variable(L, TOLUA_CAST "age", tolua_unit_get_age, tolua_unit_set_age); /* items: */ - tolua_function(L, TOLUA_CAST "get_item", &tolua_unit_get_item); - tolua_function(L, TOLUA_CAST "add_item", &tolua_unit_add_item); - tolua_variable(L, TOLUA_CAST "items", &tolua_unit_get_items, 0); - tolua_function(L, TOLUA_CAST "get_pooled", &tolua_unit_get_pooled); - tolua_function(L, TOLUA_CAST "use_pooled", &tolua_unit_use_pooled); + tolua_function(L, TOLUA_CAST "get_item", tolua_unit_get_item); + tolua_function(L, TOLUA_CAST "add_item", tolua_unit_add_item); + tolua_variable(L, TOLUA_CAST "items", tolua_unit_get_items, 0); + tolua_function(L, TOLUA_CAST "get_pooled", tolua_unit_get_pooled); + tolua_function(L, TOLUA_CAST "use_pooled", tolua_unit_use_pooled); /* effects */ - tolua_function(L, TOLUA_CAST "get_potion", &tolua_unit_get_effect); + tolua_function(L, TOLUA_CAST "get_potion", tolua_unit_get_effect); /* skills: */ - tolua_function(L, TOLUA_CAST "get_skill", &tolua_unit_getskill); - tolua_function(L, TOLUA_CAST "eff_skill", &tolua_unit_effskill); - tolua_function(L, TOLUA_CAST "set_skill", &tolua_unit_setskill); + tolua_function(L, TOLUA_CAST "get_skill", tolua_unit_getskill); + tolua_function(L, TOLUA_CAST "eff_skill", tolua_unit_effskill); + tolua_function(L, TOLUA_CAST "set_skill", tolua_unit_setskill); - tolua_function(L, TOLUA_CAST "add_notice", &tolua_unit_addnotice); + tolua_function(L, TOLUA_CAST "add_notice", tolua_unit_addnotice); /* npc logic: */ - tolua_function(L, TOLUA_CAST "add_handler", &tolua_unit_addhandler); + tolua_function(L, TOLUA_CAST "add_handler", tolua_unit_addhandler); - tolua_variable(L, TOLUA_CAST "race_name", &tolua_unit_get_racename, - &tolua_unit_set_racename); - tolua_function(L, TOLUA_CAST "add_spell", &tolua_unit_addspell); - tolua_variable(L, TOLUA_CAST "spells", &tolua_unit_get_spells, 0); - tolua_function(L, TOLUA_CAST "cast_spell", &tolua_unit_castspell); + tolua_variable(L, TOLUA_CAST "race_name", tolua_unit_get_racename, + tolua_unit_set_racename); + tolua_function(L, TOLUA_CAST "add_spell", tolua_unit_addspell); + tolua_variable(L, TOLUA_CAST "spells", tolua_unit_get_spells, 0); + tolua_function(L, TOLUA_CAST "cast_spell", tolua_unit_castspell); - tolua_variable(L, TOLUA_CAST "magic", &tolua_unit_get_magic, + tolua_variable(L, TOLUA_CAST "magic", tolua_unit_get_magic, tolua_unit_set_magic); - tolua_variable(L, TOLUA_CAST "aura", &tolua_unit_get_aura, + tolua_variable(L, TOLUA_CAST "aura", tolua_unit_get_aura, tolua_unit_set_aura); - tolua_variable(L, TOLUA_CAST "building", &tolua_unit_get_building, + tolua_variable(L, TOLUA_CAST "building", tolua_unit_get_building, tolua_unit_set_building); - tolua_variable(L, TOLUA_CAST "ship", &tolua_unit_get_ship, + tolua_variable(L, TOLUA_CAST "ship", tolua_unit_get_ship, tolua_unit_set_ship); - tolua_variable(L, TOLUA_CAST "region", &tolua_unit_get_region, + tolua_variable(L, TOLUA_CAST "region", tolua_unit_get_region, tolua_unit_set_region); - tolua_variable(L, TOLUA_CAST "number", &tolua_unit_get_number, + tolua_variable(L, TOLUA_CAST "number", tolua_unit_get_number, tolua_unit_set_number); - tolua_variable(L, TOLUA_CAST "race", &tolua_unit_get_race, + tolua_variable(L, TOLUA_CAST "race", tolua_unit_get_race, tolua_unit_set_race); - tolua_variable(L, TOLUA_CAST "hp_max", &tolua_unit_get_hpmax, 0); - tolua_variable(L, TOLUA_CAST "aura_max", &tolua_unit_get_auramax, 0); + tolua_variable(L, TOLUA_CAST "hp_max", tolua_unit_get_hpmax, 0); + tolua_variable(L, TOLUA_CAST "aura_max", tolua_unit_get_auramax, 0); - tolua_function(L, TOLUA_CAST "show", &tolua_bufunit); + tolua_function(L, TOLUA_CAST "show", tolua_bufunit); } tolua_endmodule(L); } diff --git a/src/bindings.c b/src/bindings.c index 33d609ee0..7a24a3d07 100755 --- a/src/bindings.c +++ b/src/bindings.c @@ -114,20 +114,6 @@ int log_lua_error(lua_State * L) return 1; } -int tolua_orderlist_next(lua_State * L) -{ - order **order_ptr = (order **)lua_touserdata(L, lua_upvalueindex(1)); - order *ord = *order_ptr; - if (ord != NULL) { - char cmd[8192]; - write_order(ord, cmd, sizeof(cmd)); - tolua_pushstring(L, cmd); - *order_ptr = ord->next; - return 1; - } - return 0; -} - static int tolua_selist_iter(lua_State * L) { selist **qlp = (selist **)lua_touserdata(L, lua_upvalueindex(1)); diff --git a/src/bindings.h b/src/bindings.h index 5cb64a038..b5ebf916b 100755 --- a/src/bindings.h +++ b/src/bindings.h @@ -22,7 +22,6 @@ extern "C" { int tolua_sqlite_open(struct lua_State *L); int tolua_bindings_open(struct lua_State *L, const struct _dictionary_ *d); int tolua_itemlist_next(struct lua_State *L); - int tolua_orderlist_next(struct lua_State *L); int tolua_selist_push(struct lua_State *L, const char *list_type, const char *elem_type, struct selist *list); diff --git a/src/creport.c b/src/creport.c index 4ec7f9b0b..6b3c2cdca 100644 --- a/src/creport.c +++ b/src/creport.c @@ -376,13 +376,13 @@ static int cr_alliance(variant var, char *buffer, const void *userdata) static int cr_skill(variant var, char *buffer, const void *userdata) { - const faction *report = (const faction *)userdata; + const faction *f = (const faction *)userdata; skill_t sk = (skill_t)var.i; UNUSED_ARG(userdata); if (sk != NOSKILL) sprintf(buffer, "\"%s\"", translate(mkname("skill", skillnames[sk]), skillname(sk, - report->locale))); + f->locale))); else strcpy(buffer, "\"\""); return 0; @@ -391,13 +391,14 @@ static int cr_skill(variant var, char *buffer, const void *userdata) static int cr_order(variant var, char *buffer, const void *userdata) { order *ord = (order *)var.v; - UNUSED_ARG(userdata); + const faction *f = (const faction *)userdata; + if (ord != NULL) { char cmd[ORDERSIZE]; char *wp = buffer; const char *rp; - get_command(ord, cmd, sizeof(cmd)); + get_command(ord, f->locale, cmd, sizeof(cmd)); *wp++ = '\"'; for (rp = cmd; *rp;) { @@ -722,11 +723,11 @@ static void cr_output_ship_compat(FILE *F, const ship *sh, const unit *u, cr_output_ship(&strm, sh, u, fcaptain, f, r); } -static int stream_order(stream *out, const struct order *ord) { +static int stream_order(stream *out, const struct order *ord, const struct locale *lang) { const char *str; char ebuf[1025]; char obuf[1024]; - write_order(ord, obuf, sizeof(obuf)); + write_order(ord, lang, obuf, sizeof(obuf)); str = escape_string(obuf, ebuf, sizeof(ebuf)); if (str == ebuf) { ebuf[1024] = 0; @@ -779,6 +780,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, const faction *fother; const char *prefix; bool allied; + const struct locale *lang = f->locale; assert(u && u->number); assert(u->region == r); /* TODO: if this holds true, then why did we pass in r? */ @@ -787,7 +789,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, stream_printf(out, "EINHEIT %d\n", u->no); stream_printf(out, "\"%s\";Name\n", unit_getname(u)); - str = u_description(u, f->locale); + str = u_description(u, lang); if (str) { stream_printf(out, "\"%s\";Beschr\n", str); } @@ -833,7 +835,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, prefix = raceprefix(u); if (prefix) { prefix = mkname("prefix", prefix); - stream_printf(out, "\"%s\";typprefix\n", translate(prefix, LOC(f->locale, + stream_printf(out, "\"%s\";typprefix\n", translate(prefix, LOC(lang, prefix))); } stream_printf(out, "%d;Anzahl\n", u->number); @@ -844,20 +846,20 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { const char *zRace = rc_name_s(u_race(u), NAME_PLURAL); stream_printf(out, "\"%s\";wahrerTyp\n", - translate(zRace, LOC(f->locale, zRace))); + translate(zRace, LOC(lang, zRace))); } } else { const race *irace = u_irace(u); const char *zRace = rc_name_s(irace, NAME_PLURAL); stream_printf(out, "\"%s\";Typ\n", - translate(zRace, LOC(f->locale, zRace))); + translate(zRace, LOC(lang, zRace))); if (u->faction == f && irace != u_race(u)) { assert(skill_enabled(SK_STEALTH) || !"we're resetting this on load, so.. ircase should never be used"); zRace = rc_name_s(u_race(u), NAME_PLURAL); stream_printf(out, "\"%s\";wahrerTyp\n", - translate(zRace, LOC(f->locale, zRace))); + translate(zRace, LOC(lang, zRace))); } } @@ -925,7 +927,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, for (ord = u->old_orders; ord; ord = ord->next) { /* this new order will replace the old defaults */ if (is_persistent(ord)) { - stream_order(out, ord); + stream_order(out, ord, lang); } } for (ord = u->orders; ord; ord = ord->next) { @@ -933,7 +935,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, if (u->old_orders && is_repeated(kwd)) continue; /* unit has defaults */ if (is_persistent(ord)) { - stream_order(out, ord); + stream_order(out, ord, lang); } } @@ -949,7 +951,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, } stream_printf(out, "%d %d;%s\n", u->number * level_days(sv->level), esk, translate(mkname("skill", skillnames[sk]), skillname(sk, - f->locale))); + lang))); } } @@ -963,8 +965,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, const spell *sp = mage->combatspells[i].sp; if (sp) { const char *name = - translate(mkname("spell", sp->sname), spell_name(sp, - f->locale)); + translate(mkname("spell", sp->sname), spell_name(sp, lang)); stream_printf(out, "KAMPFZAUBER %d\n", i); stream_printf(out, "\"%s\";name\n", name); stream_printf(out, "%d;level\n", mage->combatspells[i].level); @@ -999,7 +1000,7 @@ void cr_output_unit(stream *out, const region * r, const faction * f, pr = 1; stream_printf(out, "GEGENSTAENDE\n"); } - stream_printf(out, "%d;%s\n", in, translate(ic, LOC(f->locale, ic))); + stream_printf(out, "%d;%s\n", in, translate(ic, LOC(lang, ic))); } cr_output_curses(out, f, u, TYP_UNIT); diff --git a/src/kernel/command.c b/src/kernel/command.c index 912f21f7d..a6a036fe9 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -14,6 +14,7 @@ #include #include "command.h" +#include #include #include @@ -107,7 +108,7 @@ void do_command(const struct tnode *keys, struct unit *u, struct order *ord) init_order(ord); if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) { char cmd[ORDERSIZE]; - get_command(ord, cmd, sizeof(cmd)); + get_command(ord, u->faction->locale, cmd, sizeof(cmd)); log_warning("%s failed command '%s'\n", unitname(u), cmd); } } diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index cb79cafc4..5408f8fb3 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -637,7 +637,7 @@ static void test_infinitive_from_config(CuTest *tc) { CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang)); ord = create_order(K_STUDY, lang, ""); - CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer))); + CuAssertStrEquals(tc, "LERNE", get_command(ord, lang, buffer, sizeof(buffer))); free_order(ord); cJSON_Delete(json); test_cleanup(); diff --git a/src/kernel/order.c b/src/kernel/order.c index bbb211cb3..154b3f451 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -31,13 +31,11 @@ #include # define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF) -# define OD_LOCALE(odata) ((odata) ? (odata)->lang : NULL) # define OD_STRING(odata) ((odata) ? (odata)->_str : NULL) typedef struct order_data { const char *_str; int _refcount; - const struct locale *lang; } order_data; #include @@ -115,7 +113,7 @@ keyword_t getkeyword(const order * ord) * This is the inverse function to the parse_order command. Note that * keywords are expanded to their full length. */ -char* get_command(const order *ord, char *sbuffer, size_t size) { +char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) { char *bufp = sbuffer; order_data *od; const char * text; @@ -144,7 +142,6 @@ char* get_command(const order *ord, char *sbuffer, size_t size) { od = load_data(ord->id); text = OD_STRING(od); if (kwd != NOKEYWORD) { - const struct locale *lang = OD_LOCALE(od); if (size > 0) { const char *str = (const char *)LOC(lang, keyword(kwd)); assert(str); @@ -217,13 +214,12 @@ void free_orders(order ** olist) } } -static char *mkdata(order_data **pdata, size_t len, const struct locale *lang, const char *str) +static char *mkdata(order_data **pdata, size_t len, const char *str) { order_data *data; char *result; data = malloc(sizeof(order_data) + len + 1); result = (char *)(data + 1); - data->lang = lang; data->_refcount = 0; data->_str = (len > 0) ? result : NULL; if (str) strcpy(result, str); @@ -231,7 +227,7 @@ static char *mkdata(order_data **pdata, size_t len, const struct locale *lang, c return result; } -static order_data *create_data(keyword_t kwd, const char *sptr, const struct locale *lang) +static order_data *create_data(keyword_t kwd, const char *sptr) { const char *s = sptr; order_data *data; @@ -241,17 +237,17 @@ static order_data *create_data(keyword_t kwd, const char *sptr, const struct loc /* orders with no parameter, only one order_data per order required */ if (kwd != NOKEYWORD && *sptr == 0) { - mkdata(&data, 0, lang, NULL); + mkdata(&data, 0, NULL); data->_refcount = 1; return data; } - mkdata(&data, s ? strlen(s) : 0, lang, s); + mkdata(&data, s ? strlen(s) : 0, s); data->_refcount = 1; return data; } static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, - bool noerror, const struct locale *lang) + bool noerror) { order_data *od; @@ -279,7 +275,7 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p while (isspace(*(unsigned char *)sptr)) ++sptr; - od = create_data(kwd, sptr, lang); + od = create_data(kwd, sptr); ord->id = add_data(od); release_data(od); @@ -341,7 +337,7 @@ order *create_order(keyword_t kwd, const struct locale * lang, zBuffer[0] = 0; } ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, kwd, zBuffer, false, false, lang); + return create_order_i(ord, kwd, zBuffer, false, false); } order *parse_order(const char *s, const struct locale * lang) @@ -373,7 +369,7 @@ order *parse_order(const char *s, const struct locale * lang) } if (kwd != NOKEYWORD) { order *ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, kwd, sptr, persistent, noerror, lang); + return create_order_i(ord, kwd, sptr, persistent, noerror); } } return NULL; @@ -529,7 +525,7 @@ bool is_silent(const order * ord) return (ord->command & CMD_QUIET) != 0; } -char *write_order(const order * ord, char *buffer, size_t size) +char *write_order(const order * ord, const struct locale *lang, char *buffer, size_t size) { if (ord == 0) { buffer[0] = 0; @@ -544,7 +540,7 @@ char *write_order(const order * ord, char *buffer, size_t size) release_data(od); } else { - get_command(ord, buffer, size); + get_command(ord, lang, buffer, size); } } return buffer; diff --git a/src/kernel/order.h b/src/kernel/order.h index f26174ee4..916055379 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -21,6 +21,8 @@ extern "C" { #endif + struct locale; + /* Encapsulation of an order * * This structure contains one order given by a unit. These used to be @@ -57,14 +59,16 @@ extern "C" { /* access functions for orders */ keyword_t getkeyword(const order * ord); void set_order(order ** destp, order * src); - char* get_command(const order *ord, char *buffer, size_t size); + char* get_command(const order *ord, const struct locale *lang, + char *buffer, size_t size); bool is_persistent(const order * ord); bool is_silent(const order * ord); bool is_exclusive(const order * ord); bool is_repeated(keyword_t kwd); bool is_long(keyword_t kwd); - char *write_order(const order * ord, char *buffer, size_t size); + char *write_order(const order * ord, const struct locale *lang, + char *buffer, size_t size); keyword_t init_order(const struct order *ord); void close_orders(void); diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c index e1656efc9..675cd90cf 100644 --- a/src/kernel/order.test.c +++ b/src/kernel/order.test.c @@ -20,7 +20,7 @@ static void test_create_order(CuTest *tc) { ord = create_order(K_MOVE, lang, "NORTH"); CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); - CuAssertStrEquals(tc, "move NORTH", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "move NORTH", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MOVE, init_order(ord)); CuAssertStrEquals(tc, "NORTH", getstrtoken()); @@ -40,7 +40,7 @@ static void test_parse_order(CuTest *tc) { CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MOVE, ord->command); CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); - CuAssertStrEquals(tc, "move NORTH", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "move NORTH", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MOVE, init_order(ord)); CuAssertStrEquals(tc, "NORTH", getstrtoken()); @@ -98,7 +98,7 @@ static void test_parse_make(CuTest *tc) { ord = parse_order("M hurrdurr", lang); CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MAKE, getkeyword(ord)); - CuAssertStrEquals(tc, "MAKE hurrdurr", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MAKE hurrdurr", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKE, init_order(ord)); CuAssertStrEquals(tc, "hurrdurr", getstrtoken()); @@ -121,7 +121,7 @@ static void test_parse_make_temp(CuTest *tc) { ord = parse_order("M T herp", lang); CuAssertPtrNotNull(tc, ord); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); - CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertStrEquals(tc, "herp", getstrtoken()); @@ -144,7 +144,7 @@ static void test_parse_maketemp(CuTest *tc) { ord = parse_order("MAKET herp", lang); CuAssertPtrNotNull(tc, ord); - CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, cmd, sizeof(cmd))); + CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); CuAssertStrEquals(tc, "herp", getstrtoken()); @@ -213,13 +213,13 @@ static void test_get_command(CuTest *tc) { test_setup(); lang = test_create_locale(); ord = create_order(K_MAKE, lang, "iron"); - CuAssertStrEquals(tc, "make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "make iron", get_command(ord, lang, buf, sizeof(buf))); ord->command |= CMD_QUIET; - CuAssertStrEquals(tc, "!make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "!make iron", get_command(ord, lang, buf, sizeof(buf))); ord->command |= CMD_PERSIST; - CuAssertStrEquals(tc, "!@make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "!@make iron", get_command(ord, lang, buf, sizeof(buf))); ord->command = K_MAKE | CMD_PERSIST; - CuAssertStrEquals(tc, "@make iron", get_command(ord, buf, sizeof(buf))); + CuAssertStrEquals(tc, "@make iron", get_command(ord, lang, buf, sizeof(buf))); free_order(ord); test_cleanup(); } diff --git a/src/kernel/save.c b/src/kernel/save.c index aa09a27be..133472d4f 100644 --- a/src/kernel/save.c +++ b/src/kernel/save.c @@ -379,7 +379,7 @@ static void writeorder(gamedata *data, const struct order *ord, const struct locale *lang) { char obuf[1024]; - write_order(ord, obuf, sizeof(obuf)); + write_order(ord, lang, obuf, sizeof(obuf)); if (obuf[0]) WRITE_STR(data->store, obuf); } diff --git a/src/keyword.test.c b/src/keyword.test.c index 75c31904a..68858367a 100644 --- a/src/keyword.test.c +++ b/src/keyword.test.c @@ -36,7 +36,7 @@ static void test_infinitive(CuTest *tc) { CuAssertIntEquals(tc, K_STUDY, get_keyword("LERNEN", lang)); ord = create_order(K_STUDY, lang, ""); - CuAssertStrEquals(tc, "LERNE", get_command(ord, buffer, sizeof(buffer))); + CuAssertStrEquals(tc, "LERNE", get_command(ord, lang, buffer, sizeof(buffer))); free_order(ord); test_cleanup(); } diff --git a/src/laws.c b/src/laws.c index f60af7f15..7da40f83f 100644 --- a/src/laws.c +++ b/src/laws.c @@ -968,7 +968,7 @@ int quit_cmd(unit * u, struct order *ord) } else { char buffer[64]; - write_order(ord, buffer, sizeof(buffer)); + write_order(ord, f->locale, buffer, sizeof(buffer)); cmistake(u, ord, 86, MSG_EVENT); log_warning("QUIT with illegal password for faction %s: %s\n", itoa36(f->no), buffer); } diff --git a/src/monsters.test.c b/src/monsters.test.c index 51f122b87..e43ed65cc 100644 --- a/src/monsters.test.c +++ b/src/monsters.test.c @@ -31,12 +31,12 @@ extern void plan_monsters(struct faction *f); extern int monster_attacks(unit * monster, bool rich_only); -static order *find_order(const char *expected, const unit *unit) +static order *find_order(const char *expected, const unit *u) { char cmd[32]; order *ord; - for (ord = unit->orders; ord; ord = ord->next) { - if (strcmp(expected, get_command(ord, cmd, sizeof(cmd))) == 0) { + for (ord = u->orders; ord; ord = ord->next) { + if (strcmp(expected, get_command(ord, u->faction->locale, cmd, sizeof(cmd))) == 0) { return ord; } } diff --git a/src/report.c b/src/report.c index c23a9466c..68e75661e 100644 --- a/src/report.c +++ b/src/report.c @@ -1389,7 +1389,8 @@ static int report_template(const char *filename, report_context * ctx, const char *bom) { const resource_type *rsilver = get_resourcetype(R_SILVER); - faction *f = ctx->f; + const faction *f = ctx->f; + const struct locale *lang = f->locale; region *r; FILE *F = fopen(filename, "w"); stream strm = { 0 }, *out = &strm; @@ -1408,11 +1409,11 @@ report_template(const char *filename, report_context * ctx, const char *bom) } newline(out); - rps_nowrap(out, LOC(f->locale, "nr_template")); + rps_nowrap(out, LOC(lang, "nr_template")); newline(out); newline(out); - sprintf(buf, "%s %s \"password\"", LOC(f->locale, parameters[P_FACTION]), itoa36(f->no)); + sprintf(buf, "%s %s \"password\"", LOC(lang, parameters[P_FACTION]), itoa36(f->no)); rps_nowrap(out, buf); newline(out); newline(out); @@ -1439,12 +1440,12 @@ report_template(const char *filename, report_context * ctx, const char *bom) adjust_coordinates(f, &nx, &ny, pl); newline(out); if (pl && pl->id != 0) { - sprintf(buf, "%s %d,%d,%d ; %s", LOC(f->locale, - parameters[P_REGION]), nx, ny, pl->id, rname(r, f->locale)); + sprintf(buf, "%s %d,%d,%d ; %s", LOC(lang, + parameters[P_REGION]), nx, ny, pl->id, rname(r, lang)); } else { - sprintf(buf, "%s %d,%d ; %s", LOC(f->locale, parameters[P_REGION]), - nx, ny, rname(r, f->locale)); + sprintf(buf, "%s %d,%d ; %s", LOC(lang, parameters[P_REGION]), + nx, ny, rname(r, lang)); } rps_nowrap(out, buf); newline(out); @@ -1505,7 +1506,7 @@ report_template(const char *filename, report_context * ctx, const char *bom) for (ord = u->old_orders; ord; ord = ord->next) { /* this new order will replace the old defaults */ strcpy(buf, " "); - write_order(ord, buf + 2, sizeof(buf) - 2); + write_order(ord, lang, buf + 2, sizeof(buf) - 2); rps_nowrap(out, buf); newline(out); } @@ -1515,7 +1516,7 @@ report_template(const char *filename, report_context * ctx, const char *bom) continue; /* unit has defaults */ if (is_persistent(ord)) { strcpy(buf, " "); - write_order(ord, buf + 2, sizeof(buf) - 2); + write_order(ord, lang, buf + 2, sizeof(buf) - 2); rps_nowrap(out, buf); newline(out); } @@ -1527,7 +1528,7 @@ report_template(const char *filename, report_context * ctx, const char *bom) } } newline(out); - strlcpy(buf, LOC(f->locale, parameters[P_NEXT]), sizeof(buf)); + strlcpy(buf, LOC(lang, parameters[P_NEXT]), sizeof(buf)); rps_nowrap(out, buf); newline(out); fstream_done(&strm); diff --git a/src/reports.c b/src/reports.c index 1182f89c9..cbc534162 100644 --- a/src/reports.c +++ b/src/reports.c @@ -266,14 +266,14 @@ report_item(const unit * owner, const item * i, const faction * viewer, } #define ORDERS_IN_NR 1 -static size_t buforder(char *buffer, size_t size, const order * ord, int mode) +static size_t buforder(char *buffer, size_t size, const order * ord, const struct locale *lang, int mode) { char *bufp = buffer; bufp = STRLCPY(bufp, ", \"", size); if (mode < ORDERS_IN_NR) { char cmd[ORDERSIZE]; - get_command(ord, cmd, sizeof(cmd)); + get_command(ord, lang, cmd, sizeof(cmd)); bufp = STRLCPY(bufp, cmd, size); } else { @@ -498,6 +498,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, char *bufp = buf; int result = 0; item results[MAX_INVENTORY]; + const struct locale *lang = f->locale; assert(f); bufp = STRLCPY(bufp, unitname(u), size); @@ -514,7 +515,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } if (getarnt) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size); + bufp = STRLCPY(bufp, LOC(lang, "anonymous"), size); } else if (u->attribs) { faction *otherf = get_otherfaction(u); @@ -527,7 +528,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, else { if (getarnt) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "anonymous"), size); + bufp = STRLCPY(bufp, LOC(lang, "anonymous"), size); } else { if (u->attribs && alliedunit(u, f, HELP_FSTEALTH)) { @@ -562,7 +563,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, bufp = STRLCPY(bufp, pzTmp, size); if (u->faction == f && fval(u_race(u), RCF_SHAPESHIFTANY)) { bufp = STRLCPY(bufp, " (", size); - bufp = STRLCPY(bufp, racename(f->locale, u, u_race(u)), size); + bufp = STRLCPY(bufp, racename(lang, u, u_race(u)), size); if (size > 1) { strcpy(bufp++, ")"); --size; @@ -571,10 +572,10 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } else { const race *irace = u_irace(u); - bufp = STRLCPY(bufp, racename(f->locale, u, irace), size); + bufp = STRLCPY(bufp, racename(lang, u, irace), size); if (u->faction == f && irace != u_race(u)) { bufp = STRLCPY(bufp, " (", size); - bufp = STRLCPY(bufp, racename(f->locale, u, u_race(u)), size); + bufp = STRLCPY(bufp, racename(lang, u, u_race(u)), size); if (size > 1) { strcpy(bufp++, ")"); --size; @@ -584,15 +585,15 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (fval(u, UFL_HERO) && (u->faction == f || omniscient(f))) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "hero"), size); + bufp = STRLCPY(bufp, LOC(lang, "hero"), size); } /* status */ if (u->number && (u->faction == f || isbattle)) { const char *c = hp_status(u); - c = c ? LOC(f->locale, c) : 0; + c = c ? LOC(lang, c) : 0; bufp = STRLCPY(bufp, ", ", size); - bufp += report_status(u, f->locale, bufp, size); + bufp += report_status(u, lang, bufp, size); if (c || fval(u, UFL_HUNGER)) { bufp = STRLCPY(bufp, " (", size); if (c) { @@ -602,7 +603,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (c) { bufp = STRLCPY(bufp, ", ", size); } - bufp = STRLCPY(bufp, LOC(f->locale, "unit_hungers"), size); + bufp = STRLCPY(bufp, LOC(lang, "unit_hungers"), size); } if (size > 1) { strcpy(bufp++, ")"); @@ -612,7 +613,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } if (is_guard(u)) { bufp = STRLCPY(bufp, ", ", size); - bufp = STRLCPY(bufp, LOC(f->locale, "unit_guards"), size); + bufp = STRLCPY(bufp, LOC(lang, "unit_guards"), size); } if ((b = usiege(u)) != NULL) { @@ -624,7 +625,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (u->faction == f) { skill *sv; for (sv = u->skills; sv != u->skills + u->skill_size; ++sv) { - size_t bytes = spskill(bufp, size, f->locale, u, sv, &dh, 1); + size_t bytes = spskill(bufp, size, lang, u, sv, &dh, 1); assert(bytes <= INT_MAX); if (wrptr(&bufp, &size, (int)bytes) != 0) WARN_STATIC_BUFFER(); @@ -651,7 +652,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, bufp = STRLCPY(bufp, ", ", size); if (!dh) { - result = snprintf(bufp, size, "%s: ", LOC(f->locale, "nr_inventory")); + result = snprintf(bufp, size, "%s: ", LOC(lang, "nr_inventory")); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); dh = 1; @@ -681,7 +682,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, if (sbe->level <= maxlevel) { int result = 0; if (!header) { - result = snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_spells")); + result = snprintf(bufp, size, ", %s: ", LOC(lang, "nr_spells")); header = 1; } else { @@ -691,7 +692,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, WARN_STATIC_BUFFER(); } /* TODO: no need to deref the spellref here (spref->name is good) */ - bufp = STRLCPY(bufp, spell_name(sbe->sp, f->locale), size); + bufp = STRLCPY(bufp, spell_name(sbe->sp, lang), size); } } @@ -701,7 +702,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } if (i != MAXCOMBATSPELLS) { int result = - snprintf(bufp, size, ", %s: ", LOC(f->locale, "nr_combatspells")); + snprintf(bufp, size, ", %s: ", LOC(lang, "nr_combatspells")); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); @@ -717,7 +718,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, sp = get_combatspell(u, i); if (sp) { int sl = get_combatspelllevel(u, i); - bufp = STRLCPY(bufp, spell_name(sp, u->faction->locale), size); + bufp = STRLCPY(bufp, spell_name(sp, lang), size); if (sl > 0) { result = snprintf(bufp, size, " (%d)", sl); if (wrptr(&bufp, &size, result) != 0) @@ -725,7 +726,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } } else { - bufp = STRLCPY(bufp, LOC(f->locale, "nr_nospells"), size); + bufp = STRLCPY(bufp, LOC(lang, "nr_nospells"), size); } } } @@ -737,7 +738,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, keyword_t kwd = getkeyword(ord); if (is_repeated(kwd)) { if (printed < ORDERS_IN_NR) { - int result = (int)buforder(bufp, size, ord, printed++); + int result = (int)buforder(bufp, size, ord, u->faction->locale, printed++); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); } @@ -750,7 +751,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, keyword_t kwd = getkeyword(ord); if (is_repeated(kwd)) { if (printed < ORDERS_IN_NR) { - int result = (int)buforder(bufp, size, ord, printed++); + int result = (int)buforder(bufp, size, ord, lang, printed++); if (wrptr(&bufp, &size, result) != 0) WARN_STATIC_BUFFER(); } @@ -762,7 +763,7 @@ bufunit(const faction * f, const unit * u, unsigned int indent, seen_mode mode, } i = 0; - str = u_description(u, f->locale); + str = u_description(u, lang); if (str) { bufp = STRLCPY(bufp, "; ", size); bufp = STRLCPY(bufp, str, size); @@ -1979,13 +1980,15 @@ static void eval_race(struct opstack **stack, const void *userdata) static void eval_order(struct opstack **stack, const void *userdata) { /* order -> string */ + const faction *f = (const faction *)userdata; const struct order *ord = (const struct order *)opop(stack).v; char buf[4096]; size_t len; variant var; + const struct locale *lang = f ? f->locale : default_locale; UNUSED_ARG(userdata); - write_order(ord, buf, sizeof(buf)); + write_order(ord, lang, buf, sizeof(buf)); len = strlen(buf); var.v = strcpy(balloc(len + 1), buf); opush(stack, var); @@ -1993,8 +1996,8 @@ static void eval_order(struct opstack **stack, const void *userdata) static void eval_resources(struct opstack **stack, const void *userdata) { /* order -> string */ - const faction *report = (const faction *)userdata; - const struct locale *lang = report ? report->locale : default_locale; + const faction *f = (const faction *)userdata; + const struct locale *lang = f ? f->locale : default_locale; const struct resource *res = (const struct resource *)opop(stack).v; char buf[1024]; /* but we only use about half of this */ size_t size = sizeof(buf) - 1; From 710fcd0a08866ca94d2b3f5b2f75fe2ffb7a765c Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 8 Oct 2017 18:41:59 +0200 Subject: [PATCH 4/9] do not use a list to store several million entries. --- clibs | 2 +- src/kernel/order.c | 46 +++++++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/clibs b/clibs index 147584ad7..7eb6713ff 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit 147584ad70b220abf6a4e97ca76e785729b9ac32 +Subproject commit 7eb6713ff73d29e67d2bc87fb037a9c738c2c512 diff --git a/src/kernel/order.c b/src/kernel/order.c index 154b3f451..36211ccc4 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -38,24 +38,39 @@ typedef struct order_data { int _refcount; } order_data; -#include +#include -static selist * orders; +static critbit_tree cb_orders = { 0 }; +static int auto_id = 0; + +struct cb_entry { + int id; + order_data *data; +}; order_data *load_data(int id) { + void * match; + if (id > 0) { - order_data * od = (order_data *)selist_get(orders, id - 1); - ++od->_refcount; - return od; + if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) { + struct cb_entry *ent = (struct cb_entry *)match; + order_data * od = ent->data; + ++od->_refcount; + return od; + } } return NULL; } -int add_data(order_data *od) { +int save_data(order_data *od) { if (od->_str) { + struct cb_entry ent; + ++od->_refcount; - selist_push(&orders, od); - return selist_length(orders); + ent.id = ++auto_id; + ent.data = od; + cb_insert(&cb_orders, &ent, sizeof(ent)); + return ent.id; } return 0; } @@ -69,18 +84,19 @@ static void release_data(order_data * data) } } -void free_data_cb(void *entry) { - order_data *od = (order_data *)entry; +int free_data_cb(const void *match, const void *key, size_t keylen, void *udata) { + struct cb_entry * ent = (struct cb_entry *)match; + order_data *od = ent->data; if (od->_refcount > 1) { - log_error("refcount=%d for order %s", od->_refcount, od->_str); + log_error("refcount=%d for order %d, %s", od->_refcount, ent->id, od->_str); } release_data(od); + return 0; } void free_data(void) { - selist_foreach(orders, free_data_cb); - selist_free(orders); - orders = NULL; + cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL); + cb_clear(&cb_orders); } void replace_order(order ** dlist, order * orig, const order * src) @@ -276,7 +292,7 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p while (isspace(*(unsigned char *)sptr)) ++sptr; od = create_data(kwd, sptr); - ord->id = add_data(od); + ord->id = save_data(od); release_data(od); return ord; From 905873e48bd5d181bb73694764c408fd35b2ce69 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 8 Oct 2017 18:49:14 +0200 Subject: [PATCH 5/9] fix gcc build --- src/kernel/order.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kernel/order.c b/src/kernel/order.c index 36211ccc4..e11b652ae 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -580,7 +580,7 @@ keyword_t init_order(const struct order *ord) } else { if (parser_od) { - // TODO: warning + /* TODO: warning */ release_data(parser_od); } parser_od = load_data(ord->id); From f62c0866e395d5c5eb165e749d2a4e57b35b337d Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 9 Oct 2017 18:25:57 +0200 Subject: [PATCH 6/9] WIP: optimized study orders, failing 15 study tests --- src/kernel/order.c | 76 ++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 39 deletions(-) diff --git a/src/kernel/order.c b/src/kernel/order.c index e11b652ae..4124651c9 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -62,10 +62,9 @@ order_data *load_data(int id) { return NULL; } -int save_data(order_data *od) { +int save_data(keyword_t kwd, order_data *od) { if (od->_str) { struct cb_entry ent; - ++od->_refcount; ent.id = ++auto_id; ent.data = od; @@ -131,7 +130,7 @@ keyword_t getkeyword(const order * ord) */ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, size_t size) { char *bufp = sbuffer; - order_data *od; + order_data *od = NULL; const char * text; keyword_t kwd = ORD_KEYWORD(ord); int bytes; @@ -155,8 +154,14 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si } } - od = load_data(ord->id); - text = OD_STRING(od); + if (ord->id < 0) { + skill_t sk = (skill_t)(100-ord->id); + assert(kwd == K_STUDY && sk != SK_MAGIC); + text = skillname(sk, lang); + } else { + od = load_data(ord->id); + text = OD_STRING(od); + } if (kwd != NOKEYWORD) { if (size > 0) { const char *str = (const char *)LOC(lang, keyword(kwd)); @@ -187,7 +192,9 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si } } } - release_data(od); + if (od) { + release_data(od); + } if (size > 0) *bufp = 0; return sbuffer; } @@ -236,54 +243,48 @@ static char *mkdata(order_data **pdata, size_t len, const char *str) char *result; data = malloc(sizeof(order_data) + len + 1); result = (char *)(data + 1); - data->_refcount = 0; + data->_refcount = 1; data->_str = (len > 0) ? result : NULL; if (str) strcpy(result, str); if (pdata) *pdata = data; return result; } -static order_data *create_data(keyword_t kwd, const char *sptr) +static int create_data(keyword_t kwd, const char *s, + const struct locale *lang) { - const char *s = sptr; order_data *data; + int id; - if (kwd != NOKEYWORD) - s = (*sptr) ? sptr : NULL; + assert(kwd!=NOKEYWORD); - /* orders with no parameter, only one order_data per order required */ - if (kwd != NOKEYWORD && *sptr == 0) { - mkdata(&data, 0, NULL); - data->_refcount = 1; - return data; + if (!s || *s == 0) { + return 0; } + if (kwd==K_STUDY) { + const char * sptr = s; + skill_t sk = get_skill(parse_token_depr(&sptr), lang); + if (sk != SK_MAGIC && sk != NOSKILL) { + return ((int)sk)-100; + } + } + /* TODO: between mkdata and release_data, this object is very + * short-lived. */ mkdata(&data, s ? strlen(s) : 0, s); - data->_refcount = 1; - return data; + id = save_data(kwd, data); + release_data(data); + return id; } -static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool persistent, - bool noerror) +static order *create_order_i(order *ord, const struct locale *lang, + keyword_t kwd, const char *sptr, bool persistent, bool noerror) { - order_data *od; - assert(ord); if (kwd == NOKEYWORD || keyword_disabled(kwd)) { log_error("trying to create an order for disabled keyword %s.", keyword(kwd)); return NULL; } - /* if this is just nonsense, then we skip it. */ - if (lomem) { - switch (kwd) { - case K_KOMMENTAR: - case NOKEYWORD: - return NULL; - default: - break; - } - } - ord->command = (int)kwd; if (persistent) ord->command |= CMD_PERSIST; if (noerror) ord->command |= CMD_QUIET; @@ -291,10 +292,7 @@ static order *create_order_i(order *ord, keyword_t kwd, const char *sptr, bool p while (isspace(*(unsigned char *)sptr)) ++sptr; - od = create_data(kwd, sptr); - ord->id = save_data(od); - release_data(od); - + ord->id = create_data(kwd, sptr, lang); return ord; } @@ -353,7 +351,7 @@ order *create_order(keyword_t kwd, const struct locale * lang, zBuffer[0] = 0; } ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, kwd, zBuffer, false, false); + return create_order_i(ord, lang, kwd, zBuffer, false, false); } order *parse_order(const char *s, const struct locale * lang) @@ -385,7 +383,7 @@ order *parse_order(const char *s, const struct locale * lang) } if (kwd != NOKEYWORD) { order *ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, kwd, sptr, persistent, noerror); + return create_order_i(ord, lang, kwd, sptr, persistent, noerror); } } return NULL; From fc80f2bd8c5c6e05df7270ff8bab6a6445c3192e Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Mon, 9 Oct 2017 20:33:47 +0200 Subject: [PATCH 7/9] hackish optimization for STUDY orders --- src/battle.c | 2 +- src/bind_order.c | 2 +- src/economy.c | 24 +++++++++--------- src/give.c | 2 +- src/items.c | 2 +- src/kernel/alliance.c | 10 ++++---- src/kernel/build.c | 2 +- src/kernel/command.c | 2 +- src/kernel/config.test.c | 24 +++++++++--------- src/kernel/order.c | 31 ++++++++++++++++++----- src/kernel/order.h | 3 ++- src/kernel/order.test.c | 52 +++++++++++++++++++++++++++++++++----- src/laws.c | 54 ++++++++++++++++++++-------------------- src/magic.c | 2 +- src/move.c | 22 ++++++++-------- src/move.test.c | 2 +- src/piracy.c | 4 +-- src/renumber.c | 2 +- src/spy.c | 8 +++--- src/study.c | 18 +++++++------- 20 files changed, 164 insertions(+), 104 deletions(-) diff --git a/src/battle.c b/src/battle.c index ad1c0f8da..8139456cc 100644 --- a/src/battle.c +++ b/src/battle.c @@ -3841,7 +3841,7 @@ static bool start_battle(region * r, battle ** bp) /* Ende Fehlerbehandlung Angreifer */ - init_order(ord); + init_order_depr(ord); /* attackierte Einheit ermitteln */ getunit(r, u->faction, &u2); diff --git a/src/bind_order.c b/src/bind_order.c index 84399deec..cfbe51d16 100644 --- a/src/bind_order.c +++ b/src/bind_order.c @@ -13,7 +13,7 @@ static int tolua_order_get_token(lua_State *L) { order *ord = (order *)tolua_tousertype(L, 1, 0); int n = (int)tolua_tonumber(L, 2, 0); const char * str = 0; - init_order(ord); + init_order_depr(ord); while (n-->0) { str = getstrtoken(); if (!str) { diff --git a/src/economy.c b/src/economy.c index 7c4b0efd3..1de4f7e4e 100644 --- a/src/economy.c +++ b/src/economy.c @@ -441,7 +441,7 @@ static void recruit(unit * u, struct order *ord, request ** recruitorders) const char *str; int n; - init_order(ord); + init_order_depr(ord); n = getint(); if (n <= 0) { syntax_error(u, ord); @@ -598,7 +598,7 @@ int give_control_cmd(unit * u, order * ord) unit *u2; const char *s; - init_order(ord); + init_order_depr(ord); getunit(r, u->faction, &u2); s = gettoken(token, sizeof(token)); @@ -657,7 +657,7 @@ static int forget_cmd(unit * u, order * ord) return 0; } - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); sk = get_skill(s, u->faction->locale); @@ -1303,7 +1303,7 @@ int make_cmd(unit * u, struct order *ord) char ibuf[16]; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_MAKE); s = gettoken(token, sizeof(token)); @@ -1582,7 +1582,7 @@ static void buy(unit * u, request ** buyorders, struct order *ord) /* Im Augenblick kann man nur 1 Produkt kaufen. expandbuying ist aber * schon daf�r ausger�stet, mehrere Produkte zu kaufen. */ - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_BUY); n = getint(); if (n <= 0) { @@ -1896,7 +1896,7 @@ static bool sell(unit * u, request ** sellorders, struct order *ord) /* sellorders sind KEIN array, weil f�r alle items DIE SELBE resource * (das geld der region) aufgebraucht wird. */ - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_SELL); s = gettoken(token, sizeof(token)); @@ -2287,7 +2287,7 @@ static void breed_cmd(unit * u, struct order *ord) } /* z�chte [] */ - (void)init_order(ord); + (void)init_order_depr(ord); s = gettoken(token, sizeof(token)); m = s ? atoip(s) : 0; @@ -2354,7 +2354,7 @@ static void research_cmd(unit * u, struct order *ord) region *r = u->region; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_RESEARCH); if (effskill(u, SK_HERBALISM, 0) < 7) { @@ -2430,7 +2430,7 @@ static void steal_cmd(unit * u, struct order *ord, request ** stealorders) message * msg; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_STEAL); assert(skill_enabled(SK_PERCEPTION) && skill_enabled(SK_STEALTH)); @@ -2568,7 +2568,7 @@ void entertain_cmd(unit * u, struct order *ord) static int entertainperlevel = 0; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_ENTERTAIN); if (!entertainbase) { const char *str = config_get("entertain.base"); @@ -2778,7 +2778,7 @@ void tax_cmd(unit * u, struct order *ord, request ** taxorders) taxperlevel = config_get_int("taxing.perlevel", 0); } - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_TAX); if (!humanoidrace(u_race(u)) && !is_monsters(u->faction)) { @@ -2847,7 +2847,7 @@ void loot_cmd(unit * u, struct order *ord, request ** lootorders) request *o; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_LOOT); if (config_get_int("rules.enable_loot", 0) == 0 && !is_monsters(u->faction)) { diff --git a/src/give.c b/src/give.c index da75e56c5..84e2765a6 100644 --- a/src/give.c +++ b/src/give.c @@ -634,7 +634,7 @@ void give_cmd(unit * u, order * ord) message *msg; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_GIVE); err = getunit(r, u->faction, &u2); s = gettoken(token, sizeof(token)); diff --git a/src/items.c b/src/items.c index 8a6328d91..fe59ffa15 100644 --- a/src/items.c +++ b/src/items.c @@ -47,7 +47,7 @@ static int use_studypotion(struct unit *u, const struct item_type *itype, int amount, struct order *ord) { - if (u->thisorder && init_order(u->thisorder) == K_STUDY) { + if (u->thisorder && init_order(u->thisorder, u->faction->locale) == K_STUDY) { char token[128]; skill_t sk = NOSKILL; skill *sv = 0; diff --git a/src/kernel/alliance.c b/src/kernel/alliance.c index 910670d10..e4fd7c3b5 100644 --- a/src/kernel/alliance.c +++ b/src/kernel/alliance.c @@ -171,7 +171,7 @@ static void perform_kick(void) if (al && alliance_get_leader(al) == ta->u->faction) { faction *f; - init_order(ta->ord); + init_order_depr(ta->ord); skip_token(); f = getfaction(); if (f && f_get_alliance(f) == al) { @@ -192,7 +192,7 @@ static void perform_new(void) int id; faction *f = ta->u->faction; - init_order(ta->ord); + init_order_depr(ta->ord); skip_token(); id = getid(); @@ -227,7 +227,7 @@ static void perform_transfer(void) if (al && alliance_get_leader(al) == ta->u->faction) { faction *f; - init_order(ta->ord); + init_order_depr(ta->ord); skip_token(); f = getfaction(); if (f && f_get_alliance(f) == al) { @@ -264,7 +264,7 @@ static void perform_join(void) faction *fj = ta->u->faction; int aid; - init_order(ta->ord); + init_order_depr(ta->ord); skip_token(); aid = getid(); if (aid) { @@ -276,7 +276,7 @@ static void perform_join(void) faction *fi = ti->u->faction; if (fi && f_get_alliance(fi) == al) { int fid; - init_order(ti->ord); + init_order_depr(ti->ord); skip_token(); fid = getid(); if (fid == fj->no) { diff --git a/src/kernel/build.c b/src/kernel/build.c index 1f4e143df..2746fa406 100644 --- a/src/kernel/build.c +++ b/src/kernel/build.c @@ -161,7 +161,7 @@ int destroy_cmd(unit * u, struct order *ord) return 52; } - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); if (s && *s) { diff --git a/src/kernel/command.c b/src/kernel/command.c index a6a036fe9..8b2a7e5e8 100644 --- a/src/kernel/command.c +++ b/src/kernel/command.c @@ -105,7 +105,7 @@ static int do_command_i(const struct tnode *keys, struct unit *u, struct order * void do_command(const struct tnode *keys, struct unit *u, struct order *ord) { - init_order(ord); + init_order_depr(ord); if (do_command_i(keys, u, ord) != E_TOK_SUCCESS) { char cmd[ORDERSIZE]; get_command(ord, u->faction->locale, cmd, sizeof(cmd)); diff --git a/src/kernel/config.test.c b/src/kernel/config.test.c index ac35b85a3..610c2d4b3 100644 --- a/src/kernel/config.test.c +++ b/src/kernel/config.test.c @@ -32,28 +32,28 @@ static void test_read_unitid(CuTest *tc) { a->data.i = atoi36("42"); /* this unit is also TEMP 42 */ ord = create_order(K_GIVE, lang, "TEMP 42"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, u->no, read_unitid(u->faction, u->region)); free_order(ord); ord = create_order(K_GIVE, lang, "8"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, 8, read_unitid(u->faction, u->region)); free_order(ord); ord = create_order(K_GIVE, lang, ""); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region)); free_order(ord); ord = create_order(K_GIVE, lang, "TEMP"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region)); free_order(ord); /* bug https://bugs.eressea.de/view.php?id=1685 */ ord = create_order(K_GIVE, lang, "##"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, -1, read_unitid(u->faction, u->region)); free_order(ord); @@ -78,42 +78,42 @@ static void test_getunit(CuTest *tc) { r = test_create_region(1, 0, t_plain); ord = create_order(K_GIVE, lang, itoa36(u->no)); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2)); CuAssertPtrEquals(tc, u, u2); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(r, u->faction, &u2)); CuAssertPtrEquals(tc, NULL, u2); free_order(ord); ord = create_order(K_GIVE, lang, itoa36(u->no + 1)); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); CuAssertPtrEquals(tc, NULL, u2); free_order(ord); ord = create_order(K_GIVE, lang, "0"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_PEASANTS, getunit(u->region, u->faction, &u2)); CuAssertPtrEquals(tc, NULL, u2); free_order(ord); /* bug https://bugs.eressea.de/view.php?id=1685 */ ord = create_order(K_GIVE, lang, "TEMP ##"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); CuAssertPtrEquals(tc, NULL, u2); free_order(ord); /* bug https://bugs.eressea.de/view.php?id=1685 */ ord = create_order(K_GIVE, lang, "##"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_NOTFOUND, getunit(u->region, u->faction, &u2)); CuAssertPtrEquals(tc, NULL, u2); free_order(ord); ord = create_order(K_GIVE, lang, "TEMP 42"); - init_order(ord); + init_order_depr(ord); CuAssertIntEquals(tc, GET_UNIT, getunit(u->region, u->faction, &u2)); CuAssertPtrEquals(tc, u, u2); free_order(ord); diff --git a/src/kernel/order.c b/src/kernel/order.c index 4124651c9..7cdb2961d 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -155,7 +155,7 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si } if (ord->id < 0) { - skill_t sk = (skill_t)(100-ord->id); + skill_t sk = (skill_t)(100+ord->id); assert(kwd == K_STUDY && sk != SK_MAGIC); text = skillname(sk, lang); } else { @@ -569,7 +569,7 @@ void push_order(order ** ordp, order * ord) static order_data *parser_od; -keyword_t init_order(const struct order *ord) +keyword_t init_order(const struct order *ord, const struct locale *lang) { if (!ord) { release_data(parser_od); @@ -577,19 +577,38 @@ keyword_t init_order(const struct order *ord) return NOKEYWORD; } else { + keyword_t kwd = ORD_KEYWORD(ord); if (parser_od) { /* TODO: warning */ release_data(parser_od); + parser_od = NULL; } - parser_od = load_data(ord->id); - init_tokens_str(OD_STRING(parser_od)); - return ORD_KEYWORD(ord); + if (ord->id < 0) { + skill_t sk = (skill_t)(100 + ord->id); + assert(lang); + assert(kwd == K_STUDY); + init_tokens_str(skillname(sk, lang)); + } + else { + parser_od = load_data(ord->id); + init_tokens_str(OD_STRING(parser_od)); + } + return kwd; } } +keyword_t init_order_depr(const struct order *ord) +{ + if (ord) { + keyword_t kwd = ORD_KEYWORD(ord); + assert(kwd != K_STUDY); + } + return init_order(ord, NULL); +} + void close_orders(void) { if (parser_od) { - init_order(NULL); + (void)init_order(NULL, NULL); } free_data(); } diff --git a/src/kernel/order.h b/src/kernel/order.h index 916055379..0cbe4989c 100644 --- a/src/kernel/order.h +++ b/src/kernel/order.h @@ -69,7 +69,8 @@ extern "C" { char *write_order(const order * ord, const struct locale *lang, char *buffer, size_t size); - keyword_t init_order(const struct order *ord); + keyword_t init_order_depr(const struct order *ord); + keyword_t init_order(const struct order *ord, const struct locale *lang); void close_orders(void); diff --git a/src/kernel/order.test.c b/src/kernel/order.test.c index 675cd90cf..c892e7e54 100644 --- a/src/kernel/order.test.c +++ b/src/kernel/order.test.c @@ -2,6 +2,8 @@ #include #include "order.h" +#include + #include #include @@ -22,7 +24,7 @@ static void test_create_order(CuTest *tc) { CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertStrEquals(tc, "move NORTH", get_command(ord, lang, cmd, sizeof(cmd))); - CuAssertIntEquals(tc, K_MOVE, init_order(ord)); + CuAssertIntEquals(tc, K_MOVE, init_order_depr(ord)); CuAssertStrEquals(tc, "NORTH", getstrtoken()); free_order(ord); test_cleanup(); @@ -42,7 +44,7 @@ static void test_parse_order(CuTest *tc) { CuAssertIntEquals(tc, K_MOVE, getkeyword(ord)); CuAssertStrEquals(tc, "move NORTH", get_command(ord, lang, cmd, sizeof(cmd))); - CuAssertIntEquals(tc, K_MOVE, init_order(ord)); + CuAssertIntEquals(tc, K_MOVE, init_order_depr(ord)); CuAssertStrEquals(tc, "NORTH", getstrtoken()); free_order(ord); @@ -100,7 +102,7 @@ static void test_parse_make(CuTest *tc) { CuAssertIntEquals(tc, K_MAKE, getkeyword(ord)); CuAssertStrEquals(tc, "MAKE hurrdurr", get_command(ord, lang, cmd, sizeof(cmd))); - CuAssertIntEquals(tc, K_MAKE, init_order(ord)); + CuAssertIntEquals(tc, K_MAKE, init_order_depr(ord)); CuAssertStrEquals(tc, "hurrdurr", getstrtoken()); free_order(ord); test_cleanup(); @@ -123,7 +125,7 @@ static void test_parse_make_temp(CuTest *tc) { CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); - CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); + CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord)); CuAssertStrEquals(tc, "herp", getstrtoken()); free_order(ord); test_cleanup(); @@ -146,7 +148,7 @@ static void test_parse_maketemp(CuTest *tc) { CuAssertPtrNotNull(tc, ord); CuAssertStrEquals(tc, "MAKETEMP herp", get_command(ord, lang, cmd, sizeof(cmd))); CuAssertIntEquals(tc, K_MAKETEMP, getkeyword(ord)); - CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); + CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord)); CuAssertStrEquals(tc, "herp", getstrtoken()); free_order(ord); test_cleanup(); @@ -160,7 +162,7 @@ static void test_init_order(CuTest *tc) { lang = get_or_create_locale("en"); ord = create_order(K_MAKETEMP, lang, "hurr durr"); - CuAssertIntEquals(tc, K_MAKETEMP, init_order(ord)); + CuAssertIntEquals(tc, K_MAKETEMP, init_order_depr(ord)); CuAssertStrEquals(tc, "hurr", getstrtoken()); CuAssertStrEquals(tc, "durr", getstrtoken()); free_order(ord); @@ -292,10 +294,48 @@ static void test_is_silent(CuTest *tc) { test_cleanup(); } + +static void test_study_orders(CuTest *tc) { + order *ord; + struct locale *lang; + const char *s; + char token[16]; + + test_setup(); + lang = test_create_locale(); + + ord = create_order(K_STUDY, lang, skillname(SK_CROSSBOW, lang)); + CuAssertIntEquals(tc, K_STUDY, getkeyword(ord)); + CuAssertIntEquals(tc, K_STUDY, init_order(ord, lang)); + s = gettoken(token, sizeof(token)); + CuAssertStrEquals(tc, skillname(SK_CROSSBOW, lang), s); + CuAssertPtrEquals(tc, NULL, (void *)getstrtoken()); + free_order(ord); + + ord = create_order(K_STUDY, lang, skillname(SK_MAGIC, lang)); + CuAssertIntEquals(tc, K_STUDY, getkeyword(ord)); + CuAssertIntEquals(tc, K_STUDY, init_order(ord, lang)); + s = gettoken(token, sizeof(token)); + CuAssertStrEquals(tc, skillname(SK_MAGIC, lang), s); + CuAssertPtrEquals(tc, NULL, (void *)getstrtoken()); + free_order(ord); + + ord = create_order(K_STUDY, lang, "%s 100", skillname(SK_MAGIC, lang)); + CuAssertIntEquals(tc, K_STUDY, getkeyword(ord)); + CuAssertIntEquals(tc, K_STUDY, init_order(ord, lang)); + s = gettoken(token, sizeof(token)); + CuAssertStrEquals(tc, skillname(SK_MAGIC, lang), s); + CuAssertIntEquals(tc, 100, getint()); + free_order(ord); + + test_cleanup(); +} + CuSuite *get_order_suite(void) { CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_create_order); + SUITE_ADD_TEST(suite, test_study_orders); SUITE_ADD_TEST(suite, test_parse_order); SUITE_ADD_TEST(suite, test_parse_make); SUITE_ADD_TEST(suite, test_parse_make_temp); diff --git a/src/laws.c b/src/laws.c index 7da40f83f..dec35ef8c 100644 --- a/src/laws.c +++ b/src/laws.c @@ -910,7 +910,7 @@ int contact_cmd(unit * u, order * ord) unit *u2; int n; - init_order(ord); + init_order_depr(ord); n = read_unitid(u->faction, u->region); u2 = findunit(n); @@ -960,7 +960,7 @@ int quit_cmd(unit * u, struct order *ord) const char *passwd; keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_QUIT); passwd = gettoken(token, sizeof(token)); if (checkpasswd(f, (const char *)passwd)) { @@ -1146,7 +1146,7 @@ void do_enter(struct region *r, bool is_final_attempt) unit *ulast = NULL; const char * s; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); p = findparam_ex(s, u->faction->locale); id = getid(); @@ -1306,7 +1306,7 @@ int ally_cmd(unit * u, struct order *ord) int keyword, not_kw; const char *s; - init_order(ord); + init_order_depr(ord); f = getfaction(); if (f == NULL || is_monsters(f)) { @@ -1473,7 +1473,7 @@ int prefix_cmd(unit * u, struct order *ord) for (in = pnames; in->lang != lang; in = in->next); } - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); if (!s || !*s) { @@ -1517,7 +1517,7 @@ int display_cmd(unit * u, struct order *ord) const char *str; region *r = u->region; - init_order(ord); + init_order_depr(ord); str = gettoken(token, sizeof(token)); switch (findparam_ex(str, u->faction->locale)) { @@ -1690,7 +1690,7 @@ int name_cmd(struct unit *u, struct order *ord) bool foreign = false; const char *str; - init_order(ord); + init_order_depr(ord); str = gettoken(token, sizeof(token)); p = findparam_ex(str, u->faction->locale); @@ -1948,7 +1948,7 @@ int mail_cmd(unit * u, struct order *ord) const char *s; int n, cont; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); /* Falls kein Parameter, ist das eine Einheitsnummer; @@ -2105,7 +2105,7 @@ int banner_cmd(unit * u, struct order *ord) const char * s; free(u->faction->banner); - init_order(ord); + init_order_depr(ord); s = getstrtoken(); u->faction->banner = s ? strdup(s) : 0; add_message(&u->faction->msgs, msg_message("changebanner", "value", @@ -2118,7 +2118,7 @@ int email_cmd(unit * u, struct order *ord) { const char *s; - init_order(ord); + init_order_depr(ord); s = getstrtoken(); if (!s || !s[0]) { @@ -2144,7 +2144,7 @@ int password_cmd(unit * u, struct order *ord) const char *s; bool pwok = true; - init_order(ord); + init_order_depr(ord); s = gettoken(pwbuf, sizeof(pwbuf)); if (!s || !*s) { @@ -2177,7 +2177,7 @@ int send_cmd(unit * u, struct order *ord) const char *s; int option; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); option = findoption(s, u->faction->locale); @@ -2512,7 +2512,7 @@ int group_cmd(unit * u, struct order *ord) { keyword_t kwd; - kwd = init_order(ord); + kwd = init_order_depr(ord); assert(kwd == K_GROUP); join_group(u, getstrtoken()); return 0; @@ -2522,7 +2522,7 @@ int origin_cmd(unit * u, struct order *ord) { short px, py; - init_order(ord); + init_order_depr(ord); px = (short)getint(); py = (short)getint(); @@ -2534,7 +2534,7 @@ int origin_cmd(unit * u, struct order *ord) int guard_off_cmd(unit * u, struct order *ord) { assert(getkeyword(ord) == K_GUARD); - init_order(ord); + init_order_depr(ord); if (getparam(u->faction->locale) == P_NOT) { setguard(u, false); @@ -2548,7 +2548,7 @@ int reshow_cmd(unit * u, struct order *ord) const char *s; param_t p = NOPARAM; - init_order(ord); + init_order_depr(ord); s = gettoken(lbuf, sizeof(lbuf)); if (s && isparam(s, u->faction->locale, P_ANY)) { @@ -2565,7 +2565,7 @@ int status_cmd(unit * u, struct order *ord) char token[128]; const char *s; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); switch (findparam(s, u->faction->locale)) { case P_NOT: @@ -2613,7 +2613,7 @@ int combatspell_cmd(unit * u, struct order *ord) int level = 0; spell *sp = 0; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); /* KAMPFZAUBER [NICHT] löscht alle gesetzten Kampfzauber */ @@ -2658,7 +2658,7 @@ int guard_on_cmd(unit * u, struct order *ord) assert(u); assert(u->faction); - init_order(ord); + init_order_depr(ord); /* GUARD NOT is handled in goard_off_cmd earlier in the turn */ if (getparam(u->faction->locale) == P_NOT) { @@ -2739,7 +2739,7 @@ void restack_units(void) int id; unit *v; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); p = findparam(s, u->faction->locale); id = getid(); @@ -3049,7 +3049,7 @@ void maketemp_cmd(unit *u, order **olist) unit *u2; order **ordp, **oinsert; #ifndef NDEBUG - keyword_t kwd = init_order(makeord); + keyword_t kwd = init_order_depr(makeord); assert(kwd == K_MAKETEMP); #endif alias = getid(); @@ -3347,7 +3347,7 @@ void defaultorders(void) char lbuf[8192]; order *new_order = 0; const char *s; - init_order(ord); + init_order_depr(ord); s = gettoken(lbuf, sizeof(lbuf)); if (s) { new_order = parse_order(s, u->faction->locale); @@ -3463,7 +3463,7 @@ int use_cmd(unit * u, struct order *ord) int n, err = ENOITEM; const item_type *itype; - init_order(ord); + init_order_depr(ord); t = gettoken(token, sizeof(token)); if (!t) { @@ -3518,7 +3518,7 @@ int pay_cmd(unit * u, struct order *ord) param_t p; int id; - init_order(ord); + init_order_depr(ord); p = getparam(u->faction->locale); id = getid(); if (p == P_NOT) { @@ -3572,7 +3572,7 @@ static int reserve_i(unit * u, struct order *ord, int flags) const item_type *itype; const char *s; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); count = s ? atoip(s) : 0; para = findparam(s, u->faction->locale); @@ -3615,7 +3615,7 @@ int claim_cmd(unit * u, struct order *ord) int n = 1; const item_type *itype = 0; - init_order(ord); + init_order_depr(ord); t = gettoken(token, sizeof(token)); if (t) { @@ -3909,7 +3909,7 @@ int siege_cmd(unit * u, order * ord) resource_type *rt_catapultammo = NULL; resource_type *rt_catapult = NULL; - init_order(ord); + init_order_depr(ord); b = getbuilding(r); if (!b) { diff --git a/src/magic.c b/src/magic.c index f83e31a7d..c8f1b83c3 100644 --- a/src/magic.c +++ b/src/magic.c @@ -2514,7 +2514,7 @@ static castorder *cast_cmd(unit * u, order * ord) } level = effskill(u, SK_MAGIC, 0); - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); param = findparam(s, u->faction->locale); /* für Syntax ' STUFE x REGION y z ' */ diff --git a/src/move.c b/src/move.c index 3f899ac22..5a9e51b43 100644 --- a/src/move.c +++ b/src/move.c @@ -1067,7 +1067,7 @@ static void cycle_route(order * ord, unit * u, int gereist) return; tail[0] = '\0'; - init_order(ord); + init_order_depr(ord); neworder[0] = 0; for (cm = 0;; ++cm) { @@ -1144,7 +1144,7 @@ static bool transport(unit * ut, unit * u) for (ord = ut->orders; ord; ord = ord->next) { if (getkeyword(ord) == K_TRANSPORT) { unit *u2; - init_order(ord); + init_order_depr(ord); getunit(ut->region, ut->faction, &u2); if (u2 == u) { return true; @@ -1178,7 +1178,7 @@ static void init_transportation(void) && !fval(u, UFL_NOTMOVING) && !LongHunger(u)) { unit *ut = 0; - init_order(u->thisorder); + init_order_depr(u->thisorder); if (getunit(r, u->faction, &ut) != GET_UNIT) { ADDMSG(&u->faction->msgs, msg_feedback(u, u->thisorder, "feedback_unit_not_found", "")); @@ -1207,7 +1207,7 @@ static void init_transportation(void) for (ord = u->orders; ord; ord = ord->next) { if (getkeyword(ord) == K_TRANSPORT) { - init_order(ord); + init_order_depr(ord); for (;;) { unit *ut = 0; @@ -1218,7 +1218,7 @@ static void init_transportation(void) can_move(ut) && !fval(ut, UFL_NOTMOVING) && !LongHunger(ut)) { unit *u2; - init_order(ut->thisorder); + init_order_depr(ut->thisorder); getunit(r, ut->faction, &u2); if (u2 == u) { w += weight(ut); @@ -2062,7 +2062,7 @@ static const region_list *travel_i(unit * u, const region_list * route_begin, if (getkeyword(ord) != K_TRANSPORT) continue; - init_order(ord); + init_order_depr(ord); if (getunit(r, u->faction, &ut) == GET_UNIT) { if (getkeyword(ut->thisorder) == K_DRIVE) { if (ut->building && !can_leave(ut)) { @@ -2077,7 +2077,7 @@ static const region_list *travel_i(unit * u, const region_list * route_begin, if (!fval(ut, UFL_NOTMOVING) && !LongHunger(ut)) { unit *u2; - init_order(ut->thisorder); + init_order_depr(ut->thisorder); getunit(u->region, ut->faction, &u2); if (u2 == u) { const region_list *route_to = @@ -2363,7 +2363,7 @@ static void move_hunters(void) if (getkeyword(ord) == K_FOLLOW) { param_t p; - init_order(ord); + init_order_depr(ord); p = getparam(u->faction->locale); if (p != P_SHIP) { if (p != P_UNIT) { @@ -2485,13 +2485,13 @@ void movement(void) else { if (ships) { if (u->ship && ship_owner(u->ship) == u) { - init_order(u->thisorder); + init_order_depr(u->thisorder); move_cmd(u, u->thisorder); } } else { if (!u->ship || ship_owner(u->ship) != u) { - init_order(u->thisorder); + init_order_depr(u->thisorder); move_cmd(u, u->thisorder); } } @@ -2548,7 +2548,7 @@ void follow_unit(unit * u) if (getkeyword(ord) == K_FOLLOW) { int id; param_t p; - init_order(ord); + init_order_depr(ord); p = getparam(lang); if (p == P_UNIT) { id = read_unitid(u->faction, r); diff --git a/src/move.test.c b/src/move.test.c index c8fdb2436..908da0bb7 100644 --- a/src/move.test.c +++ b/src/move.test.c @@ -462,7 +462,7 @@ static void test_follow_ship_msg(CuTest * tc) { mt_register(mt_new_va("error18", "unit:unit", "region:region", "command:order", 0)); - init_order(ord); + init_order_depr(ord); getstrtoken(); follow_ship(u, ord); diff --git a/src/piracy.c b/src/piracy.c index 3025d484c..b74a0b363 100644 --- a/src/piracy.c +++ b/src/piracy.c @@ -86,7 +86,7 @@ int *parse_ids(const order *ord) { const char *s; int *il = NULL; - init_order(ord); + init_order_depr(ord); s = getstrtoken(); if (s != NULL && *s) { il = intlist_init(); @@ -211,7 +211,7 @@ void piracy_cmd(unit * u) ord = create_order(K_MOVE, u->faction->locale, "%s", LOC(u->faction->locale, directions[target_dir])); /* Bewegung ausführen */ - init_order(ord); + init_order_depr(ord); move_cmd(u, ord); free_order(ord); } diff --git a/src/renumber.c b/src/renumber.c index 30769403f..ec9d61c66 100644 --- a/src/renumber.c +++ b/src/renumber.c @@ -80,7 +80,7 @@ int renumber_cmd(unit * u, order * ord) int i = 0; faction *f = u->faction; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); switch (findparam_ex(s, u->faction->locale)) { diff --git a/src/spy.c b/src/spy.c index ae01656bf..ff1f6da4e 100644 --- a/src/spy.c +++ b/src/spy.c @@ -126,7 +126,7 @@ int spy_cmd(unit * u, struct order *ord) double spychance, observechance; region *r = u->region; - init_order(ord); + init_order_depr(ord); getunit(r, u->faction, &target); if (!target) { @@ -219,7 +219,7 @@ int setstealth_cmd(unit * u, struct order *ord) const char *s; int level; - init_order(ord); + init_order_depr(ord); s = gettoken(token, sizeof(token)); /* Tarne ohne Parameter: Setzt maximale Tarnung */ @@ -494,10 +494,10 @@ int sabotage_cmd(unit * u, struct order *ord) assert(u); assert(ord); - init_order(ord); + init_order_depr(ord); s = getstrtoken(); p = findparam(s, u->faction->locale); - init_order(NULL); + init_order_depr(NULL); switch (p) { case P_SHIP: diff --git a/src/study.c b/src/study.c index a66efd662..9264f9060 100644 --- a/src/study.c +++ b/src/study.c @@ -283,7 +283,7 @@ int teach_cmd(unit * teacher, struct order *ord) count = 0; - init_order(ord); + init_order_depr(ord); #if TEACH_ALL if (getparam(teacher->faction->locale) == P_ANY) { @@ -304,7 +304,7 @@ int teach_cmd(unit * teacher, struct order *ord) else if (student->faction == teacher->faction) { if (getkeyword(student->thisorder) == K_STUDY) { /* Input ist nun von student->thisorder !! */ - init_order(student->thisorder); + init_order(student->thisorder, student->faction->locale); sk = getskill(student->faction->locale); if (sk != NOSKILL && teachskill[0] != NOSKILL) { for (t = 0; teachskill[t] != NOSKILL; ++t) { @@ -324,7 +324,7 @@ int teach_cmd(unit * teacher, struct order *ord) else if (alliedunit(teacher, student->faction, HELP_GUARD)) { if (getkeyword(student->thisorder) == K_STUDY) { /* Input ist nun von student->thisorder !! */ - init_order(student->thisorder); + init_order(student->thisorder, student->faction->locale); sk = getskill(student->faction->locale); if (sk != NOSKILL && effskill_study(teacher, sk, 0) - TEACHDIFFERENCE >= effskill(student, sk, 0)) { @@ -343,7 +343,7 @@ int teach_cmd(unit * teacher, struct order *ord) order *new_order; zOrder[0] = '\0'; - init_order(ord); + init_order_depr(ord); while (!parser_end()) { skill_t sk; @@ -361,7 +361,7 @@ int teach_cmd(unit * teacher, struct order *ord) const char *token; /* Finde den string, der den Fehler verursacht hat */ parser_pushstate(); - init_order(ord); + init_order_depr(ord); for (j = 0; j != count - 1; ++j) { /* skip over the first 'count' units */ @@ -406,7 +406,7 @@ int teach_cmd(unit * teacher, struct order *ord) /* Input ist nun von student->thisorder !! */ parser_pushstate(); - init_order(student->thisorder); + init_order(student->thisorder, student->faction->locale); sk = getskill(student->faction->locale); parser_popstate(); @@ -448,7 +448,7 @@ int teach_cmd(unit * teacher, struct order *ord) if (academy_students > 0 && sk_academy!=NOSKILL) { academy_teaching_bonus(teacher, sk_academy, academy_students); } - init_order(NULL); + init_order_depr(NULL); return 0; } @@ -557,7 +557,7 @@ int study_cmd(unit * u, order * ord) return 0; } - init_order(ord); + (void)init_order(ord, u->faction->locale); sk = getskill(u->faction->locale); if (sk < 0) { @@ -785,7 +785,7 @@ int study_cmd(unit * u, order * ord) mage = create_mage(u, u->faction->magiegebiet); } } - init_order(NULL); + init_order_depr(NULL); return 0; } From 0a0806a181c1eb26b3cbea6bdeda08ebb13e2886 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Tue, 10 Oct 2017 09:29:32 +0200 Subject: [PATCH 8/9] try fixing coverity complaints --- src/kernel/order.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/kernel/order.c b/src/kernel/order.c index 7cdb2961d..88f6313fa 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -156,7 +156,7 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si if (ord->id < 0) { skill_t sk = (skill_t)(100+ord->id); - assert(kwd == K_STUDY && sk != SK_MAGIC); + assert(kwd == K_STUDY && sk != SK_MAGIC && sk < MAXSKILLS); text = skillname(sk, lang); } else { od = load_data(ord->id); @@ -270,7 +270,7 @@ static int create_data(keyword_t kwd, const char *s, } /* TODO: between mkdata and release_data, this object is very * short-lived. */ - mkdata(&data, s ? strlen(s) : 0, s); + mkdata(&data, strlen(s), s); id = save_data(kwd, data); release_data(data); return id; @@ -351,7 +351,11 @@ order *create_order(keyword_t kwd, const struct locale * lang, zBuffer[0] = 0; } ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, lang, kwd, zBuffer, false, false); + if (create_order_i(ord, lang, kwd, zBuffer, false, false) == NULL) { + free(ord); + return NULL; + } + return ord; } order *parse_order(const char *s, const struct locale * lang) @@ -383,7 +387,12 @@ order *parse_order(const char *s, const struct locale * lang) } if (kwd != NOKEYWORD) { order *ord = (order *)malloc(sizeof(order)); - return create_order_i(ord, lang, kwd, sptr, persistent, noerror); + if (create_order_i(ord, lang, kwd, sptr, persistent, noerror) + == NULL) { + free(ord); + return NULL; + } + return ord; } } return NULL; @@ -585,6 +594,7 @@ keyword_t init_order(const struct order *ord, const struct locale *lang) } if (ord->id < 0) { skill_t sk = (skill_t)(100 + ord->id); + assert(sk < MAXSKILLS); assert(lang); assert(kwd == K_STUDY); init_tokens_str(skillname(sk, lang)); From cbaf4bfd044272cedd1c8c4ce28698d852893d35 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 12 Oct 2017 18:11:21 +0200 Subject: [PATCH 9/9] refactoring: move order_data to orderdb module. --- src/CMakeLists.txt | 1 + src/eressea.c | 3 ++ src/kernel/order.c | 87 ++++++---------------------------------------- src/orderdb.c | 78 +++++++++++++++++++++++++++++++++++++++++ src/orderdb.h | 23 ++++++++++++ 5 files changed, 116 insertions(+), 76 deletions(-) create mode 100644 src/orderdb.c create mode 100644 src/orderdb.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e5ae8de7a..c8a4c4783 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -116,6 +116,7 @@ set (ERESSEA_SRC magic.c market.c morale.c + orderdb.c orderfile.c randenc.c renumber.c diff --git a/src/eressea.c b/src/eressea.c index 2174450eb..b4ae7890f 100755 --- a/src/eressea.c +++ b/src/eressea.c @@ -8,6 +8,7 @@ #include "creport.h" #include "report.h" #include "names.h" +#include "orderdb.h" #include "reports.h" #include "spells.h" #include "vortex.h" @@ -57,10 +58,12 @@ void game_done(void) free_special_directions(); free_locales(); kernel_done(); + orderdb_close(); } void game_init(void) { + orderdb_open(); kernel_init(); register_triggers(); register_xmas(); diff --git a/src/kernel/order.c b/src/kernel/order.c index 7cdb2961d..b8cf4bc4b 100644 --- a/src/kernel/order.c +++ b/src/kernel/order.c @@ -14,6 +14,7 @@ #include #include "order.h" +#include "orderdb.h" #include "skill.h" #include "keyword.h" @@ -33,71 +34,6 @@ # define ORD_KEYWORD(ord) (keyword_t)((ord)->command & 0xFFFF) # define OD_STRING(odata) ((odata) ? (odata)->_str : NULL) -typedef struct order_data { - const char *_str; - int _refcount; -} order_data; - -#include - -static critbit_tree cb_orders = { 0 }; -static int auto_id = 0; - -struct cb_entry { - int id; - order_data *data; -}; - -order_data *load_data(int id) { - void * match; - - if (id > 0) { - if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) { - struct cb_entry *ent = (struct cb_entry *)match; - order_data * od = ent->data; - ++od->_refcount; - return od; - } - } - return NULL; -} - -int save_data(keyword_t kwd, order_data *od) { - if (od->_str) { - struct cb_entry ent; - ++od->_refcount; - ent.id = ++auto_id; - ent.data = od; - cb_insert(&cb_orders, &ent, sizeof(ent)); - return ent.id; - } - return 0; -} - -static void release_data(order_data * data) -{ - if (data) { - if (--data->_refcount == 0) { - free(data); - } - } -} - -int free_data_cb(const void *match, const void *key, size_t keylen, void *udata) { - struct cb_entry * ent = (struct cb_entry *)match; - order_data *od = ent->data; - if (od->_refcount > 1) { - log_error("refcount=%d for order %d, %s", od->_refcount, ent->id, od->_str); - } - release_data(od); - return 0; -} - -void free_data(void) { - cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL); - cb_clear(&cb_orders); -} - void replace_order(order ** dlist, order * orig, const order * src) { assert(src); @@ -159,7 +95,7 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si assert(kwd == K_STUDY && sk != SK_MAGIC); text = skillname(sk, lang); } else { - od = load_data(ord->id); + od = odata_load(ord->id); text = OD_STRING(od); } if (kwd != NOKEYWORD) { @@ -193,7 +129,7 @@ char* get_command(const order *ord, const struct locale *lang, char *sbuffer, si } } if (od) { - release_data(od); + odata_release(od); } if (size > 0) *bufp = 0; return sbuffer; @@ -268,11 +204,11 @@ static int create_data(keyword_t kwd, const char *s, return ((int)sk)-100; } } - /* TODO: between mkdata and release_data, this object is very + /* TODO: between mkdata and odata_release, this object is very * short-lived. */ mkdata(&data, s ? strlen(s) : 0, s); - id = save_data(kwd, data); - release_data(data); + id = odata_save(data); + odata_release(data); return id; } @@ -547,11 +483,11 @@ char *write_order(const order * ord, const struct locale *lang, char *buffer, si else { keyword_t kwd = ORD_KEYWORD(ord); if (kwd == NOKEYWORD) { - order_data *od = load_data(ord->id); + order_data *od = odata_load(ord->id); const char *text = OD_STRING(od); if (text) strlcpy(buffer, (const char *)text, size); else buffer[0] = 0; - release_data(od); + odata_release(od); } else { get_command(ord, lang, buffer, size); @@ -572,7 +508,7 @@ static order_data *parser_od; keyword_t init_order(const struct order *ord, const struct locale *lang) { if (!ord) { - release_data(parser_od); + odata_release(parser_od); parser_od = NULL; return NOKEYWORD; } @@ -580,7 +516,7 @@ keyword_t init_order(const struct order *ord, const struct locale *lang) keyword_t kwd = ORD_KEYWORD(ord); if (parser_od) { /* TODO: warning */ - release_data(parser_od); + odata_release(parser_od); parser_od = NULL; } if (ord->id < 0) { @@ -590,7 +526,7 @@ keyword_t init_order(const struct order *ord, const struct locale *lang) init_tokens_str(skillname(sk, lang)); } else { - parser_od = load_data(ord->id); + parser_od = odata_load(ord->id); init_tokens_str(OD_STRING(parser_od)); } return kwd; @@ -610,6 +546,5 @@ void close_orders(void) { if (parser_od) { (void)init_order(NULL, NULL); } - free_data(); } diff --git a/src/orderdb.c b/src/orderdb.c new file mode 100644 index 000000000..37faeacc4 --- /dev/null +++ b/src/orderdb.c @@ -0,0 +1,78 @@ +#include +#include "orderdb.h" + +#include + +#include + +#include + +static critbit_tree cb_orders = { 0 }; +static int auto_id = 0; + +struct cb_entry { + int id; + order_data *data; +}; + +order_data *odata_load(int id) +{ + void * match; + + if (id > 0) { + if (cb_find_prefix(&cb_orders, &id, sizeof(id), &match, 1, 0) > 0) { + struct cb_entry *ent = (struct cb_entry *)match; + order_data * od = ent->data; + ++od->_refcount; + return od; + } + } + return NULL; +} + +int odata_save(order_data *od) +{ + if (od->_str) { + struct cb_entry ent; + ++od->_refcount; + ent.id = ++auto_id; + ent.data = od; + cb_insert(&cb_orders, &ent, sizeof(ent)); + return ent.id; + } + return 0; +} + +void odata_release(order_data * od) +{ + if (od) { + if (--od->_refcount == 0) { + free(od); + } + } +} + +int free_data_cb(const void *match, const void *key, size_t keylen, void *udata) { + struct cb_entry * ent = (struct cb_entry *)match; + order_data *od = ent->data; + if (od->_refcount > 1) { + log_error("refcount=%d for order %d, %s", od->_refcount, ent->id, od->_str); + } + odata_release(od); + return 0; +} + +void free_data(void) { + cb_foreach(&cb_orders, NULL, 0, free_data_cb, NULL); + cb_clear(&cb_orders); +} + +void orderdb_open(void) +{ + auto_id = 0; +} + +void orderdb_close(void) +{ + free_data(); +} diff --git a/src/orderdb.h b/src/orderdb.h new file mode 100644 index 000000000..be39a642e --- /dev/null +++ b/src/orderdb.h @@ -0,0 +1,23 @@ +#ifndef H_ORDERDB +#define H_ORDERDB + +#ifdef __cplusplus +extern "C" { +#endif + + typedef struct order_data { + const char *_str; + int _refcount; + } order_data; + + void orderdb_open(void); + void orderdb_close(void); + + order_data *odata_load(int id); + int odata_save(order_data *od); + void odata_release(order_data * od); + +#ifdef __cplusplus +} +#endif +#endif