Skip to content

Commit

Permalink
testings
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyx14 committed Oct 2, 2023
1 parent 037da4f commit a5081a4
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 59 deletions.
5 changes: 3 additions & 2 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,13 @@ void Player::addStorageValue(const uint32_t key, const int32_t value, const bool
}

if (value != -1) {
int32_t oldValue = getStorageValue(key);
storageMap[key] = value;

if (!isLogin) {
auto currentFrameTime = g_dispatcher().getDispatcherCycle();
g_events().eventOnStorageUpdate(static_self_cast<Player>(), key, value, getStorageValue(key), currentFrameTime);
g_callbacks().executeCallback(EventCallback_t::playerOnStorageUpdate, &EventCallback::playerOnStorageUpdate, getPlayer(), key, value, getStorageValue(key), currentFrameTime);
g_events().eventOnStorageUpdate(static_self_cast<Player>(), key, value, oldValue, currentFrameTime);
g_callbacks().executeCallback(EventCallback_t::playerOnStorageUpdate, &EventCallback::playerOnStorageUpdate, getPlayer(), key, value, oldValue, currentFrameTime);
}
} else {
storageMap.erase(key);
Expand Down
6 changes: 3 additions & 3 deletions src/creatures/players/wheel/player_wheel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ namespace {
{ 43949, "wheel.scroll.extended", 13 },
{ 43950, "wheel.scroll.advanced", 20 },
};

} // namespace

PlayerWheel::PlayerWheel(Player &initPlayer) :
Expand Down Expand Up @@ -784,7 +783,8 @@ void PlayerWheel::saveSlotPointsOnPressSaveButton(NetworkMessage &msg) {
return;
}

auto startSaveTime = OTSYS_TIME();
Benchmark bm_saveSlot;

if (!canOpenWheel()) {
return;
}
Expand Down Expand Up @@ -849,7 +849,7 @@ void PlayerWheel::saveSlotPointsOnPressSaveButton(NetworkMessage &msg) {
initializePlayerData();
registerPlayerBonusData();

g_logger().debug("Player: {} is saved the all slots info in: {} seconds", m_player.getName(), (OTSYS_TIME() - startSaveTime) / (1000.));
g_logger().debug("Player: {} is saved the all slots info in: {} seconds", m_player.getName(), bm_saveSlot.duration());
}

/*
Expand Down
3 changes: 2 additions & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,8 @@ bool Game::removeCreature(std::shared_ptr<Creature> creature, bool isLogout /* =
void Game::executeDeath(uint32_t creatureId) {
std::shared_ptr<Creature> creature = getCreatureByID(creatureId);
if (creature && !creature->isRemoved()) {
creature->onDeath();
afterCreatureZoneChange(creature, creature->getZones(), {});
creature->onDeath();
}
}

Expand Down Expand Up @@ -5715,6 +5715,7 @@ void Game::checkCreatures(size_t index) {
creature->onAttacking(EVENT_CREATURE_THINK_INTERVAL);
creature->executeConditions(EVENT_CREATURE_THINK_INTERVAL);
} else {
afterCreatureZoneChange(creature, creature->getZones(), {});
creature->onDeath();
}
++it;
Expand Down
21 changes: 21 additions & 0 deletions src/game/zones/zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,24 @@ void Zone::itemRemoved(const std::shared_ptr<Item> &item) {
}
itemsCache.erase(item);
}

void Zone::refresh() {
creaturesCache.clear();
monstersCache.clear();
npcsCache.clear();
playersCache.clear();
itemsCache.clear();

for (const auto &position : positions) {
const auto tile = g_game().map.getTile(position);
if (!tile) {
continue;
}
for (const auto &item : *tile->getItemList()) {
itemAdded(item);
}
for (const auto &creature : *tile->getCreatures()) {
creatureAdded(creature);
}
}
}
2 changes: 2 additions & 0 deletions src/game/zones/zone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class Zone {
void removeMonsters() const;
void removeNpcs() const;

void refresh();

static std::shared_ptr<Zone> addZone(const std::string &name);
static std::shared_ptr<Zone> getZone(const std::string &name);
static phmap::parallel_flat_hash_set<std::shared_ptr<Zone>> getZones(const Position position);
Expand Down
4 changes: 2 additions & 2 deletions src/io/iomap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*/

void IOMap::loadMap(Map* map, const Position &pos) {
const int64_t start = OTSYS_TIME();
Benchmark bm_mapLoad;

const auto &fileByte = mio::mmap_source(map->path.string());

Expand Down Expand Up @@ -77,7 +77,7 @@ void IOMap::loadMap(Map* map, const Position &pos) {

map->flush();

g_logger().info("Map Loaded {} ({}x{}) in {} seconds", map->path.filename().string(), map->width, map->height, static_cast<double>(OTSYS_TIME() - start) / 1000.f);
g_logger().info("Map Loaded {} ({}x{}) in {} seconds", map->path.filename().string(), map->width, map->height, bm_mapLoad.duration());
}

void IOMap::parseMapDataAttributes(FileStream &stream, Map* map) {
Expand Down
9 changes: 5 additions & 4 deletions src/io/iomapserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "items/bed.hpp"

void IOMapSerialize::loadHouseItems(Map* map) {
int64_t start = OTSYS_TIME();
Benchmark bm_context;

DBResult_ptr result = Database::getInstance().storeQuery("SELECT `data` FROM `tile_store`");
if (!result) {
Expand Down Expand Up @@ -49,7 +49,7 @@ void IOMapSerialize::loadHouseItems(Map* map) {
loadItem(propStream, tile, true);
}
} while (result->next());
g_logger().info("Loaded house items in {} seconds", (OTSYS_TIME() - start) / (1000.));
g_logger().info("Loaded house items in {} seconds", bm_context.duration());
}
bool IOMapSerialize::saveHouseItems() {
bool success = DBTransaction::executeWithinTransaction([]() {
Expand All @@ -64,7 +64,8 @@ bool IOMapSerialize::saveHouseItems() {
}

bool IOMapSerialize::SaveHouseItemsGuard() {
int64_t start = OTSYS_TIME();
Benchmark bm_context;

Database &db = Database::getInstance();
std::ostringstream query;

Expand Down Expand Up @@ -97,7 +98,7 @@ bool IOMapSerialize::SaveHouseItemsGuard() {
return false;
}

g_logger().info("Saved house items in {} seconds", (OTSYS_TIME() - start) / (1000.));
g_logger().info("Saved house items in {} seconds", bm_context.duration());
return true;
}

Expand Down
11 changes: 11 additions & 0 deletions src/lua/functions/core/game/zone_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,14 @@ int ZoneFunctions::luaZoneGetAll(lua_State* L) {
}
return 1;
}

int ZoneFunctions::luaZoneRefresh(lua_State* L) {
// Zone:refresh()
auto zone = getUserdataShared<Zone>(L, 1);
if (!zone) {
reportErrorFunc(getErrorDesc(LUA_ERROR_ZONE_NOT_FOUND));
return 1;
}
zone->refresh();
return 1;
}
1 change: 1 addition & 0 deletions src/lua/functions/core/game/zone_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ZoneFunctions final : LuaScriptInterface {
registerMethod(L, "Zone", "removePlayers", ZoneFunctions::luaZoneRemovePlayers);
registerMethod(L, "Zone", "removeMonsters", ZoneFunctions::luaZoneRemoveMonsters);
registerMethod(L, "Zone", "removeNpcs", ZoneFunctions::luaZoneRemoveNpcs);
registerMethod(L, "Zone", "refresh", ZoneFunctions::luaZoneRefresh);

// static methods
registerMethod(L, "Zone", "getByPosition", ZoneFunctions::luaZoneGetByPosition);
Expand Down
1 change: 1 addition & 0 deletions src/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// --------------------

// Utils
#include "utils/benchmark.hpp"
#include "utils/definitions.hpp"
#include "utils/simd.hpp"

Expand Down
62 changes: 20 additions & 42 deletions src/server/network/protocol/protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,9 @@
#include "security/rsa.hpp"
#include "game/scheduling/dispatcher.hpp"

Protocol::~Protocol() {
if (compreesionEnabled) {
deflateEnd(defStream.get());
}
}

void Protocol::onSendMessage(const OutputMessage_ptr &msg) {
if (!rawMessages) {
uint32_t sendMessageChecksum = 0;
if (compreesionEnabled && msg->getLength() >= 128 && compression(*msg)) {
sendMessageChecksum = (1U << 31);
}
const uint32_t sendMessageChecksum = msg->getLength() >= 128 && compression(*msg) ? (1U << 31) : 0;

msg->writeMessageLength();

Expand Down Expand Up @@ -208,54 +199,41 @@ uint32_t Protocol::getIP() const {
return 0;
}

void Protocol::enableCompression() {
if (compreesionEnabled) {
return;
}

int32_t compressionLevel = g_configManager().getNumber(COMPRESSION_LEVEL);
if (compressionLevel <= 0) {
return;
bool Protocol::compression(OutputMessage &msg) const {
if (checksumMethod != CHECKSUM_METHOD_SEQUENCE) {
return false;
}

defStream = std::make_unique<z_stream>();
defStream->zalloc = nullptr;
defStream->zfree = nullptr;
defStream->opaque = nullptr;

compreesionEnabled = deflateInit2(defStream.get(), compressionLevel, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY) == Z_OK;

if (!compreesionEnabled) {
defStream.reset();
g_logger().error("[Protocol::enableCompression()] - Zlib deflateInit2 error: {}", (defStream->msg ? defStream->msg : " unknown error"));
static const thread_local auto &compress = std::make_unique<ZStream>();
if (!compress->stream) {
return false;
}
}

bool Protocol::compression(OutputMessage &msg) const {
auto outputMessageSize = msg.getLength();
const auto outputMessageSize = msg.getLength();
if (outputMessageSize > NETWORKMESSAGE_MAXSIZE) {
g_logger().error("[NetworkMessage::compression] - Exceded NetworkMessage max size: {}, actually size: {}", NETWORKMESSAGE_MAXSIZE, outputMessageSize);
return false;
}

static thread_local std::array<char, NETWORKMESSAGE_MAXSIZE> defBuffer;
defStream->next_in = msg.getOutputBuffer();
defStream->avail_in = outputMessageSize;
defStream->next_out = (Bytef*)defBuffer.data();
defStream->avail_out = NETWORKMESSAGE_MAXSIZE;
compress->stream->next_in = msg.getOutputBuffer();
compress->stream->avail_in = outputMessageSize;
compress->stream->next_out = reinterpret_cast<Bytef*>(compress->buffer.data());
compress->stream->avail_out = NETWORKMESSAGE_MAXSIZE;

if (int32_t ret = deflate(defStream.get(), Z_FINISH);
ret != Z_OK && ret != Z_STREAM_END) {
const int32_t ret = deflate(compress->stream.get(), Z_FINISH);
if (ret != Z_OK && ret != Z_STREAM_END) {
return false;
}
auto totalSize = static_cast<uint32_t>(defStream->total_out);
deflateReset(defStream.get());

const auto totalSize = compress->stream->total_out;
deflateReset(compress->stream.get());

if (totalSize == 0) {
return false;
}

msg.reset();
auto charData = static_cast<char*>(static_cast<void*>(defBuffer.data()));
msg.addBytes(charData, static_cast<size_t>(totalSize));
msg.addBytes(compress->buffer.data(), totalSize);

return true;
}
31 changes: 27 additions & 4 deletions src/server/network/protocol/protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Protocol : public std::enable_shared_from_this<Protocol> {
explicit Protocol(Connection_ptr initConnection) :
connectionPtr(initConnection) { }

virtual ~Protocol();
virtual ~Protocol() = default;

// non-copyable
Protocol(const Protocol &) = delete;
Expand Down Expand Up @@ -71,7 +71,6 @@ class Protocol : public std::enable_shared_from_this<Protocol> {
void setChecksumMethod(ChecksumMethods_t method) {
checksumMethod = method;
}
void enableCompression();

static bool RSA_decrypt(NetworkMessage &msg);

Expand All @@ -82,12 +81,37 @@ class Protocol : public std::enable_shared_from_this<Protocol> {
virtual void release() { }

private:
struct ZStream {
ZStream() noexcept {
const int32_t compressionLevel = g_configManager().getNumber(COMPRESSION_LEVEL);
if (compressionLevel <= 0) {
return;
}

stream = std::make_unique<z_stream>();
stream->zalloc = nullptr;
stream->zfree = nullptr;
stream->opaque = nullptr;

if (deflateInit2(stream.get(), compressionLevel, Z_DEFLATED, -15, 9, Z_DEFAULT_STRATEGY) != Z_OK) {
stream.reset();
g_logger().error("[Protocol::enableCompression()] - Zlib deflateInit2 error: {}", (stream->msg ? stream->msg : " unknown error"));
}
}

~ZStream() {
deflateEnd(stream.get());
}

std::unique_ptr<z_stream> stream;
std::array<char, NETWORKMESSAGE_MAXSIZE> buffer {};
};

void XTEA_encrypt(OutputMessage &msg) const;
bool XTEA_decrypt(NetworkMessage &msg) const;
bool compression(OutputMessage &msg) const;

OutputMessage_ptr outputBuffer;
std::unique_ptr<z_stream> defStream;

const ConnectionWeak_ptr connectionPtr;
std::array<uint32_t, 4> key = {};
Expand All @@ -96,7 +120,6 @@ class Protocol : public std::enable_shared_from_this<Protocol> {
std::underlying_type_t<ChecksumMethods_t> checksumMethod = CHECKSUM_METHOD_NONE;
bool encryptionEnabled = false;
bool rawMessages = false;
bool compreesionEnabled = false;

friend class Connection;
};
1 change: 0 additions & 1 deletion src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,6 @@ void ProtocolGame::onRecvFirstMessage(NetworkMessage &msg) {
setChecksumMethod(CHECKSUM_METHOD_ADLER32);
} else if (operatingSystem <= CLIENTOS_OTCLIENT_MAC) {
setChecksumMethod(CHECKSUM_METHOD_SEQUENCE);
enableCompression();
}

clientVersion = static_cast<int32_t>(msg.get<uint32_t>());
Expand Down
Loading

0 comments on commit a5081a4

Please sign in to comment.