diff options
Diffstat (limited to 'src/char/int_storage.c')
-rw-r--r-- | src/char/int_storage.c | 92 |
1 files changed, 26 insertions, 66 deletions
diff --git a/src/char/int_storage.c b/src/char/int_storage.c index 5f005005e..65301127f 100644 --- a/src/char/int_storage.c +++ b/src/char/int_storage.c @@ -41,31 +41,33 @@ struct inter_storage_interface inter_storage_s; struct inter_storage_interface *inter_storage; /// Save storage data to sql -int inter_storage_tosql(int account_id, struct storage_data *cp, const struct storage_data *p) +int inter_storage_tosql(int account_id, const struct storage_data *p) { int i = 0, j = 0; - bool updated_p[MAX_STORAGE] = { false }; + bool matched_p[MAX_STORAGE] = { false }; int delete[MAX_STORAGE] = { 0 }; int total_deletes = 0, total_updates = 0, total_inserts = 0; int total_changes = 0; + struct storage_data cp = { 0 }; StringBuf buf; - nullpo_ret(cp); nullpo_ret(p); + VECTOR_INIT(cp.item); + inter_storage->fromsql(account_id, &cp); + StrBuf->Init(&buf); - - if (VECTOR_LENGTH(cp->item) > 0) { + if (VECTOR_LENGTH(cp.item) > 0) { /** * Compare and update items, if any. */ - for (i = 0; i < VECTOR_LENGTH(cp->item); i++) { - struct item *cp_it = &VECTOR_INDEX(cp->item, i); + for (i = 0; i < VECTOR_LENGTH(cp.item); i++) { + struct item *cp_it = &VECTOR_INDEX(cp.item, i); struct item *p_it = NULL; ARR_FIND(0, VECTOR_LENGTH(p->item), j, - updated_p[j] != true + matched_p[j] != true && (p_it = &VECTOR_INDEX(p->item, j)) != NULL && cp_it->nameid == p_it->nameid && cp_it->unique_id == p_it->unique_id @@ -84,17 +86,17 @@ int inter_storage_tosql(int account_id, struct storage_data *cp, const struct st StrBuf->AppendStr(&buf, ", `expire_time`, `bound`, `unique_id`) VALUES"); } - StrBuf->Printf(&buf, "('%d', '%d', '%d', '%d', '%u', '%d', '%d', '%d'", - cp_it->id, account_id, p_it->nameid, p_it->amount, p_it->equip, p_it->identify, p_it->refine, p_it->attribute); + StrBuf->Printf(&buf, "%s('%d', '%d', '%d', '%d', '%u', '%d', '%d', '%d'", + total_updates > 0 ? ", " : "", cp_it->id, account_id, p_it->nameid, p_it->amount, p_it->equip, p_it->identify, p_it->refine, p_it->attribute); for (k = 0; k < MAX_SLOTS; k++) StrBuf->Printf(&buf, ", '%d'", p_it->card[k]); for (k = 0; k < MAX_ITEM_OPTIONS; ++k) StrBuf->Printf(&buf, ", '%d', '%d'", p_it->option[k].index, p_it->option[k].value); - StrBuf->Printf(&buf, ", '%u', '%d', '%"PRIu64"')%s", p_it->expire_time, p_it->bound, p_it->unique_id, total_updates > 0 ? ", " : ""); + StrBuf->Printf(&buf, ", '%u', '%d', '%"PRIu64"')", p_it->expire_time, p_it->bound, p_it->unique_id); total_updates++; - updated_p[j] = true; } + matched_p[j] = true; } else { // Does not exist, so set for deletion. delete[total_deletes++] = cp_it->id; } @@ -125,7 +127,7 @@ int inter_storage_tosql(int account_id, struct storage_data *cp, const struct st for (i = 0; i < VECTOR_LENGTH(p->item); i++) { struct item *p_it = &VECTOR_INDEX(p->item, i); - if (p_it->id != 0) + if (matched_p[i]) continue; // Item is not a new entry, so lets continue. // Store the remaining items. @@ -140,8 +142,8 @@ int inter_storage_tosql(int account_id, struct storage_data *cp, const struct st } StrBuf->Printf(&buf, "%s('%d', '%d', '%d', '%u', '%d', '%d', '%d', '%u', '%d', '%"PRIu64"'", - total_inserts > 0 ? ", " : "", account_id, p_it->nameid, p_it->amount, p_it->equip, p_it->identify, p_it->refine, - p_it->attribute, p_it->expire_time, p_it->bound, p_it->unique_id); + total_inserts > 0 ? ", " : "", account_id, p_it->nameid, p_it->amount, p_it->equip, p_it->identify, p_it->refine, + p_it->attribute, p_it->expire_time, p_it->bound, p_it->unique_id); for (j = 0; j < MAX_SLOTS; ++j) StrBuf->Printf(&buf, ", '%d'", p_it->card[j]); for (j = 0; j < MAX_ITEM_OPTIONS; ++j) @@ -155,15 +157,10 @@ int inter_storage_tosql(int account_id, struct storage_data *cp, const struct st Sql_ShowDebug(inter->sql_handle); StrBuf->Destroy(&buf); - - /* re-sync loaded data with current table data. */ - VECTOR_CLEAR(cp->item); - inter_storage->fromsql(account_id, cp); + VECTOR_CLEAR(cp.item); total_changes = total_inserts + total_updates + total_deletes; - ShowInfo("storage save complete - id: %d (total: %d)\n", account_id, total_changes); - return total_changes; } @@ -305,49 +302,15 @@ int inter_storage_guild_storage_fromsql(int guild_id, struct guild_storage* p) return 0; } -/** - * Ensures storage data entity for a character. - * @see DBCreateData - */ -static struct DBData inter_storage_ensure_account_storage(union DBKey key, va_list args) -{ - struct storage_data *stor = NULL; - - CREATE(stor, struct storage_data, 1); - - stor->save = false; - stor->aggregate = 0; - - VECTOR_INIT(stor->item); - - return DB->ptr2data(stor); -} - -/** - * Cleaning function called through db_destroy() for vectors in storage_data. - */ -int inter_storage_clear_account_storage(union DBKey key, struct DBData *data, va_list args) -{ - struct storage_data *stor = DB->data2ptr(data); - - VECTOR_CLEAR(stor->item); - - return 0; -} - //--------------------------------------------------------- // storage data initialize int inter_storage_sql_init(void) { - inter_storage->account_storage = idb_alloc(DB_OPT_RELEASE_DATA); - return 1; } // storage data finalize void inter_storage_sql_final(void) { - inter_storage->account_storage->destroy(inter_storage->account_storage, inter_storage->clear_account_storage); - return; } @@ -435,14 +398,13 @@ int mapif_parse_AccountStorageLoad(int fd) */ int mapif_account_storage_load(int fd, int account_id) { - struct storage_data *stor = NULL; + struct storage_data stor = { 0 }; int count = 0, i = 0, len = 0; Assert_ret(account_id > 0); - stor = (struct storage_data *) idb_ensure(inter_storage->account_storage, account_id, inter_storage->ensure_account_storage); - - count = inter_storage->fromsql(account_id, stor); + VECTOR_INIT(stor.item); + count = inter_storage->fromsql(account_id, &stor); len = 8 + count * sizeof(struct item); @@ -451,9 +413,11 @@ int mapif_account_storage_load(int fd, int account_id) WFIFOW(fd, 2) = (uint16) len; WFIFOL(fd, 4) = account_id; for (i = 0; i < count; i++) - memcpy(WFIFOP(fd, 8 + i * sizeof(struct item)), &VECTOR_INDEX(stor->item, i), sizeof(struct item)); + memcpy(WFIFOP(fd, 8 + i * sizeof(struct item)), &VECTOR_INDEX(stor.item, i), sizeof(struct item)); WFIFOSET(fd, len); + VECTOR_CLEAR(stor.item); + return 1; } @@ -467,13 +431,11 @@ int mapif_parse_AccountStorageSave(int fd) { int payload_size = RFIFOW(fd, 2) - 8, account_id = RFIFOL(fd, 4); int i = 0, count = 0; - struct storage_data *cp_stor = NULL, p_stor = { 0 }; + struct storage_data p_stor = { 0 }; Assert_ret(fd > 0); Assert_ret(account_id > 0); - cp_stor = (struct storage_data *) idb_ensure(inter_storage->account_storage, account_id, inter_storage->ensure_account_storage); - count = payload_size/sizeof(struct item); VECTOR_INIT(p_stor.item); @@ -490,7 +452,7 @@ int mapif_parse_AccountStorageSave(int fd) p_stor.aggregate = count; } - inter_storage->tosql(account_id, cp_stor, &p_stor); + inter_storage->tosql(account_id, &p_stor); VECTOR_CLEAR(p_stor.item); @@ -783,8 +745,6 @@ void inter_storage_defaults(void) { inter_storage = &inter_storage_s; - inter_storage->ensure_account_storage = inter_storage_ensure_account_storage; - inter_storage->clear_account_storage = inter_storage_clear_account_storage; inter_storage->tosql = inter_storage_tosql; inter_storage->fromsql = inter_storage_fromsql; inter_storage->guild_storage_tosql = inter_storage_guild_storage_tosql; |