RigsofRods
Soft-body Physics Simulation
Console.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2019 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
23 
24 #pragma once
25 
26 #include "CVar.h"
27 #include "ConsoleCmd.h"
28 
29 #include <Ogre.h>
30 #include <string>
31 #include <unordered_map>
32 
33 namespace RoR {
34 
37 
40 class Console : public Ogre::LogListener
41 {
42 public:
43  typedef std::unordered_map<std::string, CVar*> CVarPtrMap;
44  typedef std::unordered_map<std::string, ConsoleCmd*> CommandPtrMap;
45 
47  {
50 
56  };
57 
59  {
65  };
66 
67  struct Message
68  {
69  Message(MessageArea area, MessageType type, std::string const& text,
70  size_t time, uint32_t net_user = 0, std::string icon = ""):
71  cm_area(area), cm_type(type), cm_text(text), cm_timestamp(time), cm_net_userid(net_user), cm_icon(icon)
72  {}
73 
76  size_t cm_timestamp;
77  uint32_t cm_net_userid;
78  std::string cm_text;
79  std::string cm_icon;
80  };
81 
82  struct MsgLockGuard
83  {
85  : lock(console.m_messages_mutex), messages(console.m_messages)
86  {}
88  : MsgLockGuard(*console)
89  {}
90 
91  std::vector<Message> & messages;
92  std::lock_guard<std::mutex> lock;
93  };
94 
95  void putMessage(MessageArea area, MessageType type, std::string const& msg, std::string icon = "");
96  void putNetMessage(int user_id, MessageType type, const char* text);
97  void forwardLogMessage(MessageArea area, std::string const& msg, Ogre::LogMessageLevel lml);
98  unsigned long queryMessageTimer() { return m_msg_timer.getMilliseconds(); }
99 
100  // ----------------------------
101  // Commands (defined in ConsoleCmd.cpp):
102 
104  void regBuiltinCommands();
105 
107  void doCommand(std::string msg);
108 
109  // ----------------------------
110  // CVars (defined in CVar.cpp):
111 
113  CVar* cVarCreate(std::string const& name, std::string const& long_name,
114  int flags, std::string const& val = std::string());
115 
117  void cVarAssign(CVar* cvar, std::string const& value);
118 
120  CVar* cVarFind(std::string const& input_name);
121 
123  CVar* cVarSet(std::string const& input_name, std::string const& input_val);
124 
126  CVar* cVarGet(std::string const& input_name, int flags);
127 
129  void cVarSetupBuiltins();
130 
131  CVarPtrMap& getCVars() { return m_cvars; }
132 
134 
135  // ----------------------------
136  // Command line (defined in AppCommandLine.cpp)
137 
138  void processCommandLine(int argc, char *argv[]);
139  void showCommandLineUsage();
140  void showCommandLineVersion();
141 
142  // ----------------------------
143  // Config file (defined in AppConfig.cpp)
144 
145  void loadConfig();
146  void saveConfig();
147 
148 private:
149  // Ogre::LogListener
150  virtual void messageLogged(
151  const Ogre::String& message, Ogre::LogMessageLevel lml,
152  bool maskDebug, const Ogre::String& logName, bool& skipThisMessage) override;
153 
154  void handleMessage(MessageArea area, MessageType type, std::string const& msg, int net_id = 0, std::string icon = "");
155 
156  std::vector<Message> m_messages;
157  std::mutex m_messages_mutex;
158  Ogre::Timer m_msg_timer;
162 };
163 
165 
166 } //namespace RoR
RoR::Console::MsgLockGuard::MsgLockGuard
MsgLockGuard(Console *console)
Definition: Console.h:87
RoR::Console::CONSOLE_MSGTYPE_LOG
@ CONSOLE_MSGTYPE_LOG
Logfile echo.
Definition: Console.h:61
RoR::Console::cVarGet
CVar * cVarGet(std::string const &input_name, int flags)
Get cvar by short/long name, or create new one using input as short name.
Definition: CVar.cpp:276
RoR::Console::getCVars
CVarPtrMap & getCVars()
Definition: Console.h:131
RoR::Console::Message::Message
Message(MessageArea area, MessageType type, std::string const &text, size_t time, uint32_t net_user=0, std::string icon="")
Definition: Console.h:69
RoR::Console::Message::cm_net_userid
uint32_t cm_net_userid
Definition: Console.h:77
RoR::Console::CONSOLE_MSGTYPE_TERRN
@ CONSOLE_MSGTYPE_TERRN
Parsing/spawn/simulation messages for terrain.
Definition: Console.h:64
RoR::Console::CVarPtrMap
std::unordered_map< std::string, CVar * > CVarPtrMap
Definition: Console.h:43
RoR::Console::Message::cm_timestamp
size_t cm_timestamp
Definition: Console.h:76
RoR::Console::cVarSetupBuiltins
void cVarSetupBuiltins()
Create builtin vars and set defaults.
Definition: CVar.cpp:29
RoR::Console::cVarFind
CVar * cVarFind(std::string const &input_name)
Find cvar by short/long name.
Definition: CVar.cpp:248
RoR::Console::putMessage
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition: Console.cpp:97
RoR::Console::cVarCreate
CVar * cVarCreate(std::string const &name, std::string const &long_name, int flags, std::string const &val=std::string())
Add CVar and parse default value if specified.
Definition: CVar.cpp:206
RoR::Console::CONSOLE_TITLE
@ CONSOLE_TITLE
Definition: Console.h:49
RoR::Console::CONSOLE_SYSTEM_NETCHAT
@ CONSOLE_SYSTEM_NETCHAT
Definition: Console.h:55
RoR::Console::m_messages
std::vector< Message > m_messages
Definition: Console.h:156
RoR::Console::CONSOLE_SYSTEM_ERROR
@ CONSOLE_SYSTEM_ERROR
Definition: Console.h:52
RoR::Console::CommandPtrMap
std::unordered_map< std::string, ConsoleCmd * > CommandPtrMap
Definition: Console.h:44
RoR::Console::Message::cm_text
std::string cm_text
Definition: Console.h:78
RoR::Console::MsgLockGuard::messages
std::vector< Message > & messages
Definition: Console.h:91
RoR::Console::CONSOLE_SYSTEM_NOTICE
@ CONSOLE_SYSTEM_NOTICE
Definition: Console.h:51
RoR::Console::Message
Definition: Console.h:67
RoR::Console::MsgLockGuard
Definition: Console.h:82
RoR::Console::cVarSet
CVar * cVarSet(std::string const &input_name, std::string const &input_val)
Set existing cvar by short/long name. Return the modified cvar (or NULL if not found)
Definition: CVar.cpp:265
RoR::Console::m_cvars
CVarPtrMap m_cvars
Definition: Console.h:159
RoR::Console::cVarAssign
void cVarAssign(CVar *cvar, std::string const &value)
Parse value by cvar type.
Definition: CVar.cpp:230
RoR::Console::forwardLogMessage
void forwardLogMessage(MessageArea area, std::string const &msg, Ogre::LogMessageLevel lml)
Definition: Console.cpp:40
RoR::Console::saveConfig
void saveConfig()
Definition: AppConfig.cpp:407
RoR::Console::MessageType
MessageType
Definition: Console.h:46
RoR::Console::queryMessageTimer
unsigned long queryMessageTimer()
Definition: Console.h:98
RoR::Console::doCommand
void doCommand(std::string msg)
Identify and execute any console line.
Definition: ConsoleCmd.cpp:678
RoR::Console::m_cvars_longname
CVarPtrMap m_cvars_longname
Definition: Console.h:160
RoR::Console::handleMessage
void handleMessage(MessageArea area, MessageType type, std::string const &msg, int net_id=0, std::string icon="")
Definition: Console.cpp:58
RoR::Console::CONSOLE_SYSTEM_REPLY
@ CONSOLE_SYSTEM_REPLY
Success.
Definition: Console.h:54
RoR::CVar
Quake-style console variable, defined in RoR.cfg or crated via Console UI and scripts.
Definition: CVar.h:52
RoR::Console::m_messages_mutex
std::mutex m_messages_mutex
Definition: Console.h:157
RoR::Console::Message::cm_area
MessageArea cm_area
Definition: Console.h:74
RoR::Console::messageLogged
virtual void messageLogged(const Ogre::String &message, Ogre::LogMessageLevel lml, bool maskDebug, const Ogre::String &logName, bool &skipThisMessage) override
Definition: Console.cpp:32
RoR::Console::getCommands
CommandPtrMap & getCommands()
Definition: Console.h:133
RoR::Console::MsgLockGuard::lock
std::lock_guard< std::mutex > lock
Definition: Console.h:92
RoR::Console::showCommandLineVersion
void showCommandLineVersion()
Definition: AppCommandLine.cpp:181
RoR::Console::loadConfig
void loadConfig()
Definition: AppConfig.cpp:342
RoR::Console::putNetMessage
void putNetMessage(int user_id, MessageType type, const char *text)
Definition: Console.cpp:102
RoR::Console::regBuiltinCommands
void regBuiltinCommands()
Register builtin commands.
Definition: ConsoleCmd.cpp:650
RoR::Console::Message::cm_icon
std::string cm_icon
Definition: Console.h:79
RoR::Console::Message::cm_type
MessageType cm_type
Definition: Console.h:75
ConsoleCmd.h
RoR::Console
Global game console backend.
Definition: Console.h:40
RoR::Console::showCommandLineUsage
void showCommandLineUsage()
Definition: AppCommandLine.cpp:162
RoR::Console::CONSOLE_MSGTYPE_ACTOR
@ CONSOLE_MSGTYPE_ACTOR
Parsing/spawn/simulation messages for actors.
Definition: Console.h:63
RoR::Console::m_msg_timer
Ogre::Timer m_msg_timer
Definition: Console.h:158
RoR::Console::CONSOLE_SYSTEM_WARNING
@ CONSOLE_SYSTEM_WARNING
Definition: Console.h:53
RoR::Console::m_commands
CommandPtrMap m_commands
Definition: Console.h:161
RoR::Console::CONSOLE_MSGTYPE_INFO
@ CONSOLE_MSGTYPE_INFO
Generic message.
Definition: Console.h:60
RoR::Console::CONSOLE_MSGTYPE_SCRIPT
@ CONSOLE_MSGTYPE_SCRIPT
Messages sent from scripts.
Definition: Console.h:62
RoR::Console::CONSOLE_HELP
@ CONSOLE_HELP
Definition: Console.h:48
CVar.h
RoR
Definition: AppContext.h:36
RoR::Console::MsgLockGuard::MsgLockGuard
MsgLockGuard(Console &console)
Definition: Console.h:84
RoR::Console::processCommandLine
void processCommandLine(int argc, char *argv[])
Definition: AppCommandLine.cpp:71
RoR::Console::MessageArea
MessageArea
Definition: Console.h:58