From b432fbe9d8dfb8f15189fc7554a88d416d24f008 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 14 Jan 2018 11:25:44 +0100 Subject: [PATCH] Make the behavior for missing messages configurable. message_handle_missing function to set it (test_eressea should enetually use MESSAGE_MISSING_ERROR). --- src/economy.test.c | 32 ++++++++++++++++++++++++-------- src/kernel/messages.c | 21 ++++++++++++++++----- src/kernel/messages.h | 6 ++++++ src/main.c | 4 +++- src/test_eressea.c | 2 ++ src/util/log.c | 1 - 6 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/economy.test.c b/src/economy.test.c index 70bff245c..51aa06137 100644 --- a/src/economy.test.c +++ b/src/economy.test.c @@ -141,11 +141,19 @@ static struct unit *create_recruiter(void) { return u; } +static void setup_production(void) { + init_resources(); + mt_register(mt_new_va("produce", "unit:unit", "region:region", "amount:int", "wanted:int", "resource:resource", NULL)); + mt_register(mt_new_va("income", "unit:unit", "region:region", "amount:int", "wanted:int", "mode:int", NULL)); + mt_register(mt_new_va("buy", "unit:unit", "money:int", NULL)); + mt_register(mt_new_va("buyamount", "unit:unit", "amount:int", "resource:resource", NULL)); +} + static void test_heroes_dont_recruit(CuTest * tc) { unit *u; test_setup(); - init_resources(); + setup_production(); u = create_recruiter(); fset(u, UFL_HERO); @@ -163,7 +171,7 @@ static void test_normals_recruit(CuTest * tc) { unit *u; test_setup(); - init_resources(); + setup_production(); u = create_recruiter(); unit_addorder(u, create_order(K_RECRUIT, default_locale, "1")); @@ -227,7 +235,7 @@ static void test_trade_insect(CuTest *tc) { const item_type *it_silver; test_setup(); - init_resources(); + setup_production(); test_create_locale(); setup_terrains(tc); r = setup_trade_region(tc, get_terrain("swamp")); @@ -259,7 +267,7 @@ static void test_buy_cmd(CuTest *tc) { const resource_type *rt_silver; const item_type *it_luxury; test_setup(); - init_resources(); + setup_production(); test_create_locale(); setup_terrains(tc); r = setup_trade_region(tc, test_create_terrain("swamp", LAND_REGION)); @@ -312,7 +320,7 @@ static void test_tax_cmd(CuTest *tc) { econ_request *taxorders = 0; test_setup(); - init_resources(); + setup_production(); config_set("taxing.perlevel", "20"); f = test_create_faction(NULL); r = test_create_region(0, 0, NULL); @@ -361,6 +369,13 @@ static void test_tax_cmd(CuTest *tc) { test_teardown(); } +static void setup_maintenance(void) { + mt_register(mt_new_va("maintenance", "unit:unit", "building:building", NULL)); + mt_register(mt_new_va("maintenancefail", "unit:unit", "building:building", NULL)); + mt_register(mt_new_va("maintenance_nowork", "building:building", NULL)); + mt_register(mt_new_va("maintenance_noowner", "building:building", NULL)); +} + /** * see https://bugs.eressea.de/view.php?id=2234 */ @@ -374,6 +389,7 @@ static void test_maintain_buildings(CuTest *tc) { item_type *itype; test_setup(); + setup_maintenance(); btype = test_create_buildingtype("Hort"); btype->maxsize = 10; r = test_create_region(0, 0, NULL); @@ -472,7 +488,7 @@ static void test_modify_material(CuTest *tc) { resource_mod *mod; test_setup(); - init_resources(); + setup_production(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); set_level(u, SK_WEAPONSMITH, 1); @@ -521,7 +537,7 @@ static void test_modify_skill(CuTest *tc) { resource_mod *mod; test_setup(); - init_resources(); + setup_production(); u = test_create_unit(test_create_faction(NULL), test_create_region(0, 0, NULL)); set_level(u, SK_WEAPONSMITH, 1); @@ -580,7 +596,7 @@ static void test_modify_production(CuTest *tc) { double d = 0.6; test_setup(); - init_resources(); + setup_production(); /* make items from other items (turn silver to stone) */ rt_silver = get_resourcetype(R_SILVER); diff --git a/src/kernel/messages.c b/src/kernel/messages.c index 837b40a11..03883f127 100644 --- a/src/kernel/messages.c +++ b/src/kernel/messages.c @@ -148,12 +148,24 @@ struct message *msg_feedback(const struct unit *u, struct order *ord, return msg_create(mtype, args); } +static int missing_message_mode; + +void message_handle_missing(int mode) { + missing_message_mode = mode; +} + static message *missing_message(const char *name) { - if (strcmp(name, "missing_message") != 0) { - if (!mt_find("missing_message")) { - mt_register(mt_new_va("missing_message", "name:string", NULL)); + if (missing_message_mode == MESSAGE_MISSING_ERROR) { + log_error("trying to create undefined message of type \"%s\"\n", name); + } + else if (missing_message_mode == MESSAGE_MISSING_REPLACE) { + log_warning("trying to create undefined message of type \"%s\"\n", name); + if (strcmp(name, "missing_message") != 0) { + if (!mt_find("missing_message")) { + mt_register(mt_new_va("missing_message", "name:string", NULL)); + } + return msg_message("missing_message", "name", name); } - return msg_message("missing_message", "name", name); } return NULL; } @@ -170,7 +182,6 @@ message *msg_message(const char *name, const char *sig, ...) memset(args, 0, sizeof(args)); if (!mtype) { - log_warning("trying to create undefined message of type \"%s\"\n", name); return missing_message(name); } diff --git a/src/kernel/messages.h b/src/kernel/messages.h index 59bac9af2..723ed2d01 100644 --- a/src/kernel/messages.h +++ b/src/kernel/messages.h @@ -46,6 +46,12 @@ extern "C" { int level; } msglevel; +#define MESSAGE_MISSING_IGNORE 0 +#define MESSAGE_MISSING_ERROR 1 +#define MESSAGE_MISSING_REPLACE 2 + + void message_handle_missing(int mode); + struct message *msg_message(const char *name, const char *sig, ...); struct message *msg_feedback(const struct unit *, struct order *cmd, const char *name, const char *sig, ...); diff --git a/src/main.c b/src/main.c index 1ad91589d..f50f65472 100644 --- a/src/main.c +++ b/src/main.c @@ -22,8 +22,9 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #include #include -#include +#include #include +#include #include #include @@ -305,6 +306,7 @@ int main(int argc, char **argv) lua_State *L; dictionary *d = 0; setup_signal_handler(); + message_handle_missing(MESSAGE_MISSING_REPLACE); /* parse arguments again, to override ini file */ err = parse_args(argc, argv); if (err != 0) { diff --git a/src/test_eressea.c b/src/test_eressea.c index fe3458d1e..a192ad4aa 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -184,5 +185,6 @@ int main(int argc, char ** argv) { ++argv; --argc; } + message_handle_missing(MESSAGE_MISSING_REPLACE); return RunAllTests(argc, argv); } diff --git a/src/util/log.c b/src/util/log.c index bae1556ed..51479cb4a 100644 --- a/src/util/log.c +++ b/src/util/log.c @@ -222,7 +222,6 @@ static void log_write(int flags, const char *module, const char *format, va_list } } - void log_fatal(const char *format, ...) { va_list args;