Added int64 support to the script engine (#4522)

Added support for signed 64 bit integer value computation into the script engine.
This is required because newer official features require support for bigger numbers inside the scripts.

This also cleans up a lot of messy code and fixes a few issues like script stack corruptions.

Thanks to @aleos89 and everyone who supported me with this.
This commit is contained in:
Lemongrass3110
2020-01-13 14:44:48 +01:00
committed by GitHub
parent 3142863a13
commit e72c7360cf
41 changed files with 948 additions and 683 deletions

View File

@@ -34,7 +34,7 @@ struct reg_db regs;
* @param uid: variable's unique identifier.
* @return: variable's integer value
*/
int mapreg_readreg(int64 uid)
int64 mapreg_readreg(int64 uid)
{
struct mapreg_save *m = (struct mapreg_save *)i64db_get(regs.vars, uid);
return m ? m->u.i : 0;
@@ -59,11 +59,11 @@ char* mapreg_readregstr(int64 uid)
* @param val new value
* @return: true value was successfully set
*/
bool mapreg_setreg(int64 uid, int val)
bool mapreg_setreg(int64 uid, int64 val)
{
struct mapreg_save *m;
int num = script_getvarid(uid);
unsigned int i = script_getvaridx(uid);
uint32 i = script_getvaridx(uid);
const char* name = get_str(num);
if (val != 0) {
@@ -87,7 +87,7 @@ bool mapreg_setreg(int64 uid, int val)
if (name[1] != '@' && !skip_insert) {// write new variable to database
char esc_name[32 * 2 + 1];
Sql_EscapeStringLen(mmysql_handle, esc_name, name, strnlen(name, 32));
if (SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%d','%d')", mapreg_table, esc_name, i, val))
if (SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%" PRIu32 "','%" PRId64 "')", mapreg_table, esc_name, i, val))
Sql_ShowDebug(mmysql_handle);
}
i64db_put(regs.vars, uid, m);
@@ -103,7 +103,7 @@ bool mapreg_setreg(int64 uid, int val)
if (name[1] != '@') {// Remove from database because it is unused.
char esc_name[32 * 2 + 1];
Sql_EscapeStringLen(mmysql_handle, esc_name, name, strnlen(name, 32));
if (SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%d'", mapreg_table, esc_name, i))
if (SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%" PRIu32 "'", mapreg_table, esc_name, i))
Sql_ShowDebug(mmysql_handle);
}
}
@@ -122,7 +122,7 @@ bool mapreg_setregstr(int64 uid, const char* str)
{
struct mapreg_save *m;
int num = script_getvarid(uid);
unsigned int i = script_getvaridx(uid);
uint32 i = script_getvaridx(uid);
const char* name = get_str(num);
if (str == NULL || *str == 0) {
@@ -131,7 +131,7 @@ bool mapreg_setregstr(int64 uid, const char* str)
if (name[1] != '@') {
char esc_name[32 * 2 + 1];
Sql_EscapeStringLen(mmysql_handle, esc_name, name, strnlen(name, 32));
if (SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%d'", mapreg_table, esc_name, i))
if (SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%" PRIu32 "'", mapreg_table, esc_name, i))
Sql_ShowDebug(mmysql_handle);
}
if ((m = static_cast<mapreg_save *>(i64db_get(regs.vars, uid)))) {
@@ -165,7 +165,7 @@ bool mapreg_setregstr(int64 uid, const char* str)
char esc_str[255 * 2 + 1];
Sql_EscapeStringLen(mmysql_handle, esc_name, name, strnlen(name, 32));
Sql_EscapeStringLen(mmysql_handle, esc_str, str, strnlen(str, 255));
if (SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%d','%s')", mapreg_table, esc_name, i, esc_str))
if (SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%" PRIu32 "','%s')", mapreg_table, esc_name, i, esc_str))
Sql_ShowDebug(mmysql_handle);
}
i64db_put(regs.vars, uid, m);
@@ -188,7 +188,7 @@ static void script_load_mapreg(void)
*/
SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
char varname[32+1];
int index;
uint32 index;
char value[255+1];
uint32 length;
@@ -203,21 +203,21 @@ static void script_load_mapreg(void)
skip_insert = true;
SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], sizeof(varname), &length, NULL);
SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
SqlStmt_BindColumn(stmt, 1, SQLDT_UINT32, &index, 0, NULL, NULL);
SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], sizeof(value), NULL, NULL);
while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) ) {
int s = add_str(varname);
int i = index;
int64 uid = reference_uid(s, index);
if( i64db_exists(regs.vars, reference_uid(s, i)) ) {
if( i64db_exists(regs.vars, uid) ) {
ShowWarning("load_mapreg: duplicate! '%s' => '%s' skipping...\n",varname,value);
continue;
}
if( varname[length-1] == '$' ) {
mapreg_setregstr(reference_uid(s, i), value);
mapreg_setregstr(uid, value);
} else {
mapreg_setreg(reference_uid(s, i), atoi(value));
mapreg_setreg(uid, strtoll(value,NULL,10));
}
}
@@ -238,19 +238,19 @@ static void script_save_mapreg(void)
for (m = static_cast<mapreg_save *>(dbi_first(iter)); dbi_exists(iter); m = static_cast<mapreg_save *>(dbi_next(iter))) {
if (m->save) {
int num = script_getvarid(m->uid);
int i = script_getvaridx(m->uid);
uint32 i = script_getvaridx(m->uid);
const char* name = get_str(num);
if (!m->is_string) {
char esc_name[32 * 2 + 1];
Sql_EscapeStringLen(mmysql_handle, esc_name, name, strnlen(name, 32));
if (SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%d' WHERE `varname`='%s' AND `index`='%d' LIMIT 1", mapreg_table, m->u.i, esc_name, i))
if (SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%" PRId64 "' WHERE `varname`='%s' AND `index`='%" PRIu32 "' LIMIT 1", mapreg_table, m->u.i, esc_name, i))
Sql_ShowDebug(mmysql_handle);
} else {
char esc_str[2 * 255 + 1];
char esc_name[32 * 2 + 1];
Sql_EscapeStringLen(mmysql_handle, esc_name, name, strnlen(name, 32));
Sql_EscapeStringLen(mmysql_handle, esc_str, m->u.str, safestrnlen(m->u.str, 255));
if (SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%s' WHERE `varname`='%s' AND `index`='%d' LIMIT 1", mapreg_table, esc_str, esc_name, i))
if (SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `value`='%s' WHERE `varname`='%s' AND `index`='%" PRIu32 "' LIMIT 1", mapreg_table, esc_str, esc_name, i))
Sql_ShowDebug(mmysql_handle);
}
m->save = false;