feat(Core/Chat): new argument parsing and unify chat hyperlink parsing (#6243)

This commit is contained in:
Kargatum
2021-10-23 15:15:42 +07:00
committed by GitHub
parent 1101f9dd2a
commit bc9473482e
90 changed files with 4280 additions and 2508 deletions

View File

@@ -19,19 +19,22 @@
/// @{
/// \file
#include "AccountMgr.h"
#include "Chat.h"
#include "CliRunnable.h"
#include "Common.h"
#include "Config.h"
#include "Language.h"
#include "Log.h"
#include "MapMgr.h"
#include "Errors.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "Util.h"
#include "World.h"
#include "WorldSession.h"
#include "Config.h"
#include "CliRunnable.h"
#include "Log.h"
#include "Util.h"
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
#include "Chat.h"
#include "ChatCommand.h"
#include <cstring>
#include <readline/readline.h>
#include <readline/history.h>
#endif
static constexpr char CLI_PREFIX[] = "AC> ";
@@ -41,75 +44,49 @@ static inline void PrintCliPrefix()
}
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
#include <readline/readline.h>
#include <readline/history.h>
char* command_finder(const char* text, int state)
namespace Acore::Impl::Readline
{
static size_t idx, len;
const char* ret;
std::vector<ChatCommand> const& cmd = ChatHandler::getCommandTable();
if (!state)
static std::vector<std::string> vec;
char* cli_unpack_vector(char const*, int state)
{
idx = 0;
len = strlen(text);
static size_t i=0;
if (!state)
i = 0;
if (i < vec.size())
return strdup(vec[i++].c_str());
else
return nullptr;
}
while (idx < cmd.size())
char** cli_completion(char const* text, int /*start*/, int /*end*/)
{
ret = cmd[idx].Name;
if (!cmd[idx].AllowConsole)
{
++idx;
continue;
}
++idx;
//printf("Checking %s \n", cmd[idx].Name);
if (strncmp(ret, text, len) == 0)
return strdup(ret);
::rl_attempted_completion_over = 1;
vec = Acore::ChatCommands::GetAutoCompletionsFor(CliHandler(nullptr,nullptr), text);
return ::rl_completion_matches(text, &cli_unpack_vector);
}
return ((char*)nullptr);
int cli_hook_func()
{
if (World::IsStopped())
::rl_done = 1;
return 0;
}
}
char** cli_completion(const char* text, int start, int /*end*/)
{
char** matches = nullptr;
if (start)
rl_bind_key('\t', rl_abort);
else
matches = rl_completion_matches((char*)text, &command_finder);
return matches;
}
int cli_hook_func()
{
if (World::IsStopped())
rl_done = 1;
return 0;
}
#endif
void utf8print(void* /*arg*/, const char* str)
void utf8print(void* /*arg*/, std::string_view str)
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
wchar_t wtemp_buf[6000];
size_t wtemp_len = 6000 - 1;
if (!Utf8toWStr(str, strlen(str), wtemp_buf, wtemp_len))
std::wstring wbuf;
if (!Utf8toWStr(str, wbuf))
return;
char temp_buf[6000];
CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len + 1);
printf(temp_buf);
wprintf(L"%s", wbuf.c_str());
#else
{
printf("%s", str);
fflush(stdout);
}
{
printf(STRING_VIEW_FMT, STRING_VIEW_FMT_ARG(str));
fflush(stdout);
}
#endif
}
@@ -129,7 +106,7 @@ int kb_hit_return()
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(STDIN_FILENO, &fds);
select(STDIN_FILENO + 1, &fds, nullptr, nullptr, &tv);
select(STDIN_FILENO+1, &fds, nullptr, nullptr, &tv);
return FD_ISSET(STDIN_FILENO, &fds);
}
#endif
@@ -137,15 +114,21 @@ int kb_hit_return()
/// %Thread start
void CliThread()
{
///- Display the list of available CLI functions then beep
//TC_LOG_INFO("server.worldserver", "");
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
rl_attempted_completion_function = cli_completion;
rl_event_hook = cli_hook_func;
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
// print this here the first time
// later it will be printed after command queue updates
PrintCliPrefix();
#else
::rl_attempted_completion_function = &Acore::Impl::Readline::cli_completion;
{
static char BLANK = '\0';
::rl_completer_word_break_characters = &BLANK;
}
::rl_event_hook = &Acore::Impl::Readline::cli_hook_func;
#endif
if (sConfigMgr->GetOption<bool>("BeepAtStart", true))
printf("\a"); // \a = Alert
printf("\a"); // \a = Alert
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
if (sConfigMgr->GetOption<bool>("FlashAtStart", true))
@@ -160,60 +143,53 @@ void CliThread()
}
#endif
// print this here the first time
// later it will be printed after command queue updates
PrintCliPrefix();
///- As long as the World is running (no World::m_stopEvent), get the command line and handle it
while (!World::IsStopped())
{
fflush(stdout);
char* command_str ; // = fgets(commandbuf, sizeof(commandbuf), stdin);
std::string command;
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
char commandbuf[256];
command_str = fgets(commandbuf, sizeof(commandbuf), stdin);
#else
command_str = readline(CLI_PREFIX);
rl_bind_key('\t', rl_complete);
#endif
if (command_str != nullptr)
wchar_t commandbuf[256];
if (fgetws(commandbuf, sizeof(commandbuf), stdin))
{
for (int x = 0; command_str[x]; ++x)
if (command_str[x] == '\r' || command_str[x] == '\n')
{
command_str[x] = 0;
break;
}
if (!*command_str)
if (!WStrToUtf8(commandbuf, wcslen(commandbuf), command))
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
PrintCliPrefix();
#else
free(command_str);
#endif
continue;
}
std::string command;
if (!consoleToUtf8(command_str, command)) // convert from console encoding to utf8
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
PrintCliPrefix();
}
#else
free(command_str);
char* command_str = readline(CLI_PREFIX);
::rl_bind_key('\t', ::rl_complete);
if (command_str != nullptr)
{
command = command_str;
free(command_str);
}
#endif
continue;
if (!command.empty())
{
std::size_t nextLineIndex = command.find_first_of("\r\n");
if (nextLineIndex != std::string::npos)
{
if (nextLineIndex == 0)
{
#if AC_PLATFORM == AC_PLATFORM_WINDOWS
PrintCliPrefix();
#endif
continue;
}
command.erase(nextLineIndex);
}
fflush(stdout);
sWorld->QueueCliCommand(new CliCommandHolder(nullptr, command.c_str(), &utf8print, &commandFinished));
#if AC_PLATFORM != AC_PLATFORM_WINDOWS
add_history(command.c_str());
free(command_str);
#endif
}
else if (feof(stdin))