diff --git a/CMakeLists.txt b/CMakeLists.txt index 243ea485d..54ed1b3b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -157,15 +157,15 @@ else() message(FATAL_ERROR "No targets were selected to build, we must build at least one target.") endif() -#if(EQEMU_BUILD_SERVER) -# add_subdirectory(shared_memory) -# add_subdirectory(world) -# add_subdirectory(zone) -# add_subdirectory(ucs) -# add_subdirectory(queryserv) -# add_subdirectory(eqlaunch) -#endif() -# +if(EQEMU_BUILD_SERVER) + add_subdirectory(shared_memory) + add_subdirectory(world) + #add_subdirectory(zone) + #add_subdirectory(ucs) + #add_subdirectory(queryserv) + #add_subdirectory(eqlaunch) +endif() + #if(EQEMU_BUILD_LOGIN) # add_subdirectory(loginserver) #endif() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index f2328383e..640257a92 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -20,6 +20,7 @@ set(common_sources database/database_update_manifest_custom.cpp database/database_update_manifest_bots.cpp database/database_update.cpp + data_bucket.cpp dbcore.cpp deity.cpp dynamic_zone_base.cpp @@ -554,6 +555,7 @@ set(common_headers database.h database_schema.h database/database_update.h + data_bucket.cpp dbcore.h deity.h discord/discord.h diff --git a/common/data_bucket.cpp b/common/data_bucket.cpp index bef06a124..0378c379f 100644 --- a/common/data_bucket.cpp +++ b/common/data_bucket.cpp @@ -9,17 +9,7 @@ using json = nlohmann::json; const std::string NESTED_KEY_DELIMITER = "."; std::vector g_data_bucket_cache = {}; -#if defined(ZONE) -#include "../zone/zonedb.h" -extern ZoneDatabase database; -#elif defined(WORLD) -#include "../world/worlddb.h" -extern WorldDatabase database; -#else -#error "You must define either ZONE or WORLD" -#endif - -void DataBucket::SetData(const std::string &bucket_key, const std::string &bucket_value, std::string expires_time) +void DataBucket::SetData(SharedDatabase* database, const std::string &bucket_key, const std::string &bucket_value, std::string expires_time) { auto k = DataBucketKey{ .key = bucket_key, @@ -27,10 +17,10 @@ void DataBucket::SetData(const std::string &bucket_key, const std::string &bucke .expires = expires_time, }; - DataBucket::SetData(k); + DataBucket::SetData(database, k); } -void DataBucket::SetData(const DataBucketKey &k_) +void DataBucket::SetData(SharedDatabase *database, const DataBucketKey &k_) { DataBucketKey k = k_; // copy the key so we can modify it bool is_nested = k.key.find(NESTED_KEY_DELIMITER) != std::string::npos; @@ -39,7 +29,7 @@ void DataBucket::SetData(const DataBucketKey &k_) } auto b = DataBucketsRepository::NewEntity(); - auto r = GetData(k, true); + auto r = GetData(database, k, true); // if we have an entry, use it if (r.id > 0) { b = r; @@ -149,10 +139,10 @@ void DataBucket::SetData(const DataBucketKey &k_) } } - DataBucketsRepository::UpdateOne(database, b); + DataBucketsRepository::UpdateOne(*database, b); } else { - b = DataBucketsRepository::InsertOne(database, b); + b = DataBucketsRepository::InsertOne(*database, b); // add to cache if it doesn't exist if (CanCache(k) && !ExistsInCache(b)) { @@ -162,9 +152,9 @@ void DataBucket::SetData(const DataBucketKey &k_) } } -std::string DataBucket::GetData(const std::string &bucket_key) +std::string DataBucket::GetData(SharedDatabase* database, const std::string &bucket_key) { - return GetData(DataBucketKey{.key = bucket_key}).value; + return GetData(database, DataBucketKey{.key = bucket_key}).value; } DataBucketsRepository::DataBuckets DataBucket::ExtractNestedValue( @@ -214,7 +204,7 @@ DataBucketsRepository::DataBuckets DataBucket::ExtractNestedValue( // if the bucket doesn't exist, it will be added to the cache as a miss // if ignore_misses_cache is true, the bucket will not be added to the cache as a miss // the only place we should be ignoring the misses cache is on the initial read during SetData -DataBucketsRepository::DataBuckets DataBucket::GetData(const DataBucketKey &k_, bool ignore_misses_cache) +DataBucketsRepository::DataBuckets DataBucket::GetData(SharedDatabase* database, const DataBucketKey &k_, bool ignore_misses_cache) { DataBucketKey k = k_; // Copy the key so we can modify it @@ -244,7 +234,7 @@ DataBucketsRepository::DataBuckets DataBucket::GetData(const DataBucketKey &k_, if (CheckBucketMatch(e, k)) { if (e.expires > 0 && e.expires < std::time(nullptr)) { LogDataBuckets("Attempted to read expired key [{}] removing from cache", e.key_); - DeleteData(k); + DeleteData(database, k); return DataBucketsRepository::NewEntity(); } @@ -261,7 +251,7 @@ DataBucketsRepository::DataBuckets DataBucket::GetData(const DataBucketKey &k_, // Fetch the value from the database auto r = DataBucketsRepository::GetWhere( - database, + *database, fmt::format( " {} `key` = '{}' LIMIT 1", DataBucket::GetScopedDbFilters(k), @@ -310,7 +300,7 @@ DataBucketsRepository::DataBuckets DataBucket::GetData(const DataBucketKey &k_, // If the entry has expired, delete it if (bucket.expires > 0 && bucket.expires < static_cast(std::time(nullptr))) { - DeleteData(k); + DeleteData(database, k); return DataBucketsRepository::NewEntity(); } @@ -337,22 +327,22 @@ DataBucketsRepository::DataBuckets DataBucket::GetData(const DataBucketKey &k_, return bucket; } -std::string DataBucket::GetDataExpires(const std::string &bucket_key) +std::string DataBucket::GetDataExpires(SharedDatabase* database, const std::string &bucket_key) { - return GetDataExpires(DataBucketKey{.key = bucket_key}); + return GetDataExpires(database, DataBucketKey{.key = bucket_key}); } -std::string DataBucket::GetDataRemaining(const std::string &bucket_key) +std::string DataBucket::GetDataRemaining(SharedDatabase* database, const std::string &bucket_key) { - return GetDataRemaining(DataBucketKey{.key = bucket_key}); + return GetDataRemaining(database, DataBucketKey{.key = bucket_key}); } -bool DataBucket::DeleteData(const std::string &bucket_key) +bool DataBucket::DeleteData(SharedDatabase* database, const std::string &bucket_key) { - return DeleteData(DataBucketKey{.key = bucket_key}); + return DeleteData(database, DataBucketKey{.key = bucket_key}); } -bool DataBucket::DeleteData(const DataBucketKey &k) +bool DataBucket::DeleteData(SharedDatabase* database, const DataBucketKey &k) { bool is_nested_key = k.key.find(NESTED_KEY_DELIMITER) != std::string::npos; @@ -374,7 +364,7 @@ bool DataBucket::DeleteData(const DataBucketKey &k) // Regular key deletion, no nesting involved return DataBucketsRepository::DeleteWhere( - database, + *database, fmt::format("{} `key` = '{}'", DataBucket::GetScopedDbFilters(k), k.key) ); } @@ -384,7 +374,7 @@ bool DataBucket::DeleteData(const DataBucketKey &k) DataBucketKey top_level_k = k; top_level_k.key = top_level_key; - auto r = GetData(top_level_k); + auto r = GetData(database, top_level_k); if (r.id == 0 || r.value.empty() || !Strings::IsValidJson(r.value)) { LogDataBuckets("Attempted to delete nested key [{}] but parent key [{}] does not exist or is invalid JSON", k.key, top_level_key); return false; @@ -444,14 +434,14 @@ bool DataBucket::DeleteData(const DataBucketKey &k) } return DataBucketsRepository::DeleteWhere( - database, + *database, fmt::format("{} `key` = '{}'", DataBucket::GetScopedDbFilters(k), top_level_key) ); } // Otherwise, update the existing JSON without the deleted key r.value = json_value.dump(); - DataBucketsRepository::UpdateOne(database, r); + DataBucketsRepository::UpdateOne(*database, r); // Update cache if (CanCache(k)) { @@ -466,7 +456,7 @@ bool DataBucket::DeleteData(const DataBucketKey &k) return true; } -std::string DataBucket::GetDataExpires(const DataBucketKey &k) +std::string DataBucket::GetDataExpires(SharedDatabase* database, const DataBucketKey &k) { LogDataBuckets( "Getting bucket expiration key [{}] bot_id [{}] account_id [{}] character_id [{}] npc_id [{}]", @@ -477,7 +467,7 @@ std::string DataBucket::GetDataExpires(const DataBucketKey &k) k.npc_id ); - auto r = GetData(k); + auto r = GetData(database, k); if (r.id == 0) { return {}; } @@ -485,7 +475,7 @@ std::string DataBucket::GetDataExpires(const DataBucketKey &k) return std::to_string(r.expires); } -std::string DataBucket::GetDataRemaining(const DataBucketKey &k) +std::string DataBucket::GetDataRemaining(SharedDatabase* database, const DataBucketKey &k) { LogDataBuckets( "Getting bucket remaining key [{}] bot_id [{}] account_id [{}] character_id [{}] npc_id [{}] bot_id [{}] zone_id [{}] instance_id [{}]", @@ -499,7 +489,7 @@ std::string DataBucket::GetDataRemaining(const DataBucketKey &k) k.instance_id ); - auto r = GetData(k); + auto r = GetData(database, k); if (r.id == 0) { return "0"; } @@ -565,10 +555,10 @@ bool DataBucket::CheckBucketMatch(const DataBucketsRepository::DataBuckets &dbe, ); } -void DataBucket::LoadZoneCache(uint16 zone_id, uint16 instance_id) +void DataBucket::LoadZoneCache(SharedDatabase* database, uint16 zone_id, uint16 instance_id) { const auto &l = DataBucketsRepository::GetWhere( - database, + *database, fmt::format( "zone_id = {} AND instance_id = {} AND (`expires` > {} OR `expires` = 0)", zone_id, @@ -608,7 +598,7 @@ void DataBucket::LoadZoneCache(uint16 zone_id, uint16 instance_id) ); } -void DataBucket::BulkLoadEntitiesToCache(DataBucketLoadType::Type t, std::vector ids) +void DataBucket::BulkLoadEntitiesToCache(SharedDatabase* database, DataBucketLoadType::Type t, std::vector ids) { if (ids.empty()) { return; @@ -653,7 +643,7 @@ void DataBucket::BulkLoadEntitiesToCache(DataBucketLoadType::Type t, std::vector } const auto &l = DataBucketsRepository::GetWhere( - database, + *database, fmt::format( "{} IN ({}) AND (`expires` > {} OR `expires` = 0)", column, @@ -843,4 +833,4 @@ bool DataBucket::CanCache(const DataBucketKey &key) } return false; -} \ No newline at end of file +} diff --git a/common/data_bucket.h b/common/data_bucket.h index 6f5c1bcc6..c93d98b2c 100644 --- a/common/data_bucket.h +++ b/common/data_bucket.h @@ -5,6 +5,7 @@ #include "types.h" #include "repositories/data_buckets_repository.h" #include "json/json_archive_single_line.h" +#include "shareddb.h" struct DataBucketKey { std::string key; @@ -38,26 +39,26 @@ namespace DataBucketLoadType { class DataBucket { public: // non-scoped bucket methods (for global buckets) - static void SetData(const std::string &bucket_key, const std::string &bucket_value, std::string expires_time = ""); - static bool DeleteData(const std::string &bucket_key); - static std::string GetData(const std::string &bucket_key); - static std::string GetDataExpires(const std::string &bucket_key); - static std::string GetDataRemaining(const std::string &bucket_key); + static void SetData(SharedDatabase *database, const std::string &bucket_key, const std::string &bucket_value, std::string expires_time = ""); + static bool DeleteData(SharedDatabase *database, const std::string &bucket_key); + static std::string GetData(SharedDatabase *database, const std::string &bucket_key); + static std::string GetDataExpires(SharedDatabase *database, const std::string &bucket_key); + static std::string GetDataRemaining(SharedDatabase *database, const std::string &bucket_key); // scoped bucket methods - static void SetData(const DataBucketKey &k_); - static bool DeleteData(const DataBucketKey &k); - static DataBucketsRepository::DataBuckets GetData(const DataBucketKey &k_, bool ignore_misses_cache = false); - static std::string GetDataExpires(const DataBucketKey &k); - static std::string GetDataRemaining(const DataBucketKey &k); + static void SetData(SharedDatabase *database, const DataBucketKey &k_); + static bool DeleteData(SharedDatabase *database, const DataBucketKey &k); + static DataBucketsRepository::DataBuckets GetData(SharedDatabase *database, const DataBucketKey &k_, bool ignore_misses_cache = false); + static std::string GetDataExpires(SharedDatabase *database, const DataBucketKey &k); + static std::string GetDataRemaining(SharedDatabase *database, const DataBucketKey &k); static std::string GetScopedDbFilters(const DataBucketKey &k); // bucket repository versus key matching static bool CheckBucketMatch(const DataBucketsRepository::DataBuckets &dbe, const DataBucketKey &k); static bool ExistsInCache(const DataBucketsRepository::DataBuckets &entry); - static void LoadZoneCache(uint16 zone_id, uint16 instance_id); - static void BulkLoadEntitiesToCache(DataBucketLoadType::Type t, std::vector ids); + static void LoadZoneCache(SharedDatabase* database, uint16 zone_id, uint16 instance_id); + static void BulkLoadEntitiesToCache(SharedDatabase* database, DataBucketLoadType::Type t, std::vector ids); static void DeleteCachedBuckets(DataBucketLoadType::Type type, uint32 id, uint32 secondary_id = 0); static void DeleteFromMissesCache(DataBucketsRepository::DataBuckets e); diff --git a/shared_memory/CMakeLists.txt b/shared_memory/CMakeLists.txt index 92e8d17d6..099d27b5b 100644 --- a/shared_memory/CMakeLists.txt +++ b/shared_memory/CMakeLists.txt @@ -15,6 +15,6 @@ add_executable(shared_memory ${shared_memory_sources} ${shared_memory_headers}) install(TARGETS shared_memory RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin) -target_link_libraries(shared_memory ${SERVER_LIBS}) +target_link_libraries(shared_memory common) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) diff --git a/world/CMakeLists.txt b/world/CMakeLists.txt index 273a0a375..d980a458f 100644 --- a/world/CMakeLists.txt +++ b/world/CMakeLists.txt @@ -7,7 +7,6 @@ set(world_sources cliententry.cpp clientlist.cpp console.cpp - ../common/data_bucket.cpp dynamic_zone.cpp dynamic_zone_manager.cpp eql_config.cpp @@ -43,7 +42,6 @@ set(world_headers cliententry.h clientlist.h console.h - ../common/data_bucket.h dynamic_zone.h dynamic_zone_manager.h eql_config.h @@ -82,6 +80,6 @@ endif() add_definitions(-DWORLD) -target_link_libraries(world ${SERVER_LIBS}) +target_link_libraries(world common) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) diff --git a/world/client.cpp b/world/client.cpp index 674d09639..91aaf3e9f 100644 --- a/world/client.cpp +++ b/world/client.cpp @@ -2530,9 +2530,9 @@ void Client::SendUnsupportedClientPacket(const std::string& message) void Client::LoadDataBucketsCache() { - DataBucket::BulkLoadEntitiesToCache(DataBucketLoadType::Account, {GetAccountID()}); + DataBucket::BulkLoadEntitiesToCache(&database, DataBucketLoadType::Account, {GetAccountID()}); const auto ids = CharacterDataRepository::GetCharacterIDsByAccountID(database, GetAccountID()); - DataBucket::BulkLoadEntitiesToCache(DataBucketLoadType::Client, ids); + DataBucket::BulkLoadEntitiesToCache(&database, DataBucketLoadType::Client, ids); } void Client::ClearDataBucketsCache() diff --git a/zone/lua_parser.cpp b/zone/lua_parser.cpp index 3e3a5257f..00f89e6d6 100644 --- a/zone/lua_parser.cpp +++ b/zone/lua_parser.cpp @@ -434,7 +434,6 @@ LuaParser::LuaParser() { ZoneArgumentDispatch[EVENT_TIMER_RESUME] = handle_zone_timer_pause_resume_start; ZoneArgumentDispatch[EVENT_TIMER_START] = handle_zone_timer_pause_resume_start; ZoneArgumentDispatch[EVENT_TIMER_STOP] = handle_zone_timer_stop; -#endif L = nullptr; } @@ -2295,3 +2294,5 @@ void LuaParser::LoadZoneScript(std::string filename) { void LuaParser::LoadGlobalZoneScript(std::string filename) { LoadScript(filename, "global_zone"); } + +#endif