Skip to content

Commit

Permalink
Progress
Browse files Browse the repository at this point in the history
  • Loading branch information
zach2good committed Feb 8, 2025
1 parent 447e410 commit e862698
Show file tree
Hide file tree
Showing 34 changed files with 316 additions and 405 deletions.
58 changes: 44 additions & 14 deletions src/common/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
*/

#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>
Expand All @@ -41,24 +43,26 @@ Application::Application(std::string const& serverName, int argc, char** argv)
SetConsoleTitleA(fmt::format("{}-server", serverName_).c_str());
#endif

gArgParser->add_argument("--log")
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");
auto logName = argParser_->get<std::string>("--log");
logging::InitializeLog(serverName_, logName, false);

settings::init();
settings::init(lua_);

xirand::seed();

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

Expand All @@ -72,20 +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()
{
return !requestExit_;
}

auto Application::lua() -> sol::state_view
{
return lua_;
}

auto Application::ioContext() -> asio::io_context&
{
return ioContext_;
}
7 changes: 5 additions & 2 deletions src/common/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ class Application
Application& operator=(Application&&) = delete;

void run();
bool isRunning();

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

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

sol::lua_state lua_;
sol::state lua_;

asio::io_context ioContext_;

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
22 changes: 13 additions & 9 deletions src/common/lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@
/**
* @brief Load the bare minimum required to use Lua.
*/
auto lua_init() -> sol::state lua
auto lua_init() -> sol::state
{
TracyZoneScoped;

sol::state lua;

lua.open_libraries();

// Globally require bit library
Expand Down Expand Up @@ -64,6 +66,8 @@ auto lua_init() -> sol::state lua
result.get<sol::table>()["start"];
ShowInfo("Started script debugger");
}

return lua;
}

/**
Expand Down Expand Up @@ -137,11 +141,11 @@ std::string lua_to_string_depth(sol::state_view lua, const sol::object& obj, std
{
if (keyObj.get_type() == sol::type::string)
{
stringVec.emplace_back(fmt::format("{}{}: {}", indent, lua_to_string_depth(keyObj, 0), lua_to_string_depth(valObj, depth + 1)));
stringVec.emplace_back(fmt::format("{}{}: {}", indent, lua_to_string_depth(lua, keyObj, 0), lua_to_string_depth(lua, valObj, depth + 1)));
}
else
{
stringVec.emplace_back(fmt::format("{}{}", indent, lua_to_string_depth(valObj, depth + 1)));
stringVec.emplace_back(fmt::format("{}{}", indent, lua_to_string_depth(lua, valObj, depth + 1)));
}
}

Expand All @@ -167,7 +171,7 @@ std::string lua_to_string_depth(sol::state_view lua, const sol::object& obj, std
/**
* @brief
*/
std::string lua_to_string(sol::variadic_args va)
std::string lua_to_string(sol::state_view lua, sol::variadic_args va)
{
TracyZoneScoped;

Expand All @@ -184,7 +188,7 @@ std::string lua_to_string(sol::variadic_args va)
}
else
{
vec.emplace_back(lua_to_string_depth(va[i], 0));
vec.emplace_back(lua_to_string_depth(lua, va[i], 0));
}
}

Expand All @@ -194,14 +198,14 @@ std::string lua_to_string(sol::variadic_args va)
/**
* @brief
*/
void lua_print(sol::variadic_args va)
void lua_print(sol::state_view lua, sol::variadic_args va)
{
TracyZoneScoped;

ShowLua(lua_to_string(va).c_str());
ShowLua(lua_to_string(lua, va).c_str());
}

std::string lua_fmt(const std::string& fmtStr, sol::variadic_args va)
std::string lua_fmt(sol::state_view lua, const std::string& fmtStr, sol::variadic_args va)
{
fmt::dynamic_format_arg_store<fmt::format_context> store;
for (auto const& arg : va)
Expand Down Expand Up @@ -232,7 +236,7 @@ std::string lua_fmt(const std::string& fmtStr, sol::variadic_args va)
}
default:
{
store.push_back(lua_to_string_depth(arg, 0));
store.push_back(lua_to_string_depth(lua, arg, 0));
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/common/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

auto lua_init() -> sol::state;
auto lua_to_string_depth(sol::state_view lua, const sol::object& obj, std::size_t depth) -> std::string;
auto lua_to_string(sol::variadic_args va) -> std::string;
void lua_print(sol::variadic_args va);
auto lua_fmt(const std::string& fmtStr, sol::variadic_args va) -> std::string;
auto lua_to_string(sol::state_view lua, sol::variadic_args va) -> std::string;
void lua_print(sol::state_view lua, sol::variadic_args va);
auto lua_fmt(sol::state_view lua, const std::string& fmtStr, sol::variadic_args va) -> std::string;

#endif // _LUA_H
5 changes: 5 additions & 0 deletions src/common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ namespace settings
namespace detail
{
std::unordered_map<std::string, SettingsVariant_t> settingsMap;

auto getSettingsMap() -> std::unordered_map<std::string, SettingsVariant_t>&
{
return settingsMap;
}
}

// We need this to figure out which environment variables are numbers
Expand Down
2 changes: 1 addition & 1 deletion src/common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace settings

// arg = type held inside the variant
std::visit(
overloaded{
detail::overloaded{
[&](bool const& arg)
{
if constexpr (std::is_same_v<T, bool>)
Expand Down
2 changes: 1 addition & 1 deletion src/common/sql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ int32 SqlConnection::QueryStr(const char* query)
auto endTime = hires_clock::now();
auto dTime = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime);

if (gProcessLoaded && settings::get<bool>("logging.SQL_SLOW_QUERY_LOG_ENABLE"))
if (settings::get<bool>("logging.SQL_SLOW_QUERY_LOG_ENABLE"))
{
if (dTime > std::chrono::milliseconds(settings::get<uint32>("logging.SQL_SLOW_QUERY_ERROR_TIME")))
{
Expand Down
Loading

0 comments on commit e862698

Please sign in to comment.