/
Console.cpp
626 lines (551 loc) · 24.1 KB
/
Console.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/Console/Console.h>
#include <AzCore/Console/ILogger.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Serialization/Json/JsonSerializationSettings.h>
#include <AzCore/Settings/CommandLine.h>
#include <AzCore/Settings/SettingsRegistryMergeUtils.h>
#include <AzCore/StringFunc/StringFunc.h>
#include <AzCore/Utils/Utils.h>
#include <AzCore/IO/FileIO.h>
#include <AzCore/IO/Path/Path.h>
#include <AzCore/IO/SystemFile.h>
#include <cctype>
namespace AZ
{
uint32_t CountMatchingPrefixes(const AZStd::string_view& string, const ConsoleCommandContainer& stringSet)
{
uint32_t count = 0;
for (AZStd::string_view iter : stringSet)
{
if (StringFunc::StartsWith(iter, string, false))
{
++count;
}
}
return count;
}
Console::Console()
: m_head(nullptr)
{
}
Console::Console(AZ::SettingsRegistryInterface& settingsRegistryInterface)
: Console()
{
RegisterCommandInvokerWithSettingsRegistry(settingsRegistryInterface);
}
Console::~Console()
{
// on console destruction relink the console functors back to the deferred head
MoveFunctorsToDeferredHead(AZ::ConsoleFunctorBase::GetDeferredHead());
}
PerformCommandResult Console::PerformCommand
(
const char* command,
ConsoleSilentMode silentMode,
ConsoleInvokedFrom invokedFrom,
ConsoleFunctorFlags requiredSet,
ConsoleFunctorFlags requiredClear
)
{
AZStd::string_view commandView;
ConsoleCommandContainer commandArgsView;
bool firstIteration = true;
auto ConvertCommandStringToArray = [&firstIteration, &commandView, &commandArgsView](AZStd::string_view token)
{
if (firstIteration)
{
commandView = token;
firstIteration = false;
}
else
{
commandArgsView.emplace_back(token);
};
};
constexpr AZStd::string_view commandSeparators = " \t\n\r";
StringFunc::TokenizeVisitor(command, ConvertCommandStringToArray, commandSeparators);
return PerformCommand(commandView, commandArgsView, silentMode, invokedFrom, requiredSet, requiredClear);
}
PerformCommandResult Console::PerformCommand
(
const ConsoleCommandContainer& commandAndArgs,
ConsoleSilentMode silentMode,
ConsoleInvokedFrom invokedFrom,
ConsoleFunctorFlags requiredSet,
ConsoleFunctorFlags requiredClear
)
{
if (commandAndArgs.empty())
{
return AZ::Failure("CommandAndArgs is empty.");
}
return PerformCommand(commandAndArgs.front(), ConsoleCommandContainer(commandAndArgs.begin() + 1, commandAndArgs.end()), silentMode, invokedFrom, requiredSet, requiredClear);
}
PerformCommandResult Console::PerformCommand
(
AZStd::string_view command,
const ConsoleCommandContainer& commandArgs,
ConsoleSilentMode silentMode,
ConsoleInvokedFrom invokedFrom,
ConsoleFunctorFlags requiredSet,
ConsoleFunctorFlags requiredClear
)
{
if (!DispatchCommand(command, commandArgs, silentMode, invokedFrom, requiredSet, requiredClear))
{
// If the command could not be dispatched at this time add it to the deferred commands queue
DeferredCommand deferredCommand{ AZStd::string_view{ command },
DeferredCommand::DeferredArguments{ commandArgs.begin(), commandArgs.end() },
silentMode,
invokedFrom,
requiredSet,
requiredClear };
CVarFixedString commandLowercase(command);
AZStd::to_lower(commandLowercase.begin(), commandLowercase.end());
m_deferredCommands.emplace_back(AZStd::move(deferredCommand));
return AZ::Failure(AZStd::string::format(
"Command \"%s\" is not yet registered. Deferring to attempt execution later.", commandLowercase.c_str()));
}
return AZ::Success();
}
void Console::ExecuteConfigFile(AZStd::string_view configFileName)
{
auto settingsRegistry = AZ::SettingsRegistry::Get();
// If the config file is a settings registry file use the SettingsRegistryInterface MergeSettingsFile function
// otherwise use the SettingsRegistryMergeUtils MergeSettingsToRegistry_ConfigFile function to merge an INI-style
// file to the settings registry
AZ::IO::PathView configFile(configFileName);
if (configFile.Extension() == ".setreg")
{
settingsRegistry->MergeSettingsFile(configFile.Native(), AZ::SettingsRegistryInterface::Format::JsonMergePatch);
}
else if (configFile.Extension() == ".setregpatch")
{
settingsRegistry->MergeSettingsFile(configFile.Native(), AZ::SettingsRegistryInterface::Format::JsonPatch);
}
else
{
AZ::SettingsRegistryMergeUtils::ConfigParserSettings configParserSettings;
configParserSettings.m_registryRootPointerPath = "/Amazon/AzCore/Runtime/ConsoleCommands";
configParserSettings.m_commandLineSettings.m_delimiterFunc = [](AZStd::string_view line)
{
SettingsRegistryInterface::CommandLineArgumentSettings::JsonPathValue pathValue;
AZStd::string_view parsedLine = line;
// Splits the line based on the <equal> or <colon>
if (auto path = AZ::StringFunc::TokenizeNext(parsedLine, "=:"); path.has_value())
{
pathValue.m_path = AZ::StringFunc::StripEnds(*path);
pathValue.m_value = AZ::StringFunc::StripEnds(parsedLine);
}
// If the value is empty, then the line either contained an equal sign followed only by whitespace or the line was empty
// 1. line="testInit=", pathValue.m_path="testInit", pathValue.m_value=""
// 2. line="testInit 1", pathValue.m_path="testInit 1", pathValue.m_value=""
// Therefore the path is split the path on whitespace in order to retrieve a value
if (pathValue.m_value.empty())
{
parsedLine = pathValue.m_path;
if (auto path = AZ::StringFunc::TokenizeNext(parsedLine, " \t"); path.has_value())
{
pathValue.m_path = AZ::StringFunc::StripEnds(*path);
pathValue.m_value = AZ::StringFunc::StripEnds(parsedLine);
}
}
return pathValue;
};
AZ::SettingsRegistryMergeUtils::MergeSettingsToRegistry_ConfigFile(*settingsRegistry, configFile.Native(), configParserSettings);
}
}
void Console::ExecuteCommandLine(const AZ::CommandLine& commandLine)
{
for (auto&& commandArgument : commandLine)
{
const auto& switchKey = commandArgument.m_option;
const auto& switchValue = commandArgument.m_value;
ConsoleCommandContainer commandArgs{ switchValue };
PerformCommand(switchKey, commandArgs, ConsoleSilentMode::NotSilent, ConsoleInvokedFrom::AzConsole, ConsoleFunctorFlags::Null, ConsoleFunctorFlags::Null);
}
}
bool Console::ExecuteDeferredConsoleCommands()
{
auto DeferredCommandCallable = [this](const DeferredCommand& deferredCommand)
{
return this->DispatchCommand(deferredCommand.m_command,
ConsoleCommandContainer(AZStd::from_range, deferredCommand.m_arguments), deferredCommand.m_silentMode,
deferredCommand.m_invokedFrom, deferredCommand.m_requiredSet, deferredCommand.m_requiredClear);
};
// Attempt to invoke the deferred command and remove it from the queue if successful
return AZStd::erase_if(m_deferredCommands, DeferredCommandCallable) != 0;
}
void Console::ClearDeferredConsoleCommands()
{
m_deferredCommands = {};
}
bool Console::HasCommand(AZStd::string_view command, ConsoleFunctorFlags ignoreAnyFlags)
{
return FindCommand(command, ignoreAnyFlags) != nullptr;
}
ConsoleFunctorBase* Console::FindCommand(AZStd::string_view command, ConsoleFunctorFlags ignoreAnyFlags)
{
CVarFixedString lowerName(command);
AZStd::to_lower(lowerName.begin(), lowerName.end());
CommandMap::iterator iter = m_commands.find(lowerName);
if (iter != m_commands.end())
{
for (ConsoleFunctorBase* curr : iter->second)
{
if ((curr->GetFlags() & ignoreAnyFlags) != ConsoleFunctorFlags::Null)
{
// Filter functors marked as invisible
continue;
}
return curr;
}
}
return nullptr;
}
AZStd::string Console::AutoCompleteCommand(AZStd::string_view command, AZStd::vector<AZStd::string>* matches)
{
if (command.empty())
{
return command;
}
ConsoleCommandContainer commandSubset;
for (const auto& functor : m_commands)
{
if (functor.second.empty())
{
continue;
}
// Filter functors registered with the same name
const ConsoleFunctorBase* curr = functor.second.front();
if ((curr->GetFlags() & ConsoleFunctorFlags::IsInvisible) == ConsoleFunctorFlags::IsInvisible)
{
// Filter functors marked as invisible
continue;
}
if (StringFunc::StartsWith(curr->m_name, command, false))
{
AZLOG_INFO("- %s : %s", curr->m_name, curr->m_desc);
if (commandSubset.size() < MaxConsoleCommandPlusArgsLength)
{
commandSubset.push_back(curr->m_name);
}
if (matches)
{
matches->push_back(curr->m_name);
}
}
}
AZStd::string largestSubstring = command;
if ((!largestSubstring.empty()) && (!commandSubset.empty()))
{
const uint32_t totalCount = CountMatchingPrefixes(command, commandSubset);
for (size_t i = largestSubstring.length(); i < commandSubset.front().length(); ++i)
{
const AZStd::string nextSubstring = largestSubstring + commandSubset.front()[i];
const uint32_t count = CountMatchingPrefixes(nextSubstring, commandSubset);
if (count < totalCount)
{
break;
}
largestSubstring = nextSubstring;
}
}
return largestSubstring;
}
void Console::VisitRegisteredFunctors(const FunctorVisitor& visitor)
{
for (auto& curr : m_commands)
{
if (!curr.second.empty())
{
visitor(curr.second.front());
}
}
}
void Console::RegisterFunctor(ConsoleFunctorBase* functor)
{
if (functor->m_console && (functor->m_console != this))
{
AZ_Assert(false, "Functor is already registered to a console");
return;
}
CVarFixedString lowerName = functor->GetName();
AZStd::to_lower(lowerName.begin(), lowerName.end());
CommandMap::iterator iter = m_commands.find(lowerName);
if (iter != m_commands.end())
{
// Validate we haven't already added this cvar
AZStd::vector<ConsoleFunctorBase*>::iterator iter2 = AZStd::find(iter->second.begin(), iter->second.end(), functor);
if (iter2 != iter->second.end())
{
AZ_Assert(false, "Duplicate functor registered to the console");
return;
}
// If multiple cvars are registered with the same name, validate that the types and flags match
if (!iter->second.empty())
{
ConsoleFunctorBase* front = iter->second.front();
if (front->GetFlags() != functor->GetFlags() || front->GetTypeId() != functor->GetTypeId())
{
AZ_Assert(false, "Mismatched console functor types registered under the same name");
return;
}
// Discard duplicate functors if the 'DontDuplicate' flag has been set
if ((front->GetFlags() & ConsoleFunctorFlags::DontDuplicate) != ConsoleFunctorFlags::Null)
{
return;
}
}
}
m_commands[lowerName].emplace_back(functor);
functor->Link(m_head);
functor->m_console = this;
}
void Console::UnregisterFunctor(ConsoleFunctorBase* functor)
{
if (functor->m_console != this)
{
AZ_Assert(false, "Unregistering a functor bound to a different console");
return;
}
CVarFixedString lowerName = functor->GetName();
AZStd::to_lower(lowerName.begin(), lowerName.end());
CommandMap::iterator iter = m_commands.find(lowerName);
if (iter != m_commands.end())
{
AZStd::vector<ConsoleFunctorBase*>::iterator iter2 = AZStd::find(iter->second.begin(), iter->second.end(), functor);
if (iter2 != iter->second.end())
{
iter->second.erase(iter2);
}
if (iter->second.empty())
{
m_commands.erase(iter);
}
}
functor->Unlink(m_head);
functor->m_console = nullptr;
}
void Console::LinkDeferredFunctors(ConsoleFunctorBase*& deferredHead)
{
ConsoleFunctorBase* curr = deferredHead;
while (curr != nullptr)
{
ConsoleFunctorBase* next = curr->m_next;
curr->Unlink(deferredHead);
RegisterFunctor(curr);
curr->m_isDeferred = false;
curr = next;
}
deferredHead = nullptr;
}
void Console::MoveFunctorsToDeferredHead(ConsoleFunctorBase*& deferredHead)
{
m_commands.clear();
// Re-initialize all of the current functors to a deferred state
for (ConsoleFunctorBase* curr = m_head; curr != nullptr; curr = curr->m_next)
{
curr->m_console = nullptr;
curr->m_isDeferred = true;
}
// If the deferred head contains unregistered functors
// move this AZ::Console functors list to the end of the deferred functors
if (deferredHead)
{
ConsoleFunctorBase* oldDeferred = deferredHead;
while (oldDeferred->m_next != nullptr)
{
oldDeferred = oldDeferred->m_next;
}
oldDeferred->Link(m_head);
}
else
{
deferredHead = m_head;
}
ConsoleFunctorBase::s_deferredHeadInvoked = false;
m_head = nullptr;
}
bool Console::DispatchCommand
(
AZStd::string_view command,
const ConsoleCommandContainer& inputs,
ConsoleSilentMode silentMode,
ConsoleInvokedFrom invokedFrom,
ConsoleFunctorFlags requiredSet,
ConsoleFunctorFlags requiredClear
)
{
bool result = false;
ConsoleFunctorFlags flags = ConsoleFunctorFlags::Null;
CVarFixedString lowerName(command);
AZStd::to_lower(lowerName.begin(), lowerName.end());
CommandMap::iterator iter = m_commands.find(lowerName);
if (iter != m_commands.end())
{
for (ConsoleFunctorBase* curr : iter->second)
{
if ((curr->GetFlags() & requiredSet) != requiredSet)
{
AZLOG_WARN("%s failed required set flag check", curr->m_name);
continue;
}
if ((curr->GetFlags() & requiredClear) != ConsoleFunctorFlags::Null)
{
AZLOG_WARN("%s failed required clear flag check", curr->m_name);
continue;
}
if ((curr->GetFlags() & ConsoleFunctorFlags::IsCheat) != ConsoleFunctorFlags::Null)
{
AZLOG_WARN("%s is marked as a cheat", curr->m_name);
}
if ((curr->GetFlags() & ConsoleFunctorFlags::IsDeprecated) != ConsoleFunctorFlags::Null)
{
AZLOG_WARN("%s is marked as deprecated", curr->m_name);
}
if ((curr->GetFlags() & ConsoleFunctorFlags::NeedsReload) != ConsoleFunctorFlags::Null)
{
AZLOG_WARN("Changes to %s will only take effect after level reload", curr->m_name);
}
// Letting this intentionally fall-through, since in editor we can register common variables multiple times
(*curr)(inputs);
if (!result)
{
result = true;
if ((silentMode == ConsoleSilentMode::NotSilent) && (curr->GetFlags() & ConsoleFunctorFlags::IsInvisible) != ConsoleFunctorFlags::IsInvisible)
{
CVarFixedString value;
curr->GetValue(value);
AZLOG_INFO("> %s : %s", curr->GetName(), value.empty() ? "<empty>" : value.c_str());
}
flags = curr->GetFlags();
}
}
}
if (result)
{
m_consoleCommandInvokedEvent.Signal(command, inputs, flags, invokedFrom);
}
else
{
m_dispatchCommandNotFoundEvent.Signal(command, inputs, invokedFrom);
}
return result;
}
struct ConsoleCommandKeyNotificationHandler
{
ConsoleCommandKeyNotificationHandler(AZ::SettingsRegistryInterface& registry, Console& console)
: m_settingsRegistry(registry)
, m_console(console)
{
}
// NotifyEventHandler function for handling when settings underneath the
// ConsoleAutoexecCommandKey or ConsoleRuntimeCommandKey key paths are modified
void operator()(const AZ::SettingsRegistryInterface::NotifyEventArgs& notifyEventArgs)
{
using FixedValueString = AZ::SettingsRegistryInterface::FixedValueString;
if (notifyEventArgs.m_type == AZ::SettingsRegistryInterface::Type::NoType)
{
return;
}
constexpr AZ::IO::PathView consoleRuntimeCommandKey{ IConsole::ConsoleRuntimeCommandKey, AZ::IO::PosixPathSeparator };
constexpr AZ::IO::PathView consoleAutoexecCommandKey{ IConsole::ConsoleAutoexecCommandKey, AZ::IO::PosixPathSeparator };
AZ::IO::PathView inputKey{ notifyEventArgs.m_jsonKeyPath, AZ::IO::PosixPathSeparator };
// Abuses the IsRelativeToFuncton function of the path class to extract the console
// command from the settings registry objects
FixedValueString command;
if (inputKey != consoleRuntimeCommandKey && inputKey.IsRelativeTo(consoleRuntimeCommandKey))
{
command = inputKey.LexicallyRelative(consoleRuntimeCommandKey).Native();
}
else if (inputKey != consoleAutoexecCommandKey && inputKey.IsRelativeTo(consoleAutoexecCommandKey))
{
command = inputKey.LexicallyRelative(consoleAutoexecCommandKey).Native();
}
if (!command.empty())
{
ConsoleCommandContainer commandArgs;
// Argument string which stores the value from the Settings Registry long enough
// to pass into the PerformCommand. The ConsoleCommandContainer stores string_views
// and therefore doesn't own the memory.
FixedValueString commandArgString;
if (notifyEventArgs.m_type == SettingsRegistryInterface::Type::String)
{
if (m_settingsRegistry.Get(commandArgString, notifyEventArgs.m_jsonKeyPath))
{
auto ConvertCommandArgumentToArray = [&commandArgs](AZStd::string_view token)
{
commandArgs.emplace_back(token);
};
constexpr AZStd::string_view commandSeparators = " \t\n\r";
StringFunc::TokenizeVisitor(commandArgString, ConvertCommandArgumentToArray, commandSeparators);
}
}
else if (notifyEventArgs.m_type == SettingsRegistryInterface::Type::Boolean)
{
bool commandArgBool{};
if (m_settingsRegistry.Get(commandArgBool, notifyEventArgs.m_jsonKeyPath))
{
commandArgString = commandArgBool ? "true" : "false";
commandArgs.emplace_back(commandArgString);
}
}
else if (notifyEventArgs.m_type == SettingsRegistryInterface::Type::Integer)
{
// Try converting to a signed 64-bit number first and then an unsigned 64-bit number
AZ::s64 commandArgInt{};
AZ::u64 commandArgUInt{};
if (m_settingsRegistry.Get(commandArgInt, notifyEventArgs.m_jsonKeyPath))
{
AZStd::to_string(commandArgString, commandArgInt);
commandArgs.emplace_back(commandArgString);
}
else if (m_settingsRegistry.Get(commandArgUInt, notifyEventArgs.m_jsonKeyPath))
{
AZStd::to_string(commandArgString, commandArgUInt);
commandArgs.emplace_back(commandArgString);
}
}
else if (notifyEventArgs.m_type == SettingsRegistryInterface::Type::FloatingPoint)
{
double commandArgFloat{};
if (m_settingsRegistry.Get(commandArgFloat, notifyEventArgs.m_jsonKeyPath))
{
AZStd::to_string(commandArgString, commandArgFloat);
commandArgs.emplace_back(commandArgString);
}
}
CVarFixedString commandTrace(command);
for (AZStd::string_view commandArg : commandArgs)
{
commandTrace.push_back(' ');
commandTrace += commandArg;
}
m_console.PerformCommand(command, commandArgs, ConsoleSilentMode::NotSilent,
ConsoleInvokedFrom::AzConsole, ConsoleFunctorFlags::Null, ConsoleFunctorFlags::Null);
}
}
AZ::Console& m_console;
AZ::SettingsRegistryInterface& m_settingsRegistry;
AZStd::string scratchBuffer;
};
void Console::RegisterCommandInvokerWithSettingsRegistry(AZ::SettingsRegistryInterface& settingsRegistry)
{
// Make sure the there is a JSON object at the ConsoleRuntimeCommandKey or ConsoleAutoexecKey
// So that JSON Patch is able to add values underneath that object (JSON Patch doesn't create intermediate objects)
settingsRegistry.MergeSettings(R"({})", SettingsRegistryInterface::Format::JsonMergePatch,
IConsole::ConsoleRuntimeCommandKey);
settingsRegistry.MergeSettings(R"({})", SettingsRegistryInterface::Format::JsonMergePatch,
IConsole::ConsoleAutoexecCommandKey);
m_consoleCommandKeyHandler = settingsRegistry.RegisterNotifier(ConsoleCommandKeyNotificationHandler{ settingsRegistry, *this });
settingsRegistry.SetNotifyForMergeOperations(true);
}
}