rodecomp/Source/Contents/Store/StoreBackupData.cpp
ragnarok-clanker d0d58f3fb3 Decompile fn_04233 (CMCStoreInfoBackup::ImportData)
Batch #17580 by eAthena: 0 byte-identical, 1 confirmed by peer review.

- fn_04233 CMCStoreInfoBackup::ImportData: logic confirmed by peer review

Peer-reviewed-by: Yommy, cremane, bibinir
Accepted without human review: peers confirmed the logic ("same" ahead by at least 3, with 70% of the votes).
2026-11-12 01:14:19 +00:00

118 lines
4.1 KiB
C++

#include "Contents/Store/MCStoreInfoBackup.h"
#include "Client/Session/Session.h"
#include "Network/TextEncoding.h"
#include "UI/Shop/UIMerchantShopMakeWnd.h"
#include "UI/Window/UIWindowMgr.h"
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <json/value.h>
// INLINED
template <typename T>
static __forceinline void StoreField(unsigned char* field, T value) {
std::memcpy(field, &value, sizeof(value));
}
// INLINED
template <typename T>
static __forceinline T LoadField(const unsigned char* field) {
T value;
std::memcpy(&value, field, sizeof(value));
return value;
}
// OFFSET: 0x00606190
// FUNCTION: fn_04233
void CMCStoreInfoBackup::ImportData(Json::Value& data) {
this->items.clear();
this->title = static_cast<const std::string&>(FromUtf8(data.get("Title", "").asString(), g_textCodePage));
Json::Value values = data["Item"];
const std::string styledText = values.toStyledString();
if (values.isNull() && this->title.empty()) {
return;
}
for (auto entry = values.begin(); entry != values.end(); ++entry) {
if (!(*entry).isObject()) {
continue;
}
ITEM_INFO item;
item.SetItemId((*entry).get("ID", 0).asInt());
item.quantity = (*entry).get("Count", 0).asInt();
item.identified = (*entry).get("Identified", 0).asBool();
item.refiningLevel = (*entry).get("RefiningLevel", 0).asInt();
item.grade = static_cast<std::int16_t>((*entry).get("GradeLevel", 0).asInt());
StoreField<std::int32_t>(item.cards + 0, (*entry).get("Slot_1", 0).asInt());
StoreField<std::int32_t>(item.cards + 4, (*entry).get("Slot_2", 0).asInt());
StoreField<std::int32_t>(item.cards + 8, (*entry).get("Slot_3", 0).asInt());
StoreField<std::int32_t>(item.cards + 12, (*entry).get("Slot_4", 0).asInt());
item.optionCount = (*entry).get("RandomOptionCount", 0).asInt();
Json::Value options = (*entry)["RandomOptionList"];
int index = 0;
unsigned char* record = item.options;
for (auto option = options.begin(); option != options.end() && index <= 5; ++option, ++index, record += 5) {
StoreField<std::int16_t>(record, (*option).get("OptIndex", 0).asInt());
StoreField<std::int16_t>(record + 2, (*option).get("Value", 0).asInt());
record[4] = static_cast<unsigned char>((*option).get("Parm1", 0).asInt());
}
item.prices[0] = static_cast<std::uint32_t>((*entry).get("Price", 0).asInt());
this->items.push_back(item);
}
UIWindow* window = g_windowMgr.merchantShopMakeWnd ? g_windowMgr.merchantShopMakeWnd : g_windowMgr.buyingShopMakeWnd;
if (window) {
static_cast<UIMerchantShopMakeWnd*>(window)->Refresh();
}
}
// OFFSET: 0x00607240
// FUNCTION: fn_04238
void CMCStoreInfoBackup::ExportData(Json::Value& data) const {
data["Title"] = ToUtf8(this->title, g_textCodePage);
Json::Value values;
const int itemCount = g_session.GetList9Size();
for (int itemIndex = 0; itemIndex < itemCount; ++itemIndex) {
const ITEM_INFO item = g_session.GetItem1758At(itemIndex);
Json::Value value;
value["ID"] = std::atoi(item.itemId.c_str());
value["Count"] = item.quantity;
value["Identified"] = item.identified != 0;
value["RefiningLevel"] = item.refiningLevel;
value["GradeLevel"] = item.grade;
value["Slot_1"] = LoadField<std::int32_t>(item.cards + 0);
value["Slot_2"] = LoadField<std::int32_t>(item.cards + 4);
value["Slot_3"] = LoadField<std::int32_t>(item.cards + 8);
value["Slot_4"] = LoadField<std::int32_t>(item.cards + 12);
const int count = item.optionCount;
value["RandomOptionCount"] = count;
Json::Value options;
for (int index = 0; index < count; ++index) {
const auto* record = reinterpret_cast<const unsigned char*>(&item) + offsetof(ITEM_INFO, options) + index * 5;
std::int16_t id, amount;
std::memcpy(&id, record, sizeof(id));
std::memcpy(&amount, record + 2, sizeof(amount));
Json::Value option;
option["OptIndex"] = id;
option["Value"] = amount;
option["Parm1"] = record[4];
options.append(option);
}
value["RandomOptionList"] = options;
value["Price"] = static_cast<int>(item.prices[0]);
values.append(value);
}
data["Item"] = values;
}