-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCmdSystem.cpp
203 lines (167 loc) · 4.26 KB
/
CmdSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "CmdSystem.h"
#include "Console.h"
#include "CVarSystem.h"
#include <charconv>
void Echo_f( const CommandArgs &args )
{
if( args.Argc() < 2 )
{
con.Print( "usage: echo [arg]" );
return;
}
std::string line;
line.reserve( 64 );
for( auto i = 1u; i < args.Argc(); i++ )
{
line.append( args[i].data() );
line.append( 1, ' ' );
}
con.Print( line );
}
void List_f( const CommandArgs &args )
{
const auto &self = commandSys;
for( auto &&[key, val] : self.commands )
{
con.Printf( "{}\t{}", val.name.data(), val.description.data() );
}
}
void Wait_f( const CommandArgs &args )
{
if( args.Argc() == 2 )
{
unsigned int cyclesToWait = 0;
std::from_chars( args[1].data(), args[1].data() + args[1].length(), cyclesToWait );
commandSys.setWait( cyclesToWait );
return;
}
con.Print( "usage: wait [arg]" );
}
CmdSystem::CmdSystem()
{
buffer.reserve( MAX_BUFFER_SIZE );
AddCommand( "echo", Echo_f, "print text" );
AddCommand( "wait", Wait_f, "dealys one or more frames" );
AddCommand( "list", List_f, "print command list" );
AddCommand( "help", List_f, "print command list" );
}
void CmdSystem::AddCommand( const std::string &name, cmdFunction_t function, std::string_view desc )
{
auto i = commands.find( name );
if( i == commands.end() )
{
commands.emplace( std::piecewise_construct, std::forward_as_tuple( name ),
std::forward_as_tuple( name, function, desc ) );
}
else
{
con.Printf( "Function '{}' already defined", name.data() );
}
}
void CmdSystem::ExecuteCommandBuffer()
{
unsigned int head = 0;
while( buffLength )
{
if( !wait )
{
wait--;
break;
}
const auto pos = buffer.find( '\n', head );
if( pos != std::string::npos )
{
std::string line( buffer.substr( head, pos - head ) );
ExecuteCommandText( line );
head = pos + 1;
buffLength -= line.length() + 1;
}
}
}
// add command without '\n' at end
void CmdSystem::AppendCommandText( std::string_view text )
{
if( text.empty() )
{
return;
}
const auto len = text.length();
if( ( buffLength + len ) > MAX_BUFFER_SIZE )
{
con.Print( "AppendCommandText: Buffer overflow" );
return;
}
buffer.insert( buffLength, text );
buffLength += len;
}
// add command with '\n' at end
void CmdSystem::InsertCommandText( std::string_view text )
{
if( text.empty() )
{
return;
}
const auto len = text.length() + 1;
if( ( buffLength + len ) > MAX_BUFFER_SIZE )
{
con.Print( "InsertCommandText: Buffer overflow" );
return;
}
buffer.insert( buffLength, text );
buffLength += text.length();
buffer.insert( buffLength, "\n" );
buffLength += 1;
}
void CmdSystem::ExecuteCommandText( std::string_view text )
{
unsigned int strBegin = 0u;
unsigned int strEnd = 0u;
while( strEnd < text.length() )
{
int quotes = 0;
for( ; strEnd < text.length(); strEnd++ )
{
if( text[strEnd] == '"' )
{
quotes++;
}
if( !( quotes & 1 ) && text[strEnd] == ';' )
{
break;
}
}
execute( CommandArgs( text.substr( strBegin, strEnd - strBegin ) ) );
strEnd++;
strBegin = strEnd;
}
}
std::vector<std::pair<std::string, std::string>> CmdSystem::GetAutocompleteList( std::string_view name )
{
std::vector<std::pair<std::string, std::string>> list;
list.reserve( 8 );
for( auto &&[key, value] : commands )
{
if( !value.name.compare( 0, name.length(), name ) )
{
list.emplace_back( value.name, value.description );
}
}
return list;
}
void CmdSystem::execute( const CommandArgs &args )
{
if( !args.Argc() )
{
return;
}
if( auto i = commands.find( args[0] ); i != commands.end() )
{
i->second.function( args );
return;
}
if( cvarSystem.Command( args ) )
{
return;
}
con.Printf( "'{}': unknown command", args[0].data() );
}