summaryrefslogtreecommitdiff
path: root/src/map/mapreg_sql.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/map/mapreg_sql.c')
-rw-r--r--src/map/mapreg_sql.c99
1 files changed, 57 insertions, 42 deletions
diff --git a/src/map/mapreg_sql.c b/src/map/mapreg_sql.c
index 6a13ef2a0..6df614aac 100644
--- a/src/map/mapreg_sql.c
+++ b/src/map/mapreg_sql.c
@@ -21,51 +21,56 @@ struct mapreg_interface mapreg_s;
#define MAPREG_AUTOSAVE_INTERVAL (300*1000)
/// Looks up the value of an integer variable using its uid.
-int mapreg_readreg(int uid) {
- struct mapreg_save *m = idb_get(mapreg->db, uid);
+int mapreg_readreg(int64 uid) {
+ struct mapreg_save *m = i64db_get(mapreg->db, uid);
return m?m->u.i:0;
}
/// Looks up the value of a string variable using its uid.
-char* mapreg_readregstr(int uid) {
- struct mapreg_save *m = idb_get(mapreg->str_db, uid);
+char* mapreg_readregstr(int64 uid) {
+ struct mapreg_save *m = i64db_get(mapreg->str_db, uid);
return m?m->u.str:NULL;
}
/// Modifies the value of an integer variable.
-bool mapreg_setreg(int uid, int val) {
+bool mapreg_setreg(int64 uid, int val) {
struct mapreg_save *m;
- int num = (uid & 0x00ffffff);
- int i = (uid & 0xff000000) >> 24;
+ int num = script_getvarid(uid);
+ unsigned int i = script_getvaridx(uid);
const char* name = script->get_str(num);
if( val != 0 ) {
- if( (m = idb_get(mapreg->db,uid)) ) {
+ if( (m = i64db_get(mapreg->db,uid)) ) {
m->u.i = val;
if(name[1] != '@') {
m->save = true;
mapreg->i_dirty = true;
}
} else {
+ if( i )
+ script->array_update(&mapreg->array_db,uid,false);
+
m = ers_alloc(mapreg->ers, struct mapreg_save);
m->u.i = val;
m->uid = uid;
m->save = false;
- if(name[1] != '@') {// write new variable to database
+ if(name[1] != '@' && !mapreg->skip_insert) {// write new variable to database
char tmp_str[32*2+1];
SQL->EscapeStringLen(map->mysql_handle, tmp_str, name, strnlen(name, 32));
if( SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%d','%d')", mapreg->table, tmp_str, i, val) )
Sql_ShowDebug(map->mysql_handle);
}
- idb_put(mapreg->db, uid, m);
+ i64db_put(mapreg->db, uid, m);
}
} else { // val == 0
- if( (m = idb_get(mapreg->db,uid)) ) {
+ if( i )
+ script->array_update(&mapreg->array_db,uid,true);
+ if( (m = i64db_get(mapreg->db,uid)) ) {
ers_free(mapreg->ers, m);
}
- idb_remove(mapreg->db,uid);
+ i64db_remove(mapreg->db,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`='%d'", mapreg->table, name, i) )
@@ -77,25 +82,27 @@ bool mapreg_setreg(int uid, int val) {
}
/// Modifies the value of a string variable.
-bool mapreg_setregstr(int uid, const char* str) {
+bool mapreg_setregstr(int64 uid, const char* str) {
struct mapreg_save *m;
- int num = (uid & 0x00ffffff);
- int i = (uid & 0xff000000) >> 24;
+ int num = script_getvarid(uid);
+ unsigned int i = script_getvaridx(uid);
const char* name = script->get_str(num);
if( str == NULL || *str == 0 ) {
+ if( i )
+ script->array_update(&mapreg->array_db,uid,true);
if(name[1] != '@') {
if( SQL_ERROR == SQL->Query(map->mysql_handle, "DELETE FROM `%s` WHERE `varname`='%s' AND `index`='%d'", mapreg->table, name, i) )
Sql_ShowDebug(map->mysql_handle);
}
- if( (m = idb_get(mapreg->str_db,uid)) ) {
+ if( (m = i64db_get(mapreg->str_db,uid)) ) {
if( m->u.str != NULL )
aFree(m->u.str);
ers_free(mapreg->ers, m);
}
- idb_remove(mapreg->str_db,uid);
+ i64db_remove(mapreg->str_db,uid);
} else {
- if( (m = idb_get(mapreg->str_db,uid)) ) {
+ if( (m = i64db_get(mapreg->str_db,uid)) ) {
if( m->u.str != NULL )
aFree(m->u.str);
m->u.str = aStrdup(str);
@@ -104,13 +111,16 @@ bool mapreg_setregstr(int uid, const char* str) {
m->save = true;
}
} else {
+ if( i )
+ script->array_update(&mapreg->array_db,uid,false);
+
m = ers_alloc(mapreg->ers, struct mapreg_save);
m->uid = uid;
m->u.str = aStrdup(str);
m->save = false;
- if(name[1] != '@') { //put returned null, so we must insert.
+ if(name[1] != '@' && !mapreg->skip_insert) { //put returned null, so we must insert.
char tmp_str[32*2+1];
char tmp_str2[255*2+1];
SQL->EscapeStringLen(map->mysql_handle, tmp_str, name, strnlen(name, 32));
@@ -118,7 +128,7 @@ bool mapreg_setregstr(int uid, const char* str) {
if( SQL_ERROR == SQL->Query(map->mysql_handle, "INSERT INTO `%s`(`varname`,`index`,`value`) VALUES ('%s','%d','%s')", mapreg->table, tmp_str, i, tmp_str2) )
Sql_ShowDebug(map->mysql_handle);
}
- idb_put(mapreg->str_db, uid, m);
+ i64db_put(mapreg->str_db, uid, m);
}
}
@@ -147,41 +157,36 @@ void script_load_mapreg(void) {
return;
}
+ mapreg->skip_insert = true;
+
SQL->StmtBindColumn(stmt, 0, SQLDT_STRING, &varname[0], sizeof(varname), &length, NULL);
SQL->StmtBindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
SQL->StmtBindColumn(stmt, 2, SQLDT_STRING, &value[0], sizeof(value), NULL, NULL);
-
+
while ( SQL_SUCCESS == SQL->StmtNextRow(stmt) ) {
- struct mapreg_save *m = NULL;
int s = script->add_str(varname);
int i = index;
+
if( varname[length-1] == '$' ) {
- if( idb_exists(mapreg->str_db, (i<<24)|s) ) {
+ if( i64db_exists(mapreg->str_db, reference_uid(s, i)) ) {
ShowWarning("load_mapreg: duplicate! '%s' => '%s' skipping...\n",varname,value);
continue;
}
+ script->set_reg(NULL,NULL,reference_uid(s, i), varname, (void*)value, NULL);
} else {
- if( idb_exists(mapreg->db, (i<<24)|s) ) {
+ if( i64db_exists(mapreg->db, reference_uid(s, i)) ) {
ShowWarning("load_mapreg: duplicate! '%s' => '%s' skipping...\n",varname,value);
continue;
}
- }
-
- m = ers_alloc(mapreg->ers, struct mapreg_save);
- m->uid = (i<<24)|s;
- m->save = false;
- if( varname[length-1] == '$' ) {
- m->u.str = aStrdup(value);
- idb_put(mapreg->str_db, m->uid, m);
- } else {
- m->u.i = atoi(value);
- idb_put(mapreg->db, m->uid, m);
+ script->set_reg(NULL,NULL,reference_uid(s, i), varname, (void*)__64BPTRSIZE(atoi(value)), NULL);
}
}
SQL->StmtFree(stmt);
+ mapreg->skip_insert = false;
+
mapreg->i_dirty = false;
mapreg->str_dirty = false;
}
@@ -195,8 +200,8 @@ void script_save_mapreg(void) {
iter = db_iterator(mapreg->db);
for( m = dbi_first(iter); dbi_exists(iter); m = dbi_next(iter) ) {
if( m->save ) {
- int num = (m->uid & 0x00ffffff);
- int i = (m->uid & 0xff000000) >> 24;
+ int num = script_getvarid(m->uid);
+ int i = script_getvaridx(m->uid);
const char* name = script->get_str(num);
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) )
@@ -212,8 +217,8 @@ void script_save_mapreg(void) {
iter = db_iterator(mapreg->str_db);
for( m = dbi_first(iter); dbi_exists(iter); m = dbi_next(iter) ) {
if( m->save ) {
- int num = (m->uid & 0x00ffffff);
- int i = (m->uid & 0xff000000) >> 24;
+ int num = script_getvarid(m->uid);
+ int i = script_getvaridx(m->uid);
const char* name = script->get_str(num);
char tmp_str2[2*255+1];
@@ -257,7 +262,10 @@ void mapreg_reload(void) {
db_clear(mapreg->db);
db_clear(mapreg->str_db);
-
+
+ if( mapreg->array_db )
+ mapreg->array_db->destroy(mapreg->array_db,script->array_free_db);
+
mapreg->load();
}
@@ -286,11 +294,14 @@ void mapreg_final(void) {
db_destroy(mapreg->str_db);
ers_destroy(mapreg->ers);
+
+ if( mapreg->array_db )
+ mapreg->array_db->destroy(mapreg->array_db,script->array_free_db);
}
void mapreg_init(void) {
- mapreg->db = idb_alloc(DB_OPT_BASE);
- mapreg->str_db = idb_alloc(DB_OPT_BASE);
+ mapreg->db = i64db_alloc(DB_OPT_BASE);
+ mapreg->str_db = i64db_alloc(DB_OPT_BASE);
mapreg->ers = ers_new(sizeof(struct mapreg_save), "mapreg_sql.c::mapreg_ers", ERS_OPT_CLEAN);
mapreg->load();
@@ -314,12 +325,16 @@ void mapreg_defaults(void) {
mapreg->db = NULL;
mapreg->str_db = NULL;
mapreg->ers = NULL;
+ mapreg->skip_insert = false;
safestrncpy(mapreg->table, "mapreg", sizeof(mapreg->table));
mapreg->i_dirty = false;
mapreg->str_dirty = false;
/* */
+ mapreg->array_db = NULL;
+
+ /* */
mapreg->init = mapreg_init;
mapreg->final = mapreg_final;