Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
GUIConsoleCommandParser.cpp
Go to the documentation of this file.
1
2
4
5#include "Headers/GUI.h"
6
15
18
21
22namespace Divide {
23
26{
27 _commands[_ID("say")] = [this](const string& args) { handleSayCommand(args); };
28 _commands[_ID("quit")] = [this](const string& args) { handleQuitCommand(args); };
29 _commands[_ID("help")] = [this](const string& args) { handleHelpCommand(args); };
30 _commands[_ID("editparam")] = [this](const string& args) { handleEditParamCommand(args); };
31 _commands[_ID("playsound")] = [this](const string& args) { handlePlaySoundCommand(args); };
32 _commands[_ID("createnavmesh")] = [this](const string& args) { handleNavMeshCommand(args); };
33 _commands[_ID("setfov")] = [this](const string& args) { handleFOVCommand(args); };
34 _commands[_ID("invalidcommand")] = [this](const string& args) { handleInvalidCommand(args); };
35 _commands[_ID("recompileshader")] = [this](const string& args) { handleShaderRecompileCommand(args); };
36
37 _commandHelp[_ID("say")] = LOCALE_STR("CONSOLE_SAY_COMMAND_HELP");
38 _commandHelp[_ID("quit")] = LOCALE_STR("CONSOLE_QUIT_COMMAND_HELP");
39 _commandHelp[_ID("help")] = LOCALE_STR("CONSOLE_HELP_COMMAND_HELP");
40 _commandHelp[_ID("editparam")] = LOCALE_STR("CONSOLE_EDITPARAM_COMMAND_HELP");
41 _commandHelp[_ID("playsound")] = LOCALE_STR("CONSOLE_PLAYSOUND_COMMAND_HELP");
42 _commandHelp[_ID("createnavmesh")] = LOCALE_STR("CONSOLE_NAVMESH_COMMAND_HELP");
43 _commandHelp[_ID("recompileshader")] = LOCALE_STR("CONSOLE_SHADER_RECOMPILE_COMMAND_HELP");
44 _commandHelp[_ID("setfov")] = LOCALE_STR("CONSOLE_CHANGE_FOV_COMMAND_HELP");
45 _commandHelp[_ID("addObject")] = LOCALE_STR("CONSOLE_ADD_OBJECT_COMMAND_HELP");
46 _commandHelp[_ID("invalidhelp")] = LOCALE_STR("CONSOLE_INVALID_HELP_ARGUMENT");
47}
48
49bool GUIConsoleCommandParser::processCommand(const string& commandString) {
50 // Be sure we have a string longer than 0
51 if (commandString.length() >= 1) {
52 // Check if the first letter is a 'command' operator
53 if (commandString.at(0) == '/') {
54 const string::size_type commandEnd = commandString.find(' ', 1);
55 string command = commandString.substr(1, commandEnd - 1);
56 string commandArgs = commandString.substr(commandEnd + 1, commandString.length() - (commandEnd + 1));
57
58 if (commandString != commandArgs) {
59 commandArgs.clear();
60 }
61 // convert command to lower case
62 for (auto& it : command) {
63 it = static_cast<char>(tolower(it));
64 }
65 if (_commands.find(_ID(command.c_str())) != std::end(_commands)) {
66 // we have a valid command
67 _commands[_ID(command.c_str())](commandArgs);
68 } else {
69 // invalid command
70 _commands[_ID("invalidcommand")](command);
71 }
72 } else {
73 // no commands, just output what was typed
74 Console::printfn({}, commandString .c_str());
75 }
76 }
77 return true;
78}
79
81 Console::printfn(LOCALE_STR("CONSOLE_SAY_NAME_TAG"), args.c_str());
82}
83
85 if (!args.empty()) {
86 // quit command can take an extra argument. A reason, for example
87 Console::printfn(LOCALE_STR("CONSOLE_QUIT_COMMAND_ARGUMENT"),
88 args.c_str());
89 }
91}
92
94 if (args.empty()) {
95 Console::printfn(LOCALE_STR("HELP_CONSOLE_COMMAND"));
96 for (const CommandMap::value_type& it : _commands) {
97 if (it.first != _ID("invalidhelp") &&
98 it.first != _ID("invalidcommand")) {
99 Console::printfn("{}", _commandHelp[it.first]);
100 }
101 }
102 } else {
103 if (_commandHelp.find(_ID(args.c_str())) != std::end(_commandHelp)) {
104 Console::printfn("{}", _commandHelp[_ID(args.c_str())]);
105 } else {
106 Console::printfn("{}", _commandHelp[_ID("invalidhelp")]);
107 }
108 }
109}
110
112 if (context().paramHandler().isParam<string>(_ID(args.c_str()))) {
113 Console::printfn(LOCALE_STR("CONSOLE_EDITPARAM_FOUND"), args.c_str(),
114 "N/A", "N/A", "N/A");
115 } else {
116 Console::printfn(LOCALE_STR("CONSOLE_EDITPARAM_NOT_FOUND"), args.c_str());
117 }
118}
119
121 const ResourcePath filename(Paths::g_assetsLocation / args);
122
123 const std::ifstream soundfile(filename.string().c_str() );
124 if (soundfile) {
125 // Check extensions (not really, musicwav.abc would still be valid, but
126 // still ...)
127 if (!hasExtension(filename, "wav") &&
128 !hasExtension(filename, "mp3") &&
129 !hasExtension(filename, "ogg")) {
130 Console::errorfn(LOCALE_STR("CONSOLE_PLAY_SOUND_INVALID_FORMAT"));
131 return;
132 }
133
134 const FileNameAndPath data = splitPathToNameAndLocation(filename);
135
136 // The file is valid, so create a descriptor for it
137 ResourceDescriptor<AudioDescriptor> sound("consoleFilePlayback");
138 sound.assetName(data._fileName);
139 sound.assetLocation(data._path);
140 _sound = CreateResource(sound);
141 if (filename.string().find("music") != string::npos) {
142 // play music
144 } else {
145 // play sound but stop music first if it's playing
148 }
149 } else {
150 Console::errorfn(LOCALE_STR("CONSOLE_PLAY_SOUND_INVALID_FILE"), filename.string());
151 }
152}
153
155 ProjectManager* sMgr = _context.kernel().projectManager().get();
156 auto& sceneGraph = sMgr->activeProject()->getActiveScene()->sceneGraph();
157 if (!args.empty()) {
158 const SceneGraphNode* sgn = sceneGraph->findNode(args.c_str());
159 if (!sgn) {
160 Console::errorfn(LOCALE_STR("CONSOLE_NAVMESH_NO_NODE"), args.c_str());
161 return;
162 }
163 }
164 auto& aiManager = sceneGraph->parentScene().aiManager();
165 // Check if we already have a NavMesh created
167 // Create a new NavMesh if we don't currently have one
168 if (temp == nullptr)
169 {
170 temp = aiManager->addNavMesh( _context, *sMgr->recast(), sceneGraph->parentScene(), AI::AIEntity::PresetAgentRadius::AGENT_RADIUS_SMALL );
171 }
172 // Set it's file name
173 temp->setFileName( sMgr->activeProject()->getActiveScene()->resourceName());
174 // Try to load it from file
175 bool loaded = temp->load(sceneGraph->getRoot());
176 if (!loaded)
177 {
178 // If we failed to load it from file, we need to build it first
179 loaded = temp->build( sceneGraph->getRoot(), AI::Navigation::NavigationMesh::CreationCallback(), false);
180 // Then save it to file
181 temp->save(sceneGraph->getRoot());
182 }
183 // If we loaded/built the NavMesh correctly, add it to the AIManager
184 if (loaded)
185 {
187 }
188}
189
192}
193
195 if (!Util::IsNumber(args)) {
196 Console::errorfn(LOCALE_STR("CONSOLE_INVALID_NUMBER"));
197 return;
198 }
199
200 const I32 FoV = CLAMPED<I32>(atoi(args.c_str()), 40, 140);
201
203}
204
206 Console::errorfn(LOCALE_STR("CONSOLE_INVALID_COMMAND"), args.c_str());
207}
208} //namespace Divide
#define LOCALE_STR(X)
Definition: Localization.h:91
bool save(const SceneGraphNode *sgn)
Save the NavigationMesh to a file.
Definition: NavMesh.cpp:784
DELEGATE< void, NavigationMesh * > CreationCallback
Definition: NavMesh.h:126
bool build(SceneGraphNode *sgn, CreationCallback creationCompleteCallback, bool threaded=true)
Definition: NavMesh.cpp:174
void setFileName(const Str< 256 > &fileName)
Definition: NavMesh.h:128
bool load(const SceneGraphNode *sgn)
Load a saved NavigationMesh from a file.
Definition: NavMesh.cpp:701
void RequestShutdown(bool clearCache) noexcept
Definition: Application.inl:45
static Camera * playerCamera(const Divide::ProjectManager *mgr, const bool skipOverride=false) noexcept
void setHorizontalFoV(Angle::DEGREES< F32 > horizontalFoV) noexcept
Definition: Camera.cpp:580
hashMap< U64, DELEGATE< void, string > > _commands
Definition: CommandParser.h:47
void handleNavMeshCommand(const string &args)
void handleHelpCommand(const string &args)
void handleEditParamCommand(const string &args)
Handle< AudioDescriptor > _sound
used for sound playback
hashMap< U64, const char * > _commandHelp
Help text for every command.
void handleShaderRecompileCommand(const string &args)
void handleQuitCommand(const string &args)
void handleInvalidCommand(const string &args)
GUIConsoleCommandParser(PlatformContext &context)
void handleSayCommand(const string &args)
void handlePlaySoundCommand(const string &args)
bool processCommand(const string &commandString) override
If we need a parser , just override this.
PlatformContext & context() noexcept
Application & app() noexcept
Kernel & kernel() noexcept
SFXDevice & sfx() noexcept
AI::Navigation::DivideRecast * recast() const noexcept
void playMusic(Handle< AudioDescriptor > music) override
Definition: SFXDevice.cpp:117
void playSound(Handle< AudioDescriptor > sound) override
Definition: SFXDevice.cpp:82
void stopMusic() override
Definition: SFXDevice.cpp:129
static bool RecompileShaderProgram(const std::string_view name)
Queue a shaderProgram recompile request.
bool IsNumber(const T_str &s)
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
bool hasExtension(const ResourcePath &filePath, const std::string_view extensionNoDot)
int32_t I32
F32 FoV(const GFXShaderData::CamData &dataIn) noexcept
constexpr U64 _ID(const char *const str, const U64 value=val_64_const) noexcept
FORCE_INLINE Handle< T > CreateResource(const ResourceDescriptor< T > &descriptor, bool &wasInCache, std::atomic_uint &taskCounter)
FileNameAndPath splitPathToNameAndLocation(const ResourcePath &input)
static NO_INLINE void errorfn(const char *format, T &&... args)
static NO_INLINE void printfn(const char *format, T &&... args)
StringReturnType< N > string() const noexcept
Definition: ResourcePath.h:64