Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
CliRunnable.cpp
Go to the documentation of this file.
1
/*
2
* This file is part of Project SkyFire https://www.projectskyfire.org.
3
* See LICENSE.md file for Copyright information
4
*/
5
9
10
#include "
Common.h
"
11
#include "
Configuration/Config.h
"
12
#include "
ObjectMgr.h
"
13
#include "
World.h
"
14
#include "
WorldSession.h
"
15
16
#include "
AccountMgr.h
"
17
#include "
Chat.h
"
18
#include "
CliRunnable.h
"
19
#include "
Language.h
"
20
#include "
Log.h
"
21
#include "
MapManager.h
"
22
#include "
Player.h
"
23
#include "
Util.h
"
24
25
#if PLATFORM != PLATFORM_WINDOWS
26
#include <readline/readline.h>
27
#include <readline/history.h>
28
29
char
*
command_finder
(
const
char
* text,
int
state)
30
{
31
static
size_t
idx, len;
32
const
char
* ret;
33
std::vector<ChatCommand>
const
& cmd =
ChatHandler::getCommandTable
();
34
35
if
(!state)
36
{
37
idx = 0;
38
len = strlen(text);
39
}
40
41
while
(idx < cmd.size())
42
{
43
ret = cmd[idx].Name;
44
if
(!cmd[idx].AllowConsole)
45
{
46
++idx;
47
continue
;
48
}
49
50
++idx;
51
//printf("Checking %s \n", cmd[idx].Name);
52
if
(strncmp(ret, text, len) == 0)
53
return
strdup(ret);
54
}
55
56
return
((
char
*)NULL);
57
}
58
59
char
**
cli_completion
(
const
char
* text,
int
start,
int
/*end*/
)
60
{
61
char
** matches = NULL;
62
63
if
(start)
64
rl_bind_key(
'\t'
, rl_abort);
65
else
66
matches = rl_completion_matches((
char
*)text, &
command_finder
);
67
return
matches;
68
}
69
70
int
cli_hook_func
()
71
{
72
if
(
World::IsStopped
())
73
rl_done = 1;
74
return
0;
75
}
76
77
#endif
78
79
void
utf8print
(
void
*
/*arg*/
,
const
char
* str)
80
{
81
#if PLATFORM == PLATFORM_WINDOWS
82
wchar_t
wtemp_buf[6000];
83
size_t
wtemp_len = 6000 - 1;
84
if
(!
Utf8toWStr
(str, strlen(str), wtemp_buf, wtemp_len))
85
return
;
86
87
char
temp_buf[6000];
88
CharToOemBuffW(&wtemp_buf[0], &temp_buf[0], wtemp_len + 1);
89
printf(temp_buf);
90
#else
91
{
92
printf(
"%s"
, str);
93
fflush(stdout);
94
}
95
#endif
96
}
97
98
void
commandFinished
(
void
*,
bool
/*success*/
)
99
{
100
printf(
"SF> "
);
101
fflush(stdout);
102
}
103
105
void
CliRunnable::Run
()
106
{
108
//SF_LOG_INFO("server.worldserver", "");
109
#if PLATFORM != PLATFORM_WINDOWS
110
rl_attempted_completion_function =
cli_completion
;
111
rl_event_hook =
cli_hook_func
;
112
#endif
113
114
if
(
sConfigMgr
->GetBoolDefault(
"BeepAtStart"
,
true
))
115
printf(
"\a"
);
// \a = Alert
116
117
// print this here the first time
118
// later it will be printed after command queue updates
119
printf(
"SF>"
);
120
122
while
(!
World::IsStopped
())
123
{
124
fflush(stdout);
125
126
char
* command_str;
// = fgets(commandbuf, sizeof(commandbuf), stdin);
127
128
#if PLATFORM == PLATFORM_WINDOWS
129
char
commandbuf[256];
130
command_str = fgets(commandbuf,
sizeof
(commandbuf), stdin);
131
#else
132
command_str = readline(
"SF>"
);
133
rl_bind_key(
'\t'
, rl_complete);
134
#endif
135
136
if
(command_str != NULL)
137
{
138
for
(
int
x = 0; command_str[x]; ++x)
139
if
(command_str[x] ==
'\r'
|| command_str[x] ==
'\n'
)
140
{
141
command_str[x] = 0;
142
break
;
143
}
144
145
if
(!*command_str)
146
{
147
#if PLATFORM == PLATFORM_WINDOWS
148
printf(
"SF>"
);
149
#else
150
free(command_str);
151
#endif
152
continue
;
153
}
154
155
std::string command;
156
if
(!
consoleToUtf8
(command_str, command))
// convert from console encoding to utf8
157
{
158
#if PLATFORM == PLATFORM_WINDOWS
159
printf(
"SF>"
);
160
#else
161
free(command_str);
162
#endif
163
continue
;
164
}
165
166
fflush(stdout);
167
sWorld
->QueueCliCommand(
new
CliCommandHolder
(NULL, command.c_str(), &
utf8print
, &
commandFinished
));
168
#if PLATFORM != PLATFORM_WINDOWS
169
add_history(command.c_str());
170
free(command_str);
171
#endif
172
}
173
else
if
(feof(stdin))
174
{
175
World::StopNow
(
SHUTDOWN_EXIT_CODE
);
176
}
177
}
178
}
AccountMgr.h
Chat.h
CliRunnable.h
Common.h
Config.h
sConfigMgr
#define sConfigMgr
Definition
Config.h:64
Language.h
Log.h
MapManager.h
ObjectMgr.h
Player.h
consoleToUtf8
bool consoleToUtf8(const std::string &conStr, std::string &utf8str)
Definition
Util.cpp:458
Utf8toWStr
bool Utf8toWStr(char const *utf8str, size_t csize, wchar_t *wstr, size_t &wsize)
Definition
Util.cpp:305
Util.h
World.h
WorldSession.h
ChatHandler::getCommandTable
static std::vector< ChatCommand > const & getCommandTable()
Definition
Chat.cpp:29
World::StopNow
static void StopNow(uint8 exitcode)
Definition
World.h:674
World::IsStopped
static bool IsStopped()
Definition
World.h:675
CliRunnable::Run
void Run()
Thread start
Definition
CliRunnable.cpp:105
utf8print
void utf8print(void *, const char *str)
Definition
CliRunnable.cpp:79
cli_completion
char ** cli_completion(const char *text, int start, int)
Definition
CliRunnable.cpp:59
command_finder
char * command_finder(const char *text, int state)
Definition
CliRunnable.cpp:29
cli_hook_func
int cli_hook_func()
Definition
CliRunnable.cpp:70
commandFinished
void commandFinished(void *, bool)
Definition
CliRunnable.cpp:98
sWorld
#define sWorld
Definition
World.h:910
SHUTDOWN_EXIT_CODE
@ SHUTDOWN_EXIT_CODE
Definition
World.h:64
CliCommandHolder
Storage class for commands issued for delayed execution.
Definition
World.h:524
src
server
worldserver
CommandLine
CliRunnable.cpp
Generated on
for Project SkyFire Core by
1.17.0