Skip to content

Commit

Permalink
[core] Refactor argparse usage in Application
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Feb 10, 2023
1 parent 7ac63cb commit 4072f9b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 21 deletions.
21 changes: 18 additions & 3 deletions src/common/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,31 @@
#include <windows.h>
#endif

Application::Application(std::string serverName, std::unique_ptr<argparse::ArgumentParser>&& pArgParser)
Application::Application(std::string serverName, int argc, char** argv)
: m_ServerName(serverName)
, m_RequestExit(false)
, gArgParser(std::move(pArgParser))
, gArgParser(std::make_unique<argparse::ArgumentParser>(argv[0]))
{
#ifdef _WIN32
SetConsoleTitleA(fmt::format("{}-server", serverName).c_str());
#endif

logging::InitializeLog(serverName, fmt::format("log/{}-server.log", serverName), false);
gArgParser->add_argument("--log")
.default_value(fmt::format("log/{}-server.log", serverName));

try
{
gArgParser->parse_args(argc, argv);
}
catch (const std::runtime_error& err)
{
std::cerr << err.what() << std::endl;
std::cerr << *gArgParser;
std::exit(1);
}

auto logName = gArgParser->get<std::string>("--log");
logging::InitializeLog(serverName, logName, false);
lua_init();
settings::init();
ShowInfo("Begin %s-server initialisation...", serverName);
Expand Down
2 changes: 1 addition & 1 deletion src/common/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
class Application
{
public:
Application(std::string serverName, std::unique_ptr<argparse::ArgumentParser>&& pArgParser);
Application(std::string serverName, int argc, char** argv);
virtual ~Application() = default;

Application(const Application&) = delete;
Expand Down
32 changes: 32 additions & 0 deletions src/login/login_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "common/logging.h"
#include "common/socket.h"

#include "map/message.h"

#include "login.h"
#include "login_auth.h"

Expand Down Expand Up @@ -131,6 +133,36 @@ int32 login_parse(int32 fd)
{
fmtQuery = "UPDATE accounts SET accounts.timelastmodify = NULL WHERE accounts.id = %d";
sql->Query(fmtQuery, sd->accid);

fmtQuery = "SELECT charid, server_addr, server_port \
FROM accounts_sessions JOIN accounts \
ON accounts_sessions.accid = accounts.id \
WHERE accounts.id = %d;";
ret = sql->Query(fmtQuery, sd->accid);
if (ret != SQL_ERROR && sql->NumRows() == 1)
{
while (sql->NextRow() == SQL_SUCCESS)
{
uint32 charid = sql->GetUIntData(0);
uint64 ip = sql->GetUIntData(1);
uint64 port = sql->GetUIntData(2);

ip |= (port << 32);

std::ignore = ip;

zmq::message_t chardata(sizeof(charid));
ref<uint32>((uint8*)chardata.data(), 0) = charid;
zmq::message_t empty(0);

// TODO: MSG_LOGIN is a no-op in message_server.cpp,
// : so sending this does nothing?
// : But in the client (message.cpp), it _could_
// : be used to clear out lingering PChar data.
// queue_message(ipp, MSG_LOGIN, &chardata, &empty);
}
}

memset(&sessions[fd]->wdata[0], 0, 33);
sessions[fd]->wdata.resize(33);
ref<uint8>(sessions[fd]->wdata.data(), 0) = LOGIN_SUCCESS;
Expand Down
15 changes: 1 addition & 14 deletions src/world/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,7 @@ std::atomic<bool> gRunFlag = true;

int main(int argc, char** argv)
{
auto argParser = std::make_unique<argparse::ArgumentParser>(argv[0]);

try
{
argParser->parse_args(argc, argv);
}
catch (const std::runtime_error& err)
{
std::cerr << err.what() << std::endl;
std::cerr << *argParser;
std::exit(1);
}

auto pWorldServer = std::make_unique<WorldServer>(std::move(argParser));
auto pWorldServer = std::make_unique<WorldServer>(argc, argv);

while (pWorldServer->IsRunning())
{
Expand Down
4 changes: 2 additions & 2 deletions src/world/world_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ along with this program. If not, see http://www.gnu.org/licenses/
#include "common/console_service.h"
#include "common/logging.h"

WorldServer::WorldServer(std::unique_ptr<argparse::ArgumentParser>&& pArgParser)
: Application("world", std::move(pArgParser))
WorldServer::WorldServer(int argc, char** argv)
: Application("world", argc, argv)
, messageServer(std::make_unique<message_server_wrapper_t>(std::ref(m_RequestExit)))
, httpServer(std::make_unique<HTTPServer>())
{
Expand Down
2 changes: 1 addition & 1 deletion src/world/world_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ along with this program. If not, see http://www.gnu.org/licenses/
class WorldServer final : public Application
{
public:
WorldServer(std::unique_ptr<argparse::ArgumentParser>&& pArgParser);
WorldServer(int argc, char** argv);
~WorldServer() override;

void Tick() override;
Expand Down

0 comments on commit 4072f9b

Please sign in to comment.