Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Core/Database): port TrinityCore database API #5611

Merged
merged 57 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
8376bba
fix(Core/Player): Prevent exploit to loot items that are already looted
Kitzunu Feb 2, 2021
dadbfb1
Revert "fix(Core/Player): Prevent exploit to loot items that are alre…
Kitzunu Feb 2, 2021
d37f1d1
Merge remote-tracking branch 'upstream/master'
Kitzunu Feb 7, 2021
9c80fd3
Merge remote-tracking branch 'upstream/master'
Kitzunu Mar 24, 2021
3c786b5
Merge remote-tracking branch 'upstream/master'
Kitzunu Mar 26, 2021
6fe5873
Merge remote-tracking branch 'upstream/master'
Kitzunu Apr 11, 2021
a0a9e26
Merge remote-tracking branch 'upstream/master'
Kitzunu Apr 15, 2021
ffc5dfc
refactor(Core/Misc): Update attributes to blizzlike
Kitzunu Apr 16, 2021
701dfd9
Revert "refactor(Core/Misc): Update attributes to blizzlike"
Kitzunu Apr 16, 2021
b7cf2c8
Merge remote-tracking branch 'upstream/master'
Kitzunu Apr 24, 2021
a653ccb
Merge remote-tracking branch 'upstream/master'
Kitzunu Apr 28, 2021
65d39b6
Merge remote-tracking branch 'upstream/master'
Kitzunu Apr 29, 2021
9f66978
Merge remote-tracking branch 'upstream/master'
Kitzunu May 7, 2021
52ff891
fix(Core/Spell): Remove ErrorCube visual
Kitzunu May 8, 2021
b5e8c5d
Revert "fix(Core/Spell): Remove ErrorCube visual"
Kitzunu May 8, 2021
9e8b8ec
Merge remote-tracking branch 'upstream/master'
Kitzunu May 8, 2021
3b11199
chore(ci): Upgrade MySQL ver on Windows build
Kitzunu May 10, 2021
39487cb
Revert "chore(ci): Upgrade MySQL ver on Windows build"
Kitzunu May 10, 2021
e50a458
Merge remote-tracking branch 'upstream/master'
Kitzunu May 10, 2021
88da5da
Merge remote-tracking branch 'upstream/master'
Kitzunu May 25, 2021
cd83e83
feat(Core/Database): port TrinityCore database API
Winfidonarleyan May 30, 2021
3c68e55
Merge remote-tracking branch 'upstream/master'
Kitzunu May 30, 2021
545de90
1
Winfidonarleyan May 30, 2021
252a4a6
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan May 30, 2021
570cae3
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan May 31, 2021
32414c0
1
Winfidonarleyan May 31, 2021
33cfd14
clang8
Winfidonarleyan Jun 1, 2021
d2ea18b
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 1, 2021
f0b28f8
20.04 - clang7, 18.04 - clang10
Winfidonarleyan Jun 1, 2021
fe1add6
to ac
Winfidonarleyan Jun 2, 2021
1d46da7
Update core_build.yml
FrancescoBorzi Jun 2, 2021
ca85741
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 3, 2021
213dae6
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 4, 2021
d69d884
ra
Winfidonarleyan Jun 4, 2021
d206b52
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 9, 2021
63a1618
time to sort includes
Winfidonarleyan Jun 9, 2021
d7a6ec4
WITH_STRICT_DATABASE_TYPE_CHECKS
Winfidonarleyan Jun 10, 2021
3e5605d
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 16, 2021
3b943f7
1
Winfidonarleyan Jun 16, 2021
719d16c
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 16, 2021
c0ecff4
1
Winfidonarleyan Jun 16, 2021
584b873
2
Winfidonarleyan Jun 17, 2021
8ba2133
Merge branch 'master' into db
FrancescoBorzi Jun 17, 2021
1abadca
Merge branch 'master' into db
Winfidonarleyan Jun 18, 2021
762ae3b
Merge remote-tracking branch 'off/master' into db
Winfidonarleyan Jun 19, 2021
586f2c0
1
Winfidonarleyan Jun 19, 2021
8bf5168
2
Winfidonarleyan Jun 19, 2021
1668ca5
Merge remote-tracking branch 'upstream/master'
Kitzunu Jun 20, 2021
71bb0b7
Merge branch 'master' into db
Kitzunu Jun 20, 2021
73b191a
Merge remote-tracking branch 'upstream/master' into pr/5611
Kitzunu Jun 21, 2021
18b68f9
1
Winfidonarleyan Jun 21, 2021
e8f7ff0
Merge branch 'master' into pr/5611
Kitzunu Jun 21, 2021
9932648
Merge branch 'db' of https://github.com/Winfidonarleyan/azerothcore-w…
Kitzunu Jun 21, 2021
97af28c
Merge remote-tracking branch 'upstream/master' into pr/5611
Kitzunu Jun 21, 2021
a917aa8
Merge branch 'master' into db
Helias Jun 21, 2021
bf044b8
Merge branch 'master' into db
Kitzunu Jun 21, 2021
3723186
Merge branch 'master' into db
Kitzunu Jun 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/dist/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ option(ENABLE_EXTRAS "Set to 0 to disable extra features optimizing perfor
option(ENABLE_VMAP_CHECKS "Enable Checks relative to DisableMgr system on vmap" 1)
option(ENABLE_EXTRA_LOGS "Enable extra log functions that can be CPU intensive" 0)
option(WITH_DYNAMIC_LINKING "Enable dynamic library linking." 0)
option(WITH_STRICT_DATABASE_TYPE_CHECKS "Enable strict checking of database field value accessors" 0)

IsDynamicLinkingRequired(WITH_DYNAMIC_LINKING_FORCED)

Expand Down
9 changes: 9 additions & 0 deletions src/cmake/showoptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ else()
message("* Show source tree : No (For UNIX default)")
endif()

if(WITH_STRICT_DATABASE_TYPE_CHECKS)
message("")
message(" *** WITH_STRICT_DATABASE_TYPE_CHECKS - WARNING!")
message(" *** Validates uses of database Get***() functions from Field class")
message(" *** invalid calls will result in returning value 0")
message(" *** NOT COMPATIBLE WITH MARIADB!")
add_definitions(-DACORE_STRICT_DATABASE_TYPE_CHECKS)
endif()

if(BUILD_SHARED_LIBS)
message("")
message(" *** WITH_DYNAMIC_LINKING - INFO!")
Expand Down
100 changes: 100 additions & 0 deletions src/common/Threading/ProducerConsumerQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef _PCQ_H
#define _PCQ_H

#include <condition_variable>
#include <mutex>
#include <queue>
#include <atomic>
#include <type_traits>

template <typename T>
class ProducerConsumerQueue
{
private:
std::mutex _queueLock;
std::queue<T> _queue;
std::condition_variable _condition;
std::atomic<bool> _shutdown;

public:

ProducerConsumerQueue<T>() : _shutdown(false) { }

void Push(const T& value)
{
std::lock_guard<std::mutex> lock(_queueLock);
_queue.push(std::move(value));

_condition.notify_one();
}

bool Empty()
{
std::lock_guard<std::mutex> lock(_queueLock);

return _queue.empty();
}

bool Pop(T& value)
{
std::lock_guard<std::mutex> lock(_queueLock);

if (_queue.empty() || _shutdown)
return false;

value = _queue.front();

_queue.pop();

return true;
}

void WaitAndPop(T& value)
{
std::unique_lock<std::mutex> lock(_queueLock);

// we could be using .wait(lock, predicate) overload here but it is broken
// https://connect.microsoft.com/VisualStudio/feedback/details/1098841
while (_queue.empty() && !_shutdown)
_condition.wait(lock);

if (_queue.empty() || _shutdown)
return;

value = _queue.front();

_queue.pop();
}

void Cancel()
{
std::unique_lock<std::mutex> lock(_queueLock);

while (!_queue.empty())
{
T& value = _queue.front();

DeleteQueuedObject(value);

_queue.pop();
}

_shutdown = true;

_condition.notify_all();
}

private:
template<typename E = T>
typename std::enable_if<std::is_pointer<E>::value>::type DeleteQueuedObject(E& obj) { delete obj; }

template<typename E = T>
typename std::enable_if<!std::is_pointer<E>::value>::type DeleteQueuedObject(E const& /*packet*/) { }
};

#endif
51 changes: 51 additions & 0 deletions src/common/Utilities/AsyncCallbackProcessor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef AsyncCallbackProcessor_h__
#define AsyncCallbackProcessor_h__

#include "Define.h"
#include <algorithm>
#include <vector>

//template <class T>
//concept AsyncCallback = requires(T t) { { t.InvokeIfReady() } -> std::convertible_to<bool> };

template<typename T> // requires AsyncCallback<T>
class AsyncCallbackProcessor
{
public:
AsyncCallbackProcessor() = default;
~AsyncCallbackProcessor() = default;

T& AddCallback(T&& query)
{
_callbacks.emplace_back(std::move(query));
return _callbacks.back();
}

void ProcessReadyCallbacks()
{
if (_callbacks.empty())
return;

std::vector<T> updateCallbacks{ std::move(_callbacks) };

updateCallbacks.erase(std::remove_if(updateCallbacks.begin(), updateCallbacks.end(), [](T& callback)
{
return callback.InvokeIfReady();
}), updateCallbacks.end());

_callbacks.insert(_callbacks.end(), std::make_move_iterator(updateCallbacks.begin()), std::make_move_iterator(updateCallbacks.end()));
}

private:
AsyncCallbackProcessor(AsyncCallbackProcessor const&) = delete;
AsyncCallbackProcessor& operator=(AsyncCallbackProcessor const&) = delete;

std::vector<T> _callbacks;
};

#endif // AsyncCallbackProcessor_h__
3 changes: 2 additions & 1 deletion src/server/authserver/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "RealmList.h"
#include "RealmAcceptor.h"
#include "DatabaseLoader.h"
#include "MySQLThreading.h"
#include "SecretMgr.h"
#include "SharedDefines.h"
#include "Util.h"
Expand Down Expand Up @@ -208,7 +209,7 @@ bool StartDB()
// Load databases
// NOTE: While authserver is singlethreaded you should keep synch_threads == 1.
// Increasing it is just silly since only 1 will be used ever.
DatabaseLoader loader;
DatabaseLoader loader("server.authserver");
loader
.AddDatabase(LoginDatabase, "Login");

Expand Down
10 changes: 5 additions & 5 deletions src/server/authserver/Server/AuthSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "SecretMgr.h"
#include "TOTP.h"
#include "Threading.h"
#include "Util.h"
#include <algorithm>
#include <openssl/crypto.h>
#include <openssl/md5.h>
Expand Down Expand Up @@ -600,7 +601,7 @@ bool AuthSocket::_HandleLogonProof()

// Update the sessionkey, last_ip, last login time and reset number of failed logins in the account table for this account
// No SQL injection (escaped user name) and IP address as received by socket
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGONPROOF);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_LOGONPROOF);
stmt->setBinary(0, _sessionKey);
stmt->setString(1, socket().getRemoteAddress().c_str());
stmt->setUInt32(2, GetLocaleByName(_localizationName));
Expand Down Expand Up @@ -648,7 +649,7 @@ bool AuthSocket::_HandleLogonProof()
// We can not include the failed account login hook. However, this is a workaround to still log this.
if (sConfigMgr->GetOption<bool>("WrongPass.Logging", false))
{
PreparedStatement* logstmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_FALP_IP_LOGGING);
LoginDatabasePreparedStatement* logstmt = LoginDatabase.GetPreparedStatement(LOGIN_INS_FALP_IP_LOGGING);
logstmt->setString(0, _accountInfo.Login);
logstmt->setString(1, socket().getRemoteAddress());
logstmt->setString(2, "Logged on failed AccountLogin due wrong password");
Expand All @@ -659,7 +660,7 @@ bool AuthSocket::_HandleLogonProof()
if (MaxWrongPassCount > 0)
{
//Increment number of failed logins by one and if it reaches the limit temporarily ban that account or IP
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_FAILEDLOGINS);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_UPD_FAILEDLOGINS);
stmt->setString(0, _accountInfo.Login);
LoginDatabase.Execute(stmt);

Expand Down Expand Up @@ -747,7 +748,6 @@ bool AuthSocket::_HandleReconnectChallenge()

auto* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_RECONNECTCHALLENGE);
stmt->setString(0, login);

PreparedQueryResult result = LoginDatabase.Query(stmt);

// Stop if the account is not found
Expand Down Expand Up @@ -866,7 +866,7 @@ bool AuthSocket::_HandleRealmList()

// Get the user id (else close the connection)
// No SQL injection (prepared statement)
PreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_ID_BY_NAME);
LoginDatabasePreparedStatement* stmt = LoginDatabase.GetPreparedStatement(LOGIN_SEL_ACCOUNT_ID_BY_NAME);
stmt->setString(0, _accountInfo.Login);

PreparedQueryResult result = LoginDatabase.Query(stmt);
Expand Down
8 changes: 4 additions & 4 deletions src/server/database/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CollectSourceFiles(
${CMAKE_CURRENT_SOURCE_DIR}
PRIVATE_SOURCES
# Exclude
${CMAKE_CURRENT_SOURCE_DIR}/Updater
${CMAKE_CURRENT_SOURCE_DIR}/PrecompiledHeaders)

if(USE_COREPCH)
Expand Down Expand Up @@ -40,12 +41,11 @@ target_include_directories(database
${CMAKE_CURRENT_BINARY_DIR})

target_link_libraries(database
# PRIVATE
PRIVATE
# acore-core-interface
# mysql
mysql
PUBLIC
common
mysql)
common)

set_target_properties(database
PROPERTIES
Expand Down
33 changes: 17 additions & 16 deletions src/server/database/Database/AdhocStatement.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#include "AdhocStatement.h"
#include "Errors.h"
#include "MySQLConnection.h"
#include "QueryResult.h"
#include <cstdlib>
#include <cstring>

/*! Basic, ad-hoc queries. */
BasicStatementTask::BasicStatementTask(const char* sql) :
m_has_result(false)
{
m_sql = strdup(sql);
}

BasicStatementTask::BasicStatementTask(const char* sql, QueryResultFuture result) :
m_has_result(true),
m_result(result)
BasicStatementTask::BasicStatementTask(char const* sql, bool async) :
m_result(nullptr)
{
m_sql = strdup(sql);
m_has_result = async; // If the operation is async, then there's a result
if (async)
m_result = new QueryResultPromise();
}

BasicStatementTask::~BasicStatementTask()
{
free((void*)m_sql);
if (m_has_result && m_result != nullptr)
delete m_result;
}

bool BasicStatementTask::Execute()
{
if (m_has_result)
{
ResultSet* result = m_conn->Query(m_sql);
if (!result || !result->GetRowCount())
if (!result || !result->GetRowCount() || !result->NextRow())
{
delete result;
m_result.set(QueryResult(nullptr));
m_result->set_value(QueryResult(nullptr));
return false;
}
result->NextRow();
m_result.set(QueryResult(result));

m_result->set_value(QueryResult(result));
return true;
}

Expand Down
23 changes: 11 additions & 12 deletions src/server/database/Database/AdhocStatement.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>
* Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef _ADHOCSTATEMENT_H
#define _ADHOCSTATEMENT_H

#include <ace/Future.h>
#include "DatabaseEnvFwd.h"
#include "Define.h"
#include "SQLOperation.h"

typedef ACE_Future<QueryResult> QueryResultFuture;
/*! Raw, ad-hoc query. */
class BasicStatementTask : public SQLOperation
class AC_DATABASE_API BasicStatementTask : public SQLOperation
{
public:
BasicStatementTask(const char* sql);
BasicStatementTask(const char* sql, QueryResultFuture result);
~BasicStatementTask() override;
BasicStatementTask(char const* sql, bool async = false);
~BasicStatementTask();

bool Execute() override;
QueryResultFuture GetFuture() const { return m_result->get_future(); }

private:
const char* m_sql; //- Raw query to be executed
char const* m_sql; //- Raw query to be executed
bool m_has_result;
QueryResultFuture m_result;
QueryResultPromise* m_result;
};

#endif
#endif
Loading