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

Core: Remove & Replace kernel.cpp #6960

Draft
wants to merge 4 commits into
base: base
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 0 additions & 4 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ set(COMMON_SOURCES
$<$<PLATFORM_ID:Windows>:${CMAKE_CURRENT_SOURCE_DIR}/debug_windows.cpp>
${CMAKE_CURRENT_SOURCE_DIR}/filewatcher.cpp
${CMAKE_CURRENT_SOURCE_DIR}/filewatcher.h
${CMAKE_CURRENT_SOURCE_DIR}/kernel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel.h
${CMAKE_CURRENT_SOURCE_DIR}/logging.cpp
${CMAKE_CURRENT_SOURCE_DIR}/logging.h
${CMAKE_CURRENT_SOURCE_DIR}/lua.cpp
Expand All @@ -27,8 +25,6 @@ set(COMMON_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/mmo.h
${CMAKE_CURRENT_SOURCE_DIR}/settings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/settings.h
${CMAKE_CURRENT_SOURCE_DIR}/socket.cpp
${CMAKE_CURRENT_SOURCE_DIR}/socket.h
${CMAKE_CURRENT_SOURCE_DIR}/sql.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sql.h
${CMAKE_CURRENT_SOURCE_DIR}/string.h
Expand Down
84 changes: 53 additions & 31 deletions src/common/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,43 +20,50 @@
*/

#include "application.h"

#include "debug.h"
#include "logging.h"
#include "lua.h"
#include "settings.h"
#include "taskmgr.h"
#include "xirand.h"

#ifdef _WIN32
#include <windows.h>
#endif

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

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

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

auto logName = gArgParser->get<std::string>("--log");
logging::InitializeLog(serverName, logName, false);
lua_init();
settings::init();
auto logName = argParser_->get<std::string>("--log");
logging::InitializeLog(serverName_, logName, false);

settings::init(lua_);

xirand::seed();

ShowInfo("Begin %s-server Init...", serverName);

#ifdef ENV64BIT
Expand All @@ -69,31 +76,46 @@ Application::Application(std::string const& serverName, int argc, char** argv)

ShowInfo("The %s-server is ready to work...", serverName);
ShowInfo("=======================================================================");
}

// clang-format off
gConsoleService = std::make_unique<ConsoleService>();

gConsoleService->RegisterCommand("exit", "Terminate the program.",
[&](std::vector<std::string>& inputs)
void Application::run()
{
ShowInfo("starting io_context");

// This busy loop looks nasty, however --
// https://think-async.com/Asio/asio-1.24.0/doc/asio/reference/io_service.html
//
// If an exception is thrown from a handler, the exception is allowed to propagate through the throwing thread's invocation of
// run(), run_one(), run_for(), run_until(), poll() or poll_one(). No other threads that are calling any of these functions are affected.
// It is then the responsibility of the application to catch the exception.
while (Application::isRunning())
{
fmt::print("> Goodbye!\n");
m_RequestExit = true;
});
// clang-format on
try
{
// NOTE: io_context.run() takes over and blocks this thread. Anything after this point will only fire
// if io_context finishes!
ioContext_.run();
break;
}
catch (std::exception& e)
{
// TODO: make a list of "allowed exceptions", the rest can/should cause shutdown.
ShowError(fmt::format("Inner fatal: {}", e.what()));
}
}
}

bool Application::IsRunning()
bool Application::isRunning()
{
return !m_RequestExit;
return !requestExit_;
}

void Application::Tick()
auto Application::lua() -> sol::state_view
{
// Main runtime cycle
duration next;
while (!m_RequestExit)
{
next = CTaskMgr::getInstance()->DoTimer(server_clock::now());
std::this_thread::sleep_for(next);
}
return lua_;
}

auto Application::ioContext() -> asio::io_context&
{
return ioContext_;
}
28 changes: 19 additions & 9 deletions src/common/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@

#pragma once

#include <memory>
#include <string>
#include "console_service.h"
#include "lua.h"

#include <argparse/argparse.hpp>

#include "console_service.h"
#include <asio/io_context.hpp>

#include <memory>
#include <string>

class Application
{
Expand All @@ -39,13 +42,20 @@ class Application
Application& operator=(const Application&) = delete;
Application& operator=(Application&&) = delete;

virtual bool IsRunning();
virtual void Tick();
void run();
bool isRunning();

auto lua() -> sol::state_view;
auto ioContext() -> asio::io_context&;

protected:
std::string m_ServerName;
std::atomic<bool> m_RequestExit;
std::string serverName_;
std::atomic<bool> requestExit_;

sol::state lua_;

asio::io_context ioContext_;

std::unique_ptr<argparse::ArgumentParser> gArgParser;
std::unique_ptr<ConsoleService> gConsoleService;
std::unique_ptr<argparse::ArgumentParser> argParser_;
std::unique_ptr<ConsoleService> consoleService_;
};
6 changes: 6 additions & 0 deletions src/common/cbasetypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ inline void destroy_arr(T*& ptr)
ptr = nullptr;
}

template <typename T, typename U>
T& ref(U* buf, std::size_t index)
{
return *reinterpret_cast<T*>(reinterpret_cast<uint8*>(buf) + index);
}

#include <chrono>

using namespace std::literals::chrono_literals;
Expand Down
24 changes: 11 additions & 13 deletions src/common/console_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "console_service.h"

#include "database.h"
#include "lua.h"

#include <sstream>

Expand Down Expand Up @@ -131,18 +130,17 @@ ConsoleService::ConsoleService()
}
});

RegisterCommand("lua", "Provides a Lua REPL",
[](std::vector<std::string>& inputs)
{
if (inputs.size() >= 2)
{
// Remove "lua" from the front of the inputs
inputs = std::vector<std::string>(inputs.begin() + 1, inputs.end());

auto input = fmt::format("local var = {}; if type(var) ~= \"nil\" then print(var) end", fmt::join(inputs, " "));
lua.safe_script(input);
}
});
// RegisterCommand("lua", "Provides a Lua REPL",
// [](std::vector<std::string>& inputs)
// {
// if (inputs.size() >= 2)
// {
// // Remove "lua" from the front of the inputs
// inputs = std::vector<std::string>(inputs.begin() + 1, inputs.end());
// auto input = fmt::format("local var = {}; if type(var) ~= \"nil\" then print(var) end", fmt::join(inputs, " "));
// lua.safe_script(input);
// }
// });

RegisterCommand("crash", "Crash the process",
[](std::vector<std::string>& inputs)
Expand Down
Loading
Loading