Add console commands for accessing config variables

This commit is contained in:
yvt 2019-07-14 17:43:04 +09:00
parent f4f9dce529
commit 0ade4b6356
No known key found for this signature in database
GPG Key ID: 48F2768FA8D07C92
4 changed files with 99 additions and 1 deletions

View File

@ -302,6 +302,7 @@
E8C7D7AF22DA34F500F78763 /* ConsoleScreen.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8C7D7AC22DA34F400F78763 /* ConsoleScreen.cpp */; };
E8C7D7B122DA35F000F78763 /* ConsoleHelper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8C7D7B022DA35F000F78763 /* ConsoleHelper.cpp */; };
E8C7D7B422DAF5B500F78763 /* ConsoleCommand.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8C7D7B322DAF5B400F78763 /* ConsoleCommand.cpp */; };
E8C7D7BA22DB220300F78763 /* ConfigConsoleResponder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8C7D7B822DB220200F78763 /* ConfigConsoleResponder.cpp */; };
E8CB47CE1DE071CA00BF606A /* SWUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CB47CC1DE071CA00BF606A /* SWUtils.cpp */; };
E8CB47D21DE07CF000BF606A /* SettingSet.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CB47CF1DE07CF000BF606A /* SettingSet.cpp */; };
E8CB47D61DE084AB00BF606A /* GLSettings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CB47D41DE084AB00BF606A /* GLSettings.cpp */; };
@ -768,6 +769,8 @@
E8C7D7B022DA35F000F78763 /* ConsoleHelper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ConsoleHelper.cpp; sourceTree = "<group>"; };
E8C7D7B222DAF5B400F78763 /* ConsoleCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConsoleCommand.h; sourceTree = "<group>"; };
E8C7D7B322DAF5B400F78763 /* ConsoleCommand.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ConsoleCommand.cpp; sourceTree = "<group>"; };
E8C7D7B822DB220200F78763 /* ConfigConsoleResponder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ConfigConsoleResponder.cpp; sourceTree = "<group>"; };
E8C7D7B922DB220300F78763 /* ConfigConsoleResponder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConfigConsoleResponder.h; sourceTree = "<group>"; };
E8C92A0A18695EA500740C9F /* SWModelRenderer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = SWModelRenderer.cpp; sourceTree = "<group>"; };
E8C92A0B18695EA500740C9F /* SWModelRenderer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SWModelRenderer.h; sourceTree = "<group>"; };
E8C92A0D186A8D3600740C9F /* CpuID.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CpuID.h; sourceTree = "<group>"; };
@ -1507,6 +1510,8 @@
E8CF039D178EDDBF000683D4 /* Gui */ = {
isa = PBXGroup;
children = (
E8C7D7B822DB220200F78763 /* ConfigConsoleResponder.cpp */,
E8C7D7B922DB220300F78763 /* ConfigConsoleResponder.h */,
E8C7D7B322DAF5B400F78763 /* ConsoleCommand.cpp */,
E8C7D7B222DAF5B400F78763 /* ConsoleCommand.h */,
E8C7D7AB22DA34F400F78763 /* ConsoleHelper.cpp */,
@ -2315,6 +2320,7 @@
E88344101E03244F000C3E39 /* FontManager.cpp in Sources */,
E893A3631E3E3331000654A4 /* PackageUpdateManager.cpp in Sources */,
E82E672318EA7954004DBA18 /* Runner.cpp in Sources */,
E8C7D7BA22DB220300F78763 /* ConfigConsoleResponder.cpp in Sources */,
E82E672418EA7954004DBA18 /* StartupScreen.cpp in Sources */,
E82E66ED18EA7914004DBA18 /* StartupScreenHelper.cpp in Sources */,
);

View File

@ -0,0 +1,58 @@
/*
Copyright (c) 2019 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ConfigConsoleResponder.h"
#include <Core/Debug.h>
#include <Core/Settings.h>
#include <Core/Strings.h>
#include "ConsoleCommand.h"
namespace spades {
namespace gui {
namespace {
std::string EscapeQuotes(std::string s) { return Replace(s, "\"", "\\\""); }
} // namespace
bool ConfigConsoleResponder::ExecCommand(const Handle<ConsoleCommand> &cmd) {
SPADES_MARK_FUNCTION();
Settings::ItemHandle cvarHandle{cmd->GetName(), nullptr};
if (cvarHandle.IsUnknown()) {
return false;
}
if (cmd->GetNumArguments() == 0) {
// Display the current value
SPLog("%s = \"%s\"", cmd->GetName().c_str(), EscapeQuotes(cvarHandle).c_str());
} else if (cmd->GetNumArguments() == 1) {
// Set a new value
const std::string &newValue = cmd->GetArgument(0);
SPLog("%s = \"%s\" (previously \"%s\")", cmd->GetName().c_str(),
EscapeQuotes(newValue).c_str(), EscapeQuotes(cvarHandle).c_str());
cvarHandle = newValue;
} else {
SPLog("Invalid number of arguments (Maybe you meant something "
"like 'varname \"value1 value2\"'?)");
}
return true;
}
} // namespace gui
} // namespace spades

View File

@ -0,0 +1,33 @@
/*
Copyright (c) 2019 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Core/RefCountedObject.h>
namespace spades {
namespace gui {
class ConsoleCommand;
/** Responds to console commands for accessing config variables. */
class ConfigConsoleResponder {
public:
static bool ExecCommand(const Handle<ConsoleCommand> &);
};
} // namespace gui
} // namespace spades

View File

@ -20,6 +20,7 @@
#include <ScriptBindings/Config.h>
#include <ScriptBindings/ScriptFunction.h>
#include "ConfigConsoleResponder.h"
#include "ConsoleHelper.h"
#include "ConsoleScreen.h"
@ -215,7 +216,7 @@ namespace spades {
bool ConsoleScreen::ExecCommand(const Handle<ConsoleCommand> &command) {
SPADES_MARK_FUNCTION();
return subview->ExecCommand(command);
return ConfigConsoleResponder::ExecCommand(command) || subview->ExecCommand(command);
}
bool ConsoleScreen::ShouldInterceptInput() {