From e14e4c77614afa2cacab9b5042554702da4b04fa Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Sun, 10 May 2020 23:29:47 +0200 Subject: Read name of new map_reg_*_db tables from configuration file --- src/map/map.c | 4 ++++ src/map/mapreg.h | 3 +++ src/map/mapreg_sql.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) (limited to 'src/map') diff --git a/src/map/map.c b/src/map/map.c index f66f40dfc..d9be46bfb 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -4486,6 +4486,10 @@ static bool inter_config_read_database_names(const char *filename, const struct ShowError("inter_config_read: inter_configuration/database_names/registry was not found in %s!\n", filename); return false; } + + if (!mapreg->config_read_registry(filename, setting, imported)) + retval = false; + return retval; } diff --git a/src/map/mapreg.h b/src/map/mapreg.h index b3b89e1b2..bd0e13e21 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -48,6 +48,8 @@ struct mapreg_interface { struct eri *ers; //[Ind/Hercules] /* */ char table[32]; + char num_db[32]; //!< Name of SQL table which holds permanent global integer variables. + char str_db[32]; //!< Name of SQL table which holds permanent global string variables. /* */ bool dirty; ///< Whether there are modified regs to be saved /* */ @@ -63,6 +65,7 @@ struct mapreg_interface { int (*save_timer) (int tid, int64 tick, int id, intptr_t data); int (*destroyreg) (union DBKey key, struct DBData *data, va_list ap); void (*reload) (void); + bool (*config_read_registry) (const char *filename, const struct config_setting_t *config, bool imported); bool (*config_read) (const char *filename, const struct config_setting_t *config, bool imported); }; diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index 2963637da..a461763fa 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -328,6 +328,42 @@ static void mapreg_reload(void) mapreg->load(); } +/** + * Loads the mapreg database table names from configuration file. + * + * @param filename Path to configuration file. (Used in error and warning messages). + * @param config The current config being parsed. + * @param imported Whether the current config is imported from another file. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_config_read_registry(const char *filename, const struct config_setting_t *config, bool imported) +{ + nullpo_retr(false, filename); + nullpo_retr(false, config); + + bool ret_val = true; + size_t sz = sizeof(mapreg->num_db); + int result = libconfig->setting_lookup_mutable_string(config, "map_reg_num_db", mapreg->num_db, sz); + + if (result != CONFIG_TRUE && !imported) { + ShowError("%s: inter_configuration/database_names/registry/map_reg_num_db was not found in %s!\n", + __func__, filename); + ret_val = false; + } + + sz = sizeof(mapreg->str_db); + result = libconfig->setting_lookup_mutable_string(config, "map_reg_str_db", mapreg->str_db, sz); + + if (result != CONFIG_TRUE && !imported) { + ShowError("%s: inter_configuration/database_names/registry/map_reg_str_db was not found in %s!\n", + __func__, filename); + ret_val = false; + } + + return ret_val; +} + /** * Finalizer. */ @@ -390,6 +426,8 @@ void mapreg_defaults(void) mapreg->skip_insert = false; safestrncpy(mapreg->table, "mapreg", sizeof(mapreg->table)); + safestrncpy(mapreg->num_db, "map_reg_num_db", sizeof(mapreg->num_db)); + safestrncpy(mapreg->str_db, "map_reg_str_db", sizeof(mapreg->str_db)); mapreg->dirty = false; /* */ @@ -409,6 +447,7 @@ void mapreg_defaults(void) mapreg->save_timer = script_autosave_mapreg; mapreg->destroyreg = mapreg_destroyreg; mapreg->reload = mapreg_reload; + mapreg->config_read_registry = mapreg_config_read_registry; mapreg->config_read = mapreg_config_read; } -- cgit v1.2.3-70-g09d2 From 94155c04e142ed5e441ffd4f07ff85290e5b4cd5 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Mon, 11 May 2020 01:12:30 +0200 Subject: Implement usage of new map_reg_*_db tables --- src/map/mapreg.h | 4 + src/map/mapreg_sql.c | 259 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 263 insertions(+) (limited to 'src/map') diff --git a/src/map/mapreg.h b/src/map/mapreg.h index bd0e13e21..772a6cb61 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -60,7 +60,11 @@ struct mapreg_interface { char* (*readregstr) (int64 uid); bool (*setreg) (int64 uid, int val); bool (*setregstr) (int64 uid, const char *str); + void (*load_num_db) (void); + void (*load_str_db) (void); void (*load) (void); + void (*save_num_db) (const char *name, unsigned int index, int value); + void (*save_str_db) (const char *name, unsigned int index, const char *value); void (*save) (void); int (*save_timer) (int tid, int64 tick, int id, intptr_t data); int (*destroyreg) (union DBKey key, struct DBData *data, va_list ap); diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index a461763fa..376e582f9 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -105,6 +105,25 @@ static bool mapreg_setreg(int64 uid, int val) SQL->EscapeStringLen(map->mysql_handle, tmp_str, name, strnlen(name, SCRIPT_VARNAME_LENGTH+1)); if( SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%u','%d')", mapreg->table, tmp_str, i, val) ) Sql_ShowDebug(map->mysql_handle); + + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + } else { + const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)"; + size_t len = strnlen(name, SCRIPT_VARNAME_LENGTH); + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &val, sizeof(val)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + } + + SQL->StmtFree(stmt); + } } i64db_put(mapreg->regs.vars, uid, m); } @@ -119,6 +138,24 @@ static bool mapreg_setreg(int64 uid, int val) if( name[1] != '@' ) {// Remove from database because it is unused. if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%u'", mapreg->table, name, i) ) Sql_ShowDebug(map->mysql_handle); + + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + } else { + const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?"; + size_t len = strnlen(name, SCRIPT_VARNAME_LENGTH); + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + } + + SQL->StmtFree(stmt); + } } } @@ -147,6 +184,24 @@ static bool mapreg_setregstr(int64 uid, const char *str) if(name[1] != '@') { if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%u'", mapreg->table, name, i)) Sql_ShowDebug(map->mysql_handle); + + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + } else { + const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?"; + size_t len = strnlen(name, SCRIPT_VARNAME_LENGTH); + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + } + + SQL->StmtFree(stmt); + } } if( (m = i64db_get(mapreg->regs.vars, uid)) ) { if( m->u.str != NULL ) @@ -181,6 +236,26 @@ static bool mapreg_setregstr(int64 uid, const char *str) SQL->EscapeStringLen(map->mysql_handle, tmp_str2, str, strnlen(str, SCRIPT_STRING_VAR_LENGTH)); if( SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%u','%s')", mapreg->table, tmp_str, i, tmp_str2) ) Sql_ShowDebug(map->mysql_handle); + + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + } else { + const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)"; + size_t len_n = strnlen(name, SCRIPT_VARNAME_LENGTH); + size_t len_v = strnlen(str, SCRIPT_STRING_VAR_LENGTH); + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len_n) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_STRING, str, len_v) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + } + + SQL->StmtFree(stmt); + } } i64db_put(mapreg->regs.vars, uid, m); } @@ -189,6 +264,108 @@ static bool mapreg_setregstr(int64 uid, const char *str) return true; } +/** + * Loads permanent global interger variables from the database. + * + **/ +static void mapreg_load_num_db(void) +{ + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return; + } + + const char *query = "SELECT `key`, `index`, `value` FROM `%s`"; + char name[SCRIPT_VARNAME_LENGTH + 1]; + unsigned int index; + int value; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) + || SQL_ERROR == SQL->StmtExecute(stmt) + || SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof(name), NULL, NULL) + || SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_UINT32, &index, sizeof(index), NULL, NULL) + || SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_INT32, &value, sizeof(value), NULL, NULL)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return; + } + + if (SQL->StmtNumRows(stmt) < 1) { + SQL->StmtFree(stmt); + return; + } + + mapreg->skip_insert = true; + + while (SQL_SUCCESS == SQL->StmtNextRow(stmt)) { + int var_key = script->add_variable(name); + int64 uid = reference_uid(var_key, index); + + if (i64db_exists(mapreg->regs.vars, uid)) { + ShowWarning("mapreg_load_num_db: Duplicate! '%s' => '%d' Skipping...\n", name, value); + continue; + } + + mapreg->setreg(uid, value); + } + + mapreg->skip_insert = false; + SQL->StmtFree(stmt); +} + +/** + * Loads permanent global string variables from the database. + * + **/ +static void mapreg_load_str_db(void) +{ + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return; + } + + const char *query = "SELECT `key`, `index`, `value` FROM `%s`"; + char name[SCRIPT_VARNAME_LENGTH + 1]; + unsigned int index; + char value[SCRIPT_STRING_VAR_LENGTH + 1]; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) + || SQL_ERROR == SQL->StmtExecute(stmt) + || SQL_ERROR == SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &name, sizeof(name), NULL, NULL) + || SQL_ERROR == SQL->StmtBindColumn(stmt, 1, SQLDT_UINT32, &index, sizeof(index), NULL, NULL) + || SQL_ERROR == SQL->StmtBindColumn(stmt, 2, SQLDT_STRING, &value, sizeof(value), NULL, NULL)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return; + } + + if (SQL->StmtNumRows(stmt) < 1) { + SQL->StmtFree(stmt); + return; + } + + mapreg->skip_insert = true; + + while (SQL_SUCCESS == SQL->StmtNextRow(stmt)) { + int var_key = script->add_variable(name); + int64 uid = reference_uid(var_key, index); + + if (i64db_exists(mapreg->regs.vars, uid)) { + ShowWarning("mapreg_load_str_db: Duplicate! '%s' => '%s' Skipping...\n", name, value); + continue; + } + + mapreg->setregstr(uid, value); + } + + mapreg->skip_insert = false; + SQL->StmtFree(stmt); +} + /** * Loads permanent variables from database. */ @@ -240,9 +417,83 @@ static void script_load_mapreg(void) mapreg->skip_insert = false; + mapreg->load_num_db(); + mapreg->load_str_db(); + mapreg->dirty = false; } +/** + * Saves a permanent global integer variable to the database. + * + * @param name The variable's name. + * @param index The variable's array index. + * @param value The variable's value. + * + **/ +static void mapreg_save_num_db(const char *name, unsigned int index, int value) +{ + nullpo_retv(name); + Assert_retv(*name != '\0'); + Assert_retv(strlen(name) <= SCRIPT_VARNAME_LENGTH); + + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return; + } + + const char *query = "UPDATE `%s` SET `value`=? WHERE `key`=? AND `index`=? LIMIT 1"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_INT32, &value, sizeof(value)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, name, strlen(name)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_UINT32, &index, sizeof(index)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + } + + SQL->StmtFree(stmt); +} + +/** + * Saves a permanent global string variable to the database. + * + * @param name The variable's name. + * @param index The variable's array index. + * @param value The variable's value. + * + **/ +static void mapreg_save_str_db(const char *name, unsigned int index, const char *value) +{ + nullpo_retv(name); + nullpo_retv(value); + Assert_retv(*name != '\0'); + Assert_retv(strlen(name) <= SCRIPT_VARNAME_LENGTH); + Assert_retv(*value != '\0'); + Assert_retv(strlen(value) <= SCRIPT_STRING_VAR_LENGTH); + + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return; + } + + const char *query = "UPDATE `%s` SET `value`=? WHERE `key`=? AND `index`=? LIMIT 1"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, value, strlen(value)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_STRING, name, strlen(name)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_UINT32, &index, sizeof(index)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + } + + SQL->StmtFree(stmt); +} + /** * Saves permanent variables to database. */ @@ -260,11 +511,15 @@ static void script_save_mapreg(void) if (!m->is_string) { if( SQL_ERROR == SQL->Query(map->mysql_handle, "UPDATE `%s` SET `value`='%d' WHERE `varname`='%s' AND `index`='%d' LIMIT 1", mapreg->table, m->u.i, name, i) ) Sql_ShowDebug(map->mysql_handle); + + mapreg->save_num_db(name, i, m->u.i); } else { char tmp_str2[SCRIPT_STRING_VAR_LENGTH * 2 + 1]; SQL->EscapeStringLen(map->mysql_handle, tmp_str2, m->u.str, safestrnlen(m->u.str, SCRIPT_STRING_VAR_LENGTH)); if( SQL_ERROR == SQL->Query(map->mysql_handle, "UPDATE `%s` SET `value`='%s' WHERE `varname`='%s' AND `index`='%d' LIMIT 1", mapreg->table, tmp_str2, name, i) ) Sql_ShowDebug(map->mysql_handle); + + mapreg->save_str_db(name, i, m->u.str); } m->save = false; } @@ -442,7 +697,11 @@ void mapreg_defaults(void) mapreg->readregstr = mapreg_readregstr; mapreg->setreg = mapreg_setreg; mapreg->setregstr = mapreg_setregstr; + mapreg->load_num_db = mapreg_load_num_db; + mapreg->load_str_db = mapreg_load_str_db; mapreg->load = script_load_mapreg; + mapreg->save_num_db = mapreg_save_num_db; + mapreg->save_str_db = mapreg_save_str_db; mapreg->save = script_save_mapreg; mapreg->save_timer = script_autosave_mapreg; mapreg->destroyreg = mapreg_destroyreg; -- cgit v1.2.3-70-g09d2 From d80274aa9483c6d2d0e1a70b85f654a7d0eddb04 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Mon, 11 May 2020 01:33:39 +0200 Subject: Remove old mapreg table related code and config --- conf/common/inter-server.conf | 1 - src/map/map.c | 11 +-- src/map/mapreg.h | 2 - src/map/mapreg_sql.c | 101 +-------------------- src/plugins/HPMHooking/HPMHooking.Defs.inc | 2 - .../HPMHooking/HPMHooking_map.HPMHooksCore.inc | 4 - .../HPMHooking/HPMHooking_map.HookingPoints.inc | 1 - src/plugins/HPMHooking/HPMHooking_map.Hooks.inc | 27 ------ 8 files changed, 5 insertions(+), 144 deletions(-) (limited to 'src/map') diff --git a/conf/common/inter-server.conf b/conf/common/inter-server.conf index 1bcf34e23..9960c46d6 100644 --- a/conf/common/inter-server.conf +++ b/conf/common/inter-server.conf @@ -114,7 +114,6 @@ inter_configuration: { position_db: "guild_position" storage_db: "guild_storage" } - mapreg_db: "mapreg" autotrade_merchants_db: "autotrade_merchants" autotrade_data_db: "autotrade_data" npc_market_data_db: "npc_market_data" diff --git a/src/map/map.c b/src/map/map.c index d9be46bfb..63d05c1c0 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -4459,7 +4459,6 @@ static bool inter_config_read_connection(const char *filename, const struct conf static bool inter_config_read_database_names(const char *filename, const struct config_t *config, bool imported) { const struct config_setting_t *setting = NULL; - bool retval = true; nullpo_retr(false, filename); nullpo_retr(false, config); @@ -4477,20 +4476,14 @@ static bool inter_config_read_database_names(const char *filename, const struct libconfig->setting_lookup_mutable_string(setting, "npc_barter_data_db", map->npc_barter_data_db, sizeof(map->npc_barter_data_db)); libconfig->setting_lookup_mutable_string(setting, "npc_expanded_barter_data_db", map->npc_expanded_barter_data_db, sizeof(map->npc_expanded_barter_data_db)); - if (!mapreg->config_read(filename, setting, imported)) - retval = false; - if ((setting = libconfig->lookup(config, "inter_configuration/database_names/registry")) == NULL) { if (imported) - return retval; + return true; ShowError("inter_config_read: inter_configuration/database_names/registry was not found in %s!\n", filename); return false; } - if (!mapreg->config_read_registry(filename, setting, imported)) - retval = false; - - return retval; + return mapreg->config_read_registry(filename, setting, imported); } /*======================================= diff --git a/src/map/mapreg.h b/src/map/mapreg.h index 772a6cb61..f97cd997d 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -47,7 +47,6 @@ struct mapreg_interface { /* */ struct eri *ers; //[Ind/Hercules] /* */ - char table[32]; char num_db[32]; //!< Name of SQL table which holds permanent global integer variables. char str_db[32]; //!< Name of SQL table which holds permanent global string variables. /* */ @@ -70,7 +69,6 @@ struct mapreg_interface { int (*destroyreg) (union DBKey key, struct DBData *data, va_list ap); void (*reload) (void); bool (*config_read_registry) (const char *filename, const struct config_setting_t *config, bool imported); - bool (*config_read) (const char *filename, const struct config_setting_t *config, bool imported); }; #ifdef HERCULES_CORE diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index 376e582f9..cf509f027 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -101,11 +101,6 @@ static bool mapreg_setreg(int64 uid, int val) m->is_string = false; if (name[1] != '@' && !mapreg->skip_insert) {// write new variable to database - char tmp_str[(SCRIPT_VARNAME_LENGTH+1)*2+1]; - SQL->EscapeStringLen(map->mysql_handle, tmp_str, name, strnlen(name, SCRIPT_VARNAME_LENGTH+1)); - if( SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%u','%d')", mapreg->table, tmp_str, i, val) ) - Sql_ShowDebug(map->mysql_handle); - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); if (stmt == NULL) { @@ -136,9 +131,6 @@ static bool mapreg_setreg(int64 uid, int val) i64db_remove(mapreg->regs.vars, uid); if( name[1] != '@' ) {// Remove from database because it is unused. - if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%u'", mapreg->table, name, i) ) - Sql_ShowDebug(map->mysql_handle); - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); if (stmt == NULL) { @@ -182,9 +174,6 @@ static bool mapreg_setregstr(int64 uid, const char *str) if( i ) script->array_update(&mapreg->regs, uid, true); if(name[1] != '@') { - if (SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%u'", mapreg->table, name, i)) - Sql_ShowDebug(map->mysql_handle); - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); if (stmt == NULL) { @@ -230,13 +219,6 @@ static bool mapreg_setregstr(int64 uid, const char *str) m->is_string = true; if(name[1] != '@' && !mapreg->skip_insert) { //put returned null, so we must insert. - char tmp_str[(SCRIPT_VARNAME_LENGTH+1)*2+1]; - char tmp_str2[SCRIPT_STRING_VAR_LENGTH * 2 + 1]; - SQL->EscapeStringLen(map->mysql_handle, tmp_str, name, strnlen(name, SCRIPT_VARNAME_LENGTH+1)); - SQL->EscapeStringLen(map->mysql_handle, tmp_str2, str, strnlen(str, SCRIPT_STRING_VAR_LENGTH)); - if( SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%u','%s')", mapreg->table, tmp_str, i, tmp_str2) ) - Sql_ShowDebug(map->mysql_handle); - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); if (stmt == NULL) { @@ -371,55 +353,8 @@ static void mapreg_load_str_db(void) */ static void script_load_mapreg(void) { - /* - 0 1 2 - +-------------------------+ - | varname | index | value | - +-------------------------+ - */ - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); - char varname[SCRIPT_VARNAME_LENGTH+1]; - int index; - char value[SCRIPT_STRING_VAR_LENGTH + 1]; - uint32 length; - - if ( SQL_ERROR == SQL->StmtPrepare(stmt, "SELECT `varname`, `index`, `value` FROM `%s`", mapreg->table) - || SQL_ERROR == SQL->StmtExecute(stmt) - ) { - SqlStmt_ShowDebug(stmt); - SQL->StmtFree(stmt); - return; - } - - mapreg->skip_insert = true; - - SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &varname, sizeof varname, &length, NULL); - SQL->StmtBindColumn(stmt, 1, SQLDT_INT, &index, sizeof index, NULL, NULL); - SQL->StmtBindColumn(stmt, 2, SQLDT_STRING, &value, sizeof value, NULL, NULL); - - while ( SQL_SUCCESS == SQL->StmtNextRow(stmt) ) { - int s = script->add_variable(varname); - int i = index; - - - if( i64db_exists(mapreg->regs.vars, reference_uid(s, i)) ) { - ShowWarning("load_mapreg: duplicate! '%s' => '%s' skipping...\n",varname,value); - continue; - } - if( varname[length-1] == '$' ) { - mapreg->setregstr(reference_uid(s, i),value); - } else { - mapreg->setreg(reference_uid(s, i),atoi(value)); - } - } - - SQL->StmtFree(stmt); - - mapreg->skip_insert = false; - mapreg->load_num_db(); mapreg->load_str_db(); - mapreg->dirty = false; } @@ -508,19 +443,12 @@ static void script_save_mapreg(void) int i = script_getvaridx(m->uid); const char* name = script->get_str(num); nullpo_retv(name); - if (!m->is_string) { - if( SQL_ERROR == SQL->Query(map->mysql_handle, "UPDATE `%s` SET `value`='%d' WHERE `varname`='%s' AND `index`='%d' LIMIT 1", mapreg->table, m->u.i, name, i) ) - Sql_ShowDebug(map->mysql_handle); + if (!m->is_string) mapreg->save_num_db(name, i, m->u.i); - } else { - char tmp_str2[SCRIPT_STRING_VAR_LENGTH * 2 + 1]; - SQL->EscapeStringLen(map->mysql_handle, tmp_str2, m->u.str, safestrnlen(m->u.str, SCRIPT_STRING_VAR_LENGTH)); - if( SQL_ERROR == SQL->Query(map->mysql_handle, "UPDATE `%s` SET `value`='%s' WHERE `varname`='%s' AND `index`='%d' LIMIT 1", mapreg->table, tmp_str2, name, i) ) - Sql_ShowDebug(map->mysql_handle); - + else mapreg->save_str_db(name, i, m->u.str); - } + m->save = false; } } @@ -648,26 +576,6 @@ static void mapreg_init(void) timer->add_interval(timer->gettick() + MAPREG_AUTOSAVE_INTERVAL, mapreg->save_timer, 0, 0, MAPREG_AUTOSAVE_INTERVAL); } -/** - * Loads the mapreg configuration file. - * - * @param filename Path to configuration file (used in error and warning messages). - * @param config The current config being parsed. - * @param imported Whether the current config is imported from another file. - * - * @retval false in case of error. - */ -static bool mapreg_config_read(const char *filename, const struct config_setting_t *config, bool imported) -{ - nullpo_retr(false, filename); - nullpo_retr(false, config); - - if (libconfig->setting_lookup_mutable_string(config, "mapreg_db", mapreg->table, sizeof(mapreg->table)) != CONFIG_TRUE) - return false; - - return true; -} - /** * Interface defaults initializer. */ @@ -680,7 +588,6 @@ void mapreg_defaults(void) mapreg->ers = NULL; mapreg->skip_insert = false; - safestrncpy(mapreg->table, "mapreg", sizeof(mapreg->table)); safestrncpy(mapreg->num_db, "map_reg_num_db", sizeof(mapreg->num_db)); safestrncpy(mapreg->str_db, "map_reg_str_db", sizeof(mapreg->str_db)); mapreg->dirty = false; @@ -707,6 +614,4 @@ void mapreg_defaults(void) mapreg->destroyreg = mapreg_destroyreg; mapreg->reload = mapreg_reload; mapreg->config_read_registry = mapreg_config_read_registry; - mapreg->config_read = mapreg_config_read; - } diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index ef71f1967..c8dd6fed3 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -5246,8 +5246,6 @@ typedef int (*HPMHOOK_pre_mapreg_destroyreg) (union DBKey *key, struct DBData ** typedef int (*HPMHOOK_post_mapreg_destroyreg) (int retVal___, union DBKey key, struct DBData *data, va_list ap); typedef void (*HPMHOOK_pre_mapreg_reload) (void); typedef void (*HPMHOOK_post_mapreg_reload) (void); -typedef bool (*HPMHOOK_pre_mapreg_config_read) (const char **filename, const struct config_setting_t **config, bool *imported); -typedef bool (*HPMHOOK_post_mapreg_config_read) (bool retVal___, const char *filename, const struct config_setting_t *config, bool imported); #endif // MAP_MAPREG_H #ifdef COMMON_MD5CALC_H /* md5 */ typedef void (*HPMHOOK_pre_md5_string) (const char **string, char **output); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index e8cb41240..c458ef539 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -3850,8 +3850,6 @@ struct { struct HPMHookPoint *HP_mapreg_destroyreg_post; struct HPMHookPoint *HP_mapreg_reload_pre; struct HPMHookPoint *HP_mapreg_reload_post; - struct HPMHookPoint *HP_mapreg_config_read_pre; - struct HPMHookPoint *HP_mapreg_config_read_post; struct HPMHookPoint *HP_md5_string_pre; struct HPMHookPoint *HP_md5_string_post; struct HPMHookPoint *HP_md5_binary_pre; @@ -10751,8 +10749,6 @@ struct { int HP_mapreg_destroyreg_post; int HP_mapreg_reload_pre; int HP_mapreg_reload_post; - int HP_mapreg_config_read_pre; - int HP_mapreg_config_read_post; int HP_md5_string_pre; int HP_md5_string_post; int HP_md5_binary_pre; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 6b89841ad..bdfcbf931 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -1971,7 +1971,6 @@ struct HookingPointData HookingPoints[] = { { HP_POP(mapreg->save_timer, HP_mapreg_save_timer) }, { HP_POP(mapreg->destroyreg, HP_mapreg_destroyreg) }, { HP_POP(mapreg->reload, HP_mapreg_reload) }, - { HP_POP(mapreg->config_read, HP_mapreg_config_read) }, /* md5_interface */ { HP_POP(md5->string, HP_md5_string) }, { HP_POP(md5->binary, HP_md5_binary) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index 02d55228e..b25ca934b 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -50916,33 +50916,6 @@ void HP_mapreg_reload(void) { } return; } -bool HP_mapreg_config_read(const char *filename, const struct config_setting_t *config, bool imported) { - int hIndex = 0; - bool retVal___ = false; - if (HPMHooks.count.HP_mapreg_config_read_pre > 0) { - bool (*preHookFunc) (const char **filename, const struct config_setting_t **config, bool *imported); - *HPMforce_return = false; - for (hIndex = 0; hIndex < HPMHooks.count.HP_mapreg_config_read_pre; hIndex++) { - preHookFunc = HPMHooks.list.HP_mapreg_config_read_pre[hIndex].func; - retVal___ = preHookFunc(&filename, &config, &imported); - } - if (*HPMforce_return) { - *HPMforce_return = false; - return retVal___; - } - } - { - retVal___ = HPMHooks.source.mapreg.config_read(filename, config, imported); - } - if (HPMHooks.count.HP_mapreg_config_read_post > 0) { - bool (*postHookFunc) (bool retVal___, const char *filename, const struct config_setting_t *config, bool imported); - for (hIndex = 0; hIndex < HPMHooks.count.HP_mapreg_config_read_post; hIndex++) { - postHookFunc = HPMHooks.list.HP_mapreg_config_read_post[hIndex].func; - retVal___ = postHookFunc(retVal___, filename, config, imported); - } - } - return retVal___; -} /* md5_interface */ void HP_md5_string(const char *string, char *output) { int hIndex = 0; -- cgit v1.2.3-70-g09d2 From b1fd1222631d43fe3f91beee941f514cc0181773 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Mon, 11 May 2020 04:45:50 +0200 Subject: Use separate functions for setting and deleting global integer variables --- src/map/mapreg.h | 2 + src/map/mapreg_sql.c | 184 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 121 insertions(+), 65 deletions(-) (limited to 'src/map') diff --git a/src/map/mapreg.h b/src/map/mapreg.h index f97cd997d..70c8666fa 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -57,6 +57,8 @@ struct mapreg_interface { /* */ int (*readreg) (int64 uid); char* (*readregstr) (int64 uid); + bool (*set_num_db) (int64 uid, const char *name, unsigned int index, int value); + bool (*delete_num_db) (int64 uid, const char *name, unsigned int index); bool (*setreg) (int64 uid, int val); bool (*setregstr) (int64 uid, const char *str); void (*load_num_db) (void); diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index cf509f027..59692a742 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -68,92 +68,144 @@ static char *mapreg_readregstr(int64 uid) } /** - * Modifies the value of an integer variable. + * Sets the value of a global integer variable. * - * @param uid variable's unique identifier - * @param val new value - * @retval true value was successfully set - */ -static bool mapreg_setreg(int64 uid, int val) + * @param uid The variable's unique ID. + * @param name The variable's name. + * @param index The variable's array index. + * @param value The variable's new value. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_set_num_db(int64 uid, const char *name, unsigned int index, int value) { - struct mapreg_save *m; - int num = script_getvarid(uid); - unsigned int i = script_getvaridx(uid); - const char* name = script->get_str(num); + nullpo_retr(false, name); + Assert_retr(false, *name != '\0'); + Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH); - nullpo_retr(true, name); - if( val != 0 ) { - if( (m = i64db_get(mapreg->regs.vars, uid)) ) { - m->u.i = val; - if(name[1] != '@') { - m->save = true; - mapreg->dirty = true; - } - } else { - if( i ) - script->array_update(&mapreg->regs, uid, false); + if (value == 0) + return mapreg->delete_num_db(uid, name, index); - m = ers_alloc(mapreg->ers, struct mapreg_save); + struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid); - m->u.i = val; - m->uid = uid; - m->save = false; - m->is_string = false; + // Update variable. + if (var != NULL) { + var->u.i = value; - if (name[1] != '@' && !mapreg->skip_insert) {// write new variable to database - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + if (script->is_permanent_variable(name)) { + var->save = true; + mapreg->dirty = true; + } - if (stmt == NULL) { - SqlStmt_ShowDebug(stmt); - } else { - const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)"; - size_t len = strnlen(name, SCRIPT_VARNAME_LENGTH); + return true; + } - if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) - || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len) - || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) - || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &val, sizeof(val)) - || SQL_ERROR == SQL->StmtExecute(stmt)) { - SqlStmt_ShowDebug(stmt); - } + // Add new variable. + if (index != 0) + script->array_update(&mapreg->regs, uid, false); - SQL->StmtFree(stmt); - } - } - i64db_put(mapreg->regs.vars, uid, m); + var = ers_alloc(mapreg->ers, struct mapreg_save); + var->u.i = value; + var->uid = uid; + var->save = false; + var->is_string = false; + i64db_put(mapreg->regs.vars, uid, var); + + if (script->is_permanent_variable(name) && !mapreg->skip_insert) { + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return false; } - } else { // val == 0 - if( i ) - script->array_update(&mapreg->regs, uid, true); - if( (m = i64db_get(mapreg->regs.vars, uid)) ) { - ers_free(mapreg->ers, m); + + const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_INT32, &value, sizeof(value)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return false; } - i64db_remove(mapreg->regs.vars, uid); - if( name[1] != '@' ) {// Remove from database because it is unused. - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + SQL->StmtFree(stmt); + } - if (stmt == NULL) { - SqlStmt_ShowDebug(stmt); - } else { - const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?"; - size_t len = strnlen(name, SCRIPT_VARNAME_LENGTH); + return true; +} - if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) - || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len) - || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) - || SQL_ERROR == SQL->StmtExecute(stmt)) { - SqlStmt_ShowDebug(stmt); - } +/** + * Deletes a global integer variable. + * + * @param uid The variable's unique ID. + * @param name The variable's name. + * @param index The variable's array index. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_delete_num_db(int64 uid, const char *name, unsigned int index) +{ + nullpo_retr(false, name); + Assert_retr(false, *name != '\0'); + Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH); - SQL->StmtFree(stmt); - } + struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid); + + if (var != NULL) + ers_free(mapreg->ers, var); + + if (index != 0) + script->array_update(&mapreg->regs, uid, true); + + i64db_remove(mapreg->regs.vars, uid); + + if (script->is_permanent_variable(name)) { + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return false; + } + + const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->num_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return false; } + + SQL->StmtFree(stmt); } return true; } +/** + * Sets the value of a global integer variable or deletes it if passed value is 0. + * + * @param uid The variable's unique ID. + * @param val The variable's new value. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_setreg(int64 uid, int val) +{ + unsigned int index = script_getvaridx(uid); + const char *name = script->get_str(script_getvarid(uid)); + + if (val != 0) + return mapreg->set_num_db(uid, name, index, val); + else + return mapreg->delete_num_db(uid, name, index); +} + /** * Modifies the value of a string variable. * @@ -602,6 +654,8 @@ void mapreg_defaults(void) /* */ mapreg->readreg = mapreg_readreg; mapreg->readregstr = mapreg_readregstr; + mapreg->set_num_db = mapreg_set_num_db; + mapreg->delete_num_db = mapreg_delete_num_db; mapreg->setreg = mapreg_setreg; mapreg->setregstr = mapreg_setregstr; mapreg->load_num_db = mapreg_load_num_db; -- cgit v1.2.3-70-g09d2 From 8b521e30ef2f373c11a49329da4e330ea588aed3 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Mon, 11 May 2020 05:32:18 +0200 Subject: Use separate functions for setting and deleting global string variables --- src/map/mapreg.h | 2 + src/map/mapreg_sql.c | 221 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 142 insertions(+), 81 deletions(-) (limited to 'src/map') diff --git a/src/map/mapreg.h b/src/map/mapreg.h index 70c8666fa..e461734eb 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -60,6 +60,8 @@ struct mapreg_interface { bool (*set_num_db) (int64 uid, const char *name, unsigned int index, int value); bool (*delete_num_db) (int64 uid, const char *name, unsigned int index); bool (*setreg) (int64 uid, int val); + bool (*set_str_db) (int64 uid, const char *name, unsigned int index, const char *value); + bool (*delete_str_db) (int64 uid, const char *name, unsigned int index); bool (*setregstr) (int64 uid, const char *str); void (*load_num_db) (void); void (*load_str_db) (void); diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index 59692a742..c6ebe33b7 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -207,97 +207,154 @@ static bool mapreg_setreg(int64 uid, int val) } /** - * Modifies the value of a string variable. + * Sets the value of a global string variable. * - * @param uid variable's unique identifier - * @param str new value - * @retval true value was successfully set - */ -static bool mapreg_setregstr(int64 uid, const char *str) + * @param uid The variable's unique ID. + * @param name The variable's name. + * @param index The variable's array index. + * @param value The variable's new value. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_set_str_db(int64 uid, const char *name, unsigned int index, const char *value) { - struct mapreg_save *m; - int num = script_getvarid(uid); - unsigned int i = script_getvaridx(uid); - const char* name = script->get_str(num); - - nullpo_retr(true, name); - - if( str == NULL || *str == 0 ) { - if( i ) - script->array_update(&mapreg->regs, uid, true); - if(name[1] != '@') { - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); - - if (stmt == NULL) { - SqlStmt_ShowDebug(stmt); - } else { - const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?"; - size_t len = strnlen(name, SCRIPT_VARNAME_LENGTH); - - if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) - || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len) - || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) - || SQL_ERROR == SQL->StmtExecute(stmt)) { - SqlStmt_ShowDebug(stmt); - } - - SQL->StmtFree(stmt); - } + nullpo_retr(false, name); + Assert_retr(false, *name != '\0'); + Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH); + + if (value == NULL || *value == '\0') + return mapreg->delete_str_db(uid, name, index); + + if (script->is_permanent_variable(name)) + Assert_retr(false, strlen(value) <= SCRIPT_STRING_VAR_LENGTH); + + struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid); + + // Update variable. + if (var != NULL) { + if (var->u.str != NULL) + aFree(var->u.str); + + var->u.str = aStrdup(value); + + if (script->is_permanent_variable(name)) { + var->save = true; + mapreg->dirty = true; } - if( (m = i64db_get(mapreg->regs.vars, uid)) ) { - if( m->u.str != NULL ) - aFree(m->u.str); - ers_free(mapreg->ers, m); + + return true; + } + + // Add new variable. + if (index != 0) + script->array_update(&mapreg->regs, uid, false); + + var = ers_alloc(mapreg->ers, struct mapreg_save); + var->u.str = aStrdup(value); + var->uid = uid; + var->save = false; + var->is_string = true; + i64db_put(mapreg->regs.vars, uid, var); + + if (script->is_permanent_variable(name) && !mapreg->skip_insert) { + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return false; } - i64db_remove(mapreg->regs.vars, uid); - } else { - if( (m = i64db_get(mapreg->regs.vars, uid)) ) { - if( m->u.str != NULL ) - aFree(m->u.str); - m->u.str = aStrdup(str); - if(name[1] != '@') { - mapreg->dirty = true; - m->save = true; - } - } else { - if( i ) - script->array_update(&mapreg->regs, uid, false); - - m = ers_alloc(mapreg->ers, struct mapreg_save); - - m->uid = uid; - m->u.str = aStrdup(str); - m->save = false; - m->is_string = true; - - if(name[1] != '@' && !mapreg->skip_insert) { //put returned null, so we must insert. - struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); - - if (stmt == NULL) { - SqlStmt_ShowDebug(stmt); - } else { - const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)"; - size_t len_n = strnlen(name, SCRIPT_VARNAME_LENGTH); - size_t len_v = strnlen(str, SCRIPT_STRING_VAR_LENGTH); - - if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) - || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, len_n) - || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &i, sizeof(i)) - || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_STRING, str, len_v) - || SQL_ERROR == SQL->StmtExecute(stmt)) { - SqlStmt_ShowDebug(stmt); - } - - SQL->StmtFree(stmt); - } - } - i64db_put(mapreg->regs.vars, uid, m); + + const char *query = "INSERT INTO `%s` (`key`, `index`, `value`) VALUES (?, ?, ?)"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 2, SQLDT_STRING, value, strlen(value)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return false; } + + SQL->StmtFree(stmt); } return true; } +/** + * Deletes a global string variable. + * + * @param uid The variable's unique ID. + * @param name The variable's name. + * @param index The variable's array index. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_delete_str_db(int64 uid, const char *name, unsigned int index) +{ + nullpo_retr(false, name); + Assert_retr(false, *name != '\0'); + Assert_retr(false, strlen(name) <= SCRIPT_VARNAME_LENGTH); + + struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid); + + if (var != NULL) { + if (var->u.str != NULL) + aFree(var->u.str); + + ers_free(mapreg->ers, var); + } + + if (index != 0) + script->array_update(&mapreg->regs, uid, true); + + i64db_remove(mapreg->regs.vars, uid); + + if (script->is_permanent_variable(name)) { + struct SqlStmt *stmt = SQL->StmtMalloc(map->mysql_handle); + + if (stmt == NULL) { + SqlStmt_ShowDebug(stmt); + return false; + } + + const char *query = "DELETE FROM `%s` WHERE `key`=? AND `index`=?"; + + if (SQL_ERROR == SQL->StmtPrepare(stmt, query, mapreg->str_db) + || SQL_ERROR == SQL->StmtBindParam(stmt, 0, SQLDT_STRING, name, strlen(name)) + || SQL_ERROR == SQL->StmtBindParam(stmt, 1, SQLDT_UINT32, &index, sizeof(index)) + || SQL_ERROR == SQL->StmtExecute(stmt)) { + SqlStmt_ShowDebug(stmt); + SQL->StmtFree(stmt); + return false; + } + + SQL->StmtFree(stmt); + } + + return true; +} + +/** + * Sets the value of a global string variable or deletes it if passed value is NULL or an empty string. + * + * @param uid The variable's unique ID. + * @param str The variable's new value. + * @return True on success, otherwise false. + * + **/ +static bool mapreg_setregstr(int64 uid, const char *str) +{ + unsigned int index = script_getvaridx(uid); + const char *name = script->get_str(script_getvarid(uid)); + + if (str != NULL && *str != '\0') + return mapreg->set_str_db(uid, name, index, str); + else + return mapreg->delete_str_db(uid, name, index); +} + /** * Loads permanent global interger variables from the database. * @@ -657,6 +714,8 @@ void mapreg_defaults(void) mapreg->set_num_db = mapreg_set_num_db; mapreg->delete_num_db = mapreg_delete_num_db; mapreg->setreg = mapreg_setreg; + mapreg->set_str_db = mapreg_set_str_db; + mapreg->delete_str_db = mapreg_delete_str_db; mapreg->setregstr = mapreg_setregstr; mapreg->load_num_db = mapreg_load_num_db; mapreg->load_str_db = mapreg_load_str_db; -- cgit v1.2.3-70-g09d2 From 30b7c9dee6c19568820b1e6042212bbda21e02e6 Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Mon, 11 May 2020 06:25:16 +0200 Subject: Apply code style to mapreg_sql.c --- src/map/mapreg.h | 2 + src/map/mapreg_sql.c | 194 +++++++++++++++++++++++++++------------------------ 2 files changed, 104 insertions(+), 92 deletions(-) (limited to 'src/map') diff --git a/src/map/mapreg.h b/src/map/mapreg.h index e461734eb..495d75102 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -29,6 +29,8 @@ struct config_setting_t; // common/conf.h struct eri; +#define MAPREG_AUTOSAVE_INTERVAL (300 * 1000) + /** Container for a mapreg value */ struct mapreg_save { int64 uid; ///< Unique ID diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c index c6ebe33b7..16d8b0ff6 100644 --- a/src/map/mapreg_sql.c +++ b/src/map/mapreg_sql.c @@ -22,7 +22,7 @@ #include "mapreg.h" -#include "map/map.h" // map-"mysql_handle +#include "map/map.h" #include "map/script.h" #include "common/cbasetypes.h" #include "common/conf.h" @@ -38,33 +38,33 @@ #include #include -static struct mapreg_interface mapreg_s; -struct mapreg_interface *mapreg; - -#define MAPREG_AUTOSAVE_INTERVAL (300*1000) +static struct mapreg_interface mapreg_s; //!< Private interface structure. +struct mapreg_interface *mapreg; //!< Public interface structure. /** - * Looks up the value of an integer variable using its uid. + * Looks up the value of a global integer variable using its unique ID. * - * @param uid variable's unique identifier. - * @return variable's integer value - */ -static int mapreg_readreg(int64 uid) + * @param uid The variable's unique ID. + * @return The variable's value or 0 if the variable does not exist. + * + **/ +static int mapreg_get_num_reg(int64 uid) { - struct mapreg_save *m = i64db_get(mapreg->regs.vars, uid); - return m?m->u.i:0; + struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid); + return (var != NULL) ? var->u.i : 0; } /** - * Looks up the value of a string variable using its uid. + * Looks up the value of a global string variable using its unique ID. * - * @param uid variable's unique identifier - * @return variable's string value - */ -static char *mapreg_readregstr(int64 uid) + * @param uid The variable's unique ID. + * @return The variable's value or NULL if the variable does not exist. + * + **/ +static char *mapreg_get_str_reg(int64 uid) { - struct mapreg_save *m = i64db_get(mapreg->regs.vars, uid); - return m?m->u.str:NULL; + struct mapreg_save *var = i64db_get(mapreg->regs.vars, uid); + return (var != NULL) ? var->u.str : NULL; } /** @@ -195,7 +195,7 @@ static bool mapreg_delete_num_db(int64 uid, const char *name, unsigned int index * @return True on success, otherwise false. * **/ -static bool mapreg_setreg(int64 uid, int val) +static bool mapreg_set_num(int64 uid, int val) { unsigned int index = script_getvaridx(uid); const char *name = script->get_str(script_getvarid(uid)); @@ -344,7 +344,7 @@ static bool mapreg_delete_str_db(int64 uid, const char *name, unsigned int index * @return True on success, otherwise false. * **/ -static bool mapreg_setregstr(int64 uid, const char *str) +static bool mapreg_set_str(int64 uid, const char *str) { unsigned int index = script_getvaridx(uid); const char *name = script->get_str(script_getvarid(uid)); @@ -458,9 +458,10 @@ static void mapreg_load_str_db(void) } /** - * Loads permanent variables from database. - */ -static void script_load_mapreg(void) + * Loads permanent global variables from the database. + * + **/ +static void mapreg_load(void) { mapreg->load_num_db(); mapreg->load_str_db(); @@ -539,80 +540,94 @@ static void mapreg_save_str_db(const char *name, unsigned int index, const char } /** - * Saves permanent variables to database. - */ -static void script_save_mapreg(void) + * Saves permanent global variables to the database. + * + **/ +static void mapreg_save(void) { if (mapreg->dirty) { struct DBIterator *iter = db_iterator(mapreg->regs.vars); - struct mapreg_save *m = NULL; - for (m = dbi_first(iter); dbi_exists(iter); m = dbi_next(iter)) { - if (m->save) { - int num = script_getvarid(m->uid); - int i = script_getvaridx(m->uid); - const char* name = script->get_str(num); - nullpo_retv(name); - - if (!m->is_string) - mapreg->save_num_db(name, i, m->u.i); + struct mapreg_save *var = NULL; + + for (var = dbi_first(iter); dbi_exists(iter); var = dbi_next(iter)) { + if (var->save) { + int index = script_getvaridx(var->uid); + const char *name = script->get_str(script_getvarid(var->uid)); + + if (!var->is_string) + mapreg->save_num_db(name, index, var->u.i); else - mapreg->save_str_db(name, i, m->u.str); + mapreg->save_str_db(name, index, var->u.str); - m->save = false; + var->save = false; } } + dbi_destroy(iter); mapreg->dirty = false; } } /** - * Timer event to auto-save permanent variables. + * Timer event to auto-save permanent global variables. * - * @see timer->do_timer - */ -static int script_autosave_mapreg(int tid, int64 tick, int id, intptr_t data) + * @see timer->do_timer() + * + * @param tid Unused. + * @param tick Unused. + * @param id Unused. + * @param data Unused. + * @return Always 0. + * + **/ +static int mapreg_save_timer(int tid, int64 tick, int id, intptr_t data) { mapreg->save(); return 0; } /** - * Destroys a mapreg_save structure, freeing the contained string, if any. + * Destroys a mapreg_save structure and frees the contained string, if any. * * @see DBApply - */ -static int mapreg_destroyreg(union DBKey key, struct DBData *data, va_list ap) + * + * @param key Unused. + * @param data The DB data holding the mapreg_save data. + * @param ap Unused. + * @return 0 on success, otherwise 1. + * + **/ +static int mapreg_destroy_reg(union DBKey key, struct DBData *data, va_list ap) { - struct mapreg_save *m = NULL; + nullpo_retr(1, data); if (data->type != DB_DATA_PTR) // Sanity check - return 0; + return 1; - m = DB->data2ptr(data); + struct mapreg_save *var = DB->data2ptr(data); - if (m->is_string) { - if (m->u.str) - aFree(m->u.str); - } - ers_free(mapreg->ers, m); + if (var == NULL) + return 1; + + if (var->is_string && var->u.str != NULL) + aFree(var->u.str); + ers_free(mapreg->ers, var); return 0; } /** - * Reloads mapregs, saving to database beforehand. + * Reloads permanent global variables, saving them to the database beforehand. * - * This has the effect of clearing the temporary variables, and - * reloading the permanent ones. - */ + * This has the effect of clearing the temporary global variables and reloading the permanent ones. + * + **/ static void mapreg_reload(void) { mapreg->save(); - mapreg->regs.vars->clear(mapreg->regs.vars, mapreg->destroyreg); - if( mapreg->regs.arrays ) { + if (mapreg->regs.arrays != NULL) { mapreg->regs.arrays->destroy(mapreg->regs.arrays, script->array_free_db); mapreg->regs.arrays = NULL; } @@ -657,74 +672,69 @@ static bool mapreg_config_read_registry(const char *filename, const struct confi } /** - * Finalizer. - */ + * Saves permanent global variables to the database and frees all the memory they use afterwards. + * + **/ static void mapreg_final(void) { mapreg->save(); - mapreg->regs.vars->destroy(mapreg->regs.vars, mapreg->destroyreg); - ers_destroy(mapreg->ers); - if( mapreg->regs.arrays ) + if (mapreg->regs.arrays != NULL) mapreg->regs.arrays->destroy(mapreg->regs.arrays, script->array_free_db); } /** - * Initializer. - */ + * Allocates memory for permanent global variables, loads them from the database and initializes the auto-save timer. + * + **/ static void mapreg_init(void) { mapreg->regs.vars = i64db_alloc(DB_OPT_BASE); mapreg->ers = ers_new(sizeof(struct mapreg_save), "mapreg_sql.c::mapreg_ers", ERS_OPT_CLEAN); - mapreg->load(); - - timer->add_func_list(mapreg->save_timer, "mapreg_script_autosave_mapreg"); + timer->add_func_list(mapreg->save_timer, "mapreg_save_timer"); timer->add_interval(timer->gettick() + MAPREG_AUTOSAVE_INTERVAL, mapreg->save_timer, 0, 0, MAPREG_AUTOSAVE_INTERVAL); } /** - * Interface defaults initializer. - */ + * Initializes the mapreg interface defaults. + * + **/ void mapreg_defaults(void) { + /** Interface structure. **/ mapreg = &mapreg_s; - /* */ - mapreg->regs.vars = NULL; + /** Interface variables. **/ mapreg->ers = NULL; + mapreg->regs.vars = NULL; + mapreg->regs.arrays = NULL; + mapreg->dirty = false; mapreg->skip_insert = false; - safestrncpy(mapreg->num_db, "map_reg_num_db", sizeof(mapreg->num_db)); safestrncpy(mapreg->str_db, "map_reg_str_db", sizeof(mapreg->str_db)); - mapreg->dirty = false; - /* */ - mapreg->regs.arrays = NULL; - - /* */ - mapreg->init = mapreg_init; - mapreg->final = mapreg_final; - - /* */ - mapreg->readreg = mapreg_readreg; - mapreg->readregstr = mapreg_readregstr; + /** Interface functions. **/ + mapreg->readreg = mapreg_get_num_reg; + mapreg->readregstr = mapreg_get_str_reg; mapreg->set_num_db = mapreg_set_num_db; mapreg->delete_num_db = mapreg_delete_num_db; - mapreg->setreg = mapreg_setreg; + mapreg->setreg = mapreg_set_num; mapreg->set_str_db = mapreg_set_str_db; mapreg->delete_str_db = mapreg_delete_str_db; - mapreg->setregstr = mapreg_setregstr; + mapreg->setregstr = mapreg_set_str; mapreg->load_num_db = mapreg_load_num_db; mapreg->load_str_db = mapreg_load_str_db; - mapreg->load = script_load_mapreg; + mapreg->load = mapreg_load; mapreg->save_num_db = mapreg_save_num_db; mapreg->save_str_db = mapreg_save_str_db; - mapreg->save = script_save_mapreg; - mapreg->save_timer = script_autosave_mapreg; - mapreg->destroyreg = mapreg_destroyreg; + mapreg->save = mapreg_save; + mapreg->save_timer = mapreg_save_timer; + mapreg->destroyreg = mapreg_destroy_reg; mapreg->reload = mapreg_reload; mapreg->config_read_registry = mapreg_config_read_registry; + mapreg->final = mapreg_final; + mapreg->init = mapreg_init; } -- cgit v1.2.3-70-g09d2 From feaae9bc9244ee3c3f8968980b17c69d0ccd710f Mon Sep 17 00:00:00 2001 From: Kenpachi Developer Date: Tue, 12 May 2020 03:44:00 +0200 Subject: Apply code style to mapreg.h --- src/map/mapreg.h | 58 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'src/map') diff --git a/src/map/mapreg.h b/src/map/mapreg.h index 495d75102..4318eaea8 100644 --- a/src/map/mapreg.h +++ b/src/map/mapreg.h @@ -21,44 +21,42 @@ #ifndef MAP_MAPREG_H #define MAP_MAPREG_H -#include "map/script.h" // struct reg_db -#include "common/hercules.h" +#include "map/script.h" #include "common/db.h" +#include "common/hercules.h" -/* Forward Declarations */ -struct config_setting_t; // common/conf.h +/** Forward Declarations **/ +struct config_setting_t; struct eri; -#define MAPREG_AUTOSAVE_INTERVAL (300 * 1000) +#ifndef MAPREG_AUTOSAVE_INTERVAL +#define MAPREG_AUTOSAVE_INTERVAL (300 * 1000) //!< Interval for auto-saving permanent global variables to the database in milliseconds. +#endif /** MAPREG_AUTOSAVE_INTERVAL **/ -/** Container for a mapreg value */ +/** Global variable structure. **/ struct mapreg_save { - int64 uid; ///< Unique ID - union { - int i; ///< Numeric value - char *str; ///< String value + int64 uid; //!< The variable's unique ID. + union value { //!< The variable's value container. + int i; //!< The variable's integer value. + char *str; //!< The variable's string value. } u; - bool is_string; ///< true if it's a string, false if it's a number - bool save; ///< Whether a save operation is pending + bool is_string; //!< Whether the variable's value is a string. + bool save; //!< Whether the variable's save operation is pending. }; +/** The mapreg interface structure. **/ struct mapreg_interface { - struct reg_db regs; - /* */ - bool skip_insert; - /* */ - struct eri *ers; //[Ind/Hercules] - /* */ - char num_db[32]; //!< Name of SQL table which holds permanent global integer variables. - char str_db[32]; //!< Name of SQL table which holds permanent global string variables. - /* */ - bool dirty; ///< Whether there are modified regs to be saved - /* */ - void (*init) (void); - void (*final) (void); - /* */ + /** Interface variables. **/ + struct eri *ers; //!< Entry manager for global variables. + struct reg_db regs; //!< Generic database for global variables. + bool dirty; //!< Whether there are modified global variables to be saved. + bool skip_insert; //!< Whether to skip inserting the variable into the SQL database in mapreg_set_*_db(). + char num_db[32]; //!< Name of SQL table which holds permanent global integer variables. + char str_db[32]; //!< Name of SQL table which holds permanent global string variables. + + /** Interface functions. **/ int (*readreg) (int64 uid); - char* (*readregstr) (int64 uid); + char *(*readregstr) (int64 uid); bool (*set_num_db) (int64 uid, const char *name, unsigned int index, int value); bool (*delete_num_db) (int64 uid, const char *name, unsigned int index); bool (*setreg) (int64 uid, int val); @@ -75,12 +73,14 @@ struct mapreg_interface { int (*destroyreg) (union DBKey key, struct DBData *data, va_list ap); void (*reload) (void); bool (*config_read_registry) (const char *filename, const struct config_setting_t *config, bool imported); + void (*final) (void); + void (*init) (void); }; #ifdef HERCULES_CORE void mapreg_defaults(void); -#endif // HERCULES_CORE +#endif /** HERCULES_CORE **/ HPShared struct mapreg_interface *mapreg; -#endif /* MAP_MAPREG_H */ +#endif /** MAP_MAPREG_H **/ -- cgit v1.2.3-70-g09d2