mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-05-17 09:51:21 +00:00
Add ability to flag commands
This commit is contained in:
parent
031c139e2d
commit
eae0dbd2bb
@ -4,6 +4,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "CVar.hpp"
|
#include "CVar.hpp"
|
||||||
|
#include "hecl/SystemChar.hpp"
|
||||||
|
|
||||||
namespace hecl
|
namespace hecl
|
||||||
{
|
{
|
||||||
@ -78,6 +79,8 @@ public:
|
|||||||
|
|
||||||
void setDeveloperMode(bool v, bool setDeserialized=false);
|
void setDeveloperMode(bool v, bool setDeserialized=false);
|
||||||
bool restartRequired() const;
|
bool restartRequired() const;
|
||||||
|
|
||||||
|
void parseCommandLine(const std::vector<SystemString>& args);
|
||||||
private:
|
private:
|
||||||
bool suppressDeveloper();
|
bool suppressDeveloper();
|
||||||
void restoreDeveloper(bool oldDeveloper);
|
void restoreDeveloper(bool oldDeveloper);
|
||||||
|
@ -14,11 +14,19 @@ class CVarManager;
|
|||||||
class CVar;
|
class CVar;
|
||||||
struct SConsoleCommand
|
struct SConsoleCommand
|
||||||
{
|
{
|
||||||
|
enum class ECommandFlags
|
||||||
|
{
|
||||||
|
Normal = 0,
|
||||||
|
Cheat = (1 << 0),
|
||||||
|
Developer = (1 << 1)
|
||||||
|
};
|
||||||
std::string m_displayName;
|
std::string m_displayName;
|
||||||
std::string m_helpString;
|
std::string m_helpString;
|
||||||
std::string m_usage;
|
std::string m_usage;
|
||||||
std::function<void(class Console*, const std::vector<std::string>&)> m_func;
|
std::function<void(class Console*, const std::vector<std::string>&)> m_func;
|
||||||
|
ECommandFlags m_flags;
|
||||||
};
|
};
|
||||||
|
ENABLE_BITWISE_ENUM(SConsoleCommand::ECommandFlags)
|
||||||
|
|
||||||
class Console
|
class Console
|
||||||
{
|
{
|
||||||
@ -77,9 +85,11 @@ private:
|
|||||||
CVar* m_conHeight;
|
CVar* m_conHeight;
|
||||||
float m_cachedConSpeed;
|
float m_cachedConSpeed;
|
||||||
float m_cachedConHeight;
|
float m_cachedConHeight;
|
||||||
|
bool m_showCursor = true;
|
||||||
|
float m_cursorTime = 0.f;
|
||||||
public:
|
public:
|
||||||
Console(CVarManager*);
|
Console(CVarManager*);
|
||||||
void registerCommand(std::string_view name, std::string_view helpText, std::string_view usage, const std::function<void(Console*, const std::vector<std::string>&)>&& func);
|
void registerCommand(std::string_view name, std::string_view helpText, std::string_view usage, const std::function<void(Console*, const std::vector<std::string>&)>&& func, SConsoleCommand::ECommandFlags cmdFlags = SConsoleCommand::ECommandFlags::Normal);
|
||||||
void unregisterCommand(std::string_view name);
|
void unregisterCommand(std::string_view name);
|
||||||
|
|
||||||
void executeString(const std::string& strToExec);
|
void executeString(const std::string& strToExec);
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
#include <athena/FileWriter.hpp>
|
#include <athena/FileWriter.hpp>
|
||||||
#include <athena/Utility.hpp>
|
#include <athena/Utility.hpp>
|
||||||
#include <hecl/Runtime.hpp>
|
#include <hecl/Runtime.hpp>
|
||||||
|
#include <hecl/hecl.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
namespace hecl
|
namespace hecl
|
||||||
{
|
{
|
||||||
@ -12,6 +14,7 @@ CVar* com_developer = nullptr;
|
|||||||
CVar* com_configfile = nullptr;
|
CVar* com_configfile = nullptr;
|
||||||
CVar* com_enableCheats = nullptr;
|
CVar* com_enableCheats = nullptr;
|
||||||
|
|
||||||
|
static const std::regex cmdLineRegex("\\+(\\w+)=([\\w\\.\\-]+)");
|
||||||
CVarManager* CVarManager::m_instance = nullptr;
|
CVarManager* CVarManager::m_instance = nullptr;
|
||||||
|
|
||||||
static logvisor::Module CVarLog("CVarManager");
|
static logvisor::Module CVarLog("CVarManager");
|
||||||
@ -269,11 +272,42 @@ bool CVarManager::restartRequired() const
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CVarManager::parseCommandLine(const std::vector<SystemString>& args)
|
||||||
|
{
|
||||||
|
bool oldDeveloper = suppressDeveloper();
|
||||||
|
std::string developerName = com_developer->name().data();
|
||||||
|
ToLower(developerName);
|
||||||
|
for (const SystemString& arg : args)
|
||||||
|
{
|
||||||
|
if (arg[0] == _S('+'))
|
||||||
|
{
|
||||||
|
std::string tmp = SystemUTF8Conv(arg).c_str();
|
||||||
|
|
||||||
|
std::smatch matches;
|
||||||
|
if (std::regex_match(tmp, matches, cmdLineRegex))
|
||||||
|
{
|
||||||
|
std::string cvarName = matches[1].str();
|
||||||
|
std::string cvarValue = matches[2].str();
|
||||||
|
if (CVar* cv = findCVar(cvarName))
|
||||||
|
{
|
||||||
|
cv->fromLiteralToType(cvarValue);
|
||||||
|
hecl::ToLower(cvarName);
|
||||||
|
if (developerName == cvarName)
|
||||||
|
/* Make sure we're not overriding developer mode when we restore */
|
||||||
|
oldDeveloper = com_developer->toBoolean();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
restoreDeveloper(oldDeveloper);
|
||||||
|
}
|
||||||
|
|
||||||
bool CVarManager::suppressDeveloper()
|
bool CVarManager::suppressDeveloper()
|
||||||
{
|
{
|
||||||
bool oldDeveloper = com_developer->toBoolean();
|
bool oldDeveloper = com_developer->toBoolean();
|
||||||
CVarUnlocker unlock(com_developer);
|
CVarUnlocker unlock(com_developer);
|
||||||
com_developer->fromBoolean(false);
|
com_developer->fromBoolean(true);
|
||||||
|
|
||||||
return oldDeveloper;
|
return oldDeveloper;
|
||||||
}
|
}
|
||||||
|
@ -24,12 +24,12 @@ Console::Console(CVarManager* cvarMgr)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Console::registerCommand(std::string_view name, std::string_view helpText, std::string_view usage, const std::function<void(Console*, const std::vector<std::string> &)>&& func)
|
void Console::registerCommand(std::string_view name, std::string_view helpText, std::string_view usage, const std::function<void(Console*, const std::vector<std::string> &)>&& func, SConsoleCommand::ECommandFlags cmdFlags)
|
||||||
{
|
{
|
||||||
std::string lowName = name.data();
|
std::string lowName = name.data();
|
||||||
athena::utility::tolower(lowName);
|
athena::utility::tolower(lowName);
|
||||||
if (m_commands.find(lowName) == m_commands.end())
|
if (m_commands.find(lowName) == m_commands.end())
|
||||||
m_commands[lowName] = SConsoleCommand{name.data(), helpText.data(), usage.data(), std::move(func)};
|
m_commands[lowName] = SConsoleCommand{name.data(), helpText.data(), usage.data(), std::move(func), cmdFlags};
|
||||||
}
|
}
|
||||||
|
|
||||||
void Console::unregisterCommand(std::string_view name)
|
void Console::unregisterCommand(std::string_view name)
|
||||||
@ -65,7 +65,21 @@ void Console::executeString(const std::string& str)
|
|||||||
std::string lowComName = commandName;
|
std::string lowComName = commandName;
|
||||||
athena::utility::tolower(lowComName);
|
athena::utility::tolower(lowComName);
|
||||||
if (m_commands.find(lowComName) != m_commands.end())
|
if (m_commands.find(lowComName) != m_commands.end())
|
||||||
|
{
|
||||||
|
const SConsoleCommand& cmd = m_commands[lowComName];
|
||||||
|
if (bool(cmd.m_flags & SConsoleCommand::ECommandFlags::Developer) && !com_developer->toBoolean())
|
||||||
|
{
|
||||||
|
report(Level::Error, "This command can only be executed in developer mode", commandName.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bool(cmd.m_flags & SConsoleCommand::ECommandFlags::Cheat) && !com_enableCheats->toBoolean())
|
||||||
|
{
|
||||||
|
report(Level::Error, "This command can only be executed with cheats enabled", commandName.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_commands[lowComName].m_func(this, args);
|
m_commands[lowComName].m_func(this, args);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
report(Level::Error, "Command '%s' is not valid!", commandName.c_str());
|
report(Level::Error, "Command '%s' is not valid!", commandName.c_str());
|
||||||
}
|
}
|
||||||
@ -266,8 +280,8 @@ void Console::handleSpecialKeyDown(boo::ESpecialKey sp, boo::EModifierKey mod, b
|
|||||||
m_cursorPosition = -1;
|
m_cursorPosition = -1;
|
||||||
m_commandHistory.insert(m_commandHistory.begin(), m_commandString);
|
m_commandHistory.insert(m_commandHistory.begin(), m_commandString);
|
||||||
m_commandString.clear();
|
m_commandString.clear();
|
||||||
//m_showCursor = true;
|
m_showCursor = true;
|
||||||
//m_cursorTime = 0;
|
m_cursorTime = 0.f;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case boo::ESpecialKey::Left:
|
case boo::ESpecialKey::Left:
|
||||||
@ -280,8 +294,8 @@ void Console::handleSpecialKeyDown(boo::ESpecialKey sp, boo::EModifierKey mod, b
|
|||||||
else
|
else
|
||||||
m_cursorPosition--;
|
m_cursorPosition--;
|
||||||
|
|
||||||
//m_showCursor = true;
|
m_showCursor = true;
|
||||||
//m_cursorTime = 0;
|
m_cursorTime = 0.f;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case boo::ESpecialKey::Right:
|
case boo::ESpecialKey::Right:
|
||||||
@ -303,8 +317,8 @@ void Console::handleSpecialKeyDown(boo::ESpecialKey sp, boo::EModifierKey mod, b
|
|||||||
else
|
else
|
||||||
m_cursorPosition++;
|
m_cursorPosition++;
|
||||||
|
|
||||||
// m_showCursor = true;
|
m_showCursor = true;
|
||||||
// m_cursorTime = 0;
|
m_cursorTime = 0.f;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user