From 982562fa19bb87a3dab352e562f386f61c171b7b Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 17 Feb 2008 18:59:52 +0000 Subject: major rewrite of Cvar, Func and Entity --- src/core/commandbuffer.cc | 129 +++++++++++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 48 deletions(-) (limited to 'src/core/commandbuffer.cc') diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc index a15708c..8f02f71 100644 --- a/src/core/commandbuffer.cc +++ b/src/core/commandbuffer.cc @@ -4,88 +4,123 @@ the terms of the GNU General Public License version 2 */ -#include "core/commandbuffer.h" -#include "sys/sys.h" - -// C++ headers #include #include #include +#include "sys/sys.h" +#include "core/commandbuffer.h" +#include "core/func.h" +#include "core/cvar.h" +#include "core/gameinterface.h" + namespace core { -std::stringstream cmd(std::stringstream::in | std::stringstream::out); +void func_list_func(std::istringstream const &args) +{ + Func::list(); +} + +void func_list_var(std::istringstream const &args) +{ + Cvar::list(); +} + +void func_list_ent(std::istringstream const &args) +{ + Entity::list(); +} + +std::stringstream CommandBuffer::cmdbuf(std::stringstream::in | std::stringstream::out); + +void CommandBuffer::init() +{ + con_debug << "Initializing command buffer...\n"; + + Func::add("list_var", (FuncPtr)func_list_var); + Func::add("list_func", (FuncPtr)func_list_func); + Func::add("list_ent", (FuncPtr)func_list_ent); +} -namespace commandbuffer +void CommandBuffer::shutdown() { + con_debug << "Shutting down command buffer...\n"; -void exec(const char *text) + Func::remove("list_var"); + Func::remove("list_func"); + Func::remove("list_ent"); +} + +void CommandBuffer::exec(std::string const &cmdline) { - std::stringstream cmdstream(text); + if (!cmdline.size()) + return; + + std::istringstream cmdstream(cmdline); std::string command; if (!(cmdstream >> command)) return; + con_debug << "Executing '" << cmdline << "'\n"; + // is it a function - Func f = func::find(command); + Func *f = Func::find(command); if (f) { - // function exists, execute it - if (f->flags() && func::Game) { - // it's a game function + std::string args; + char c; + if (cmdstream >> args) { + while (cmdstream >> c) + args += c; + } + if ((f->flags() & Func::Game)) { if (game() && game()->connected) { - GameFuncPtr function = (GameFuncPtr) f->ptr; - function(localplayer, cmdstream); + f->exec(&Player::local, args); } } else { - // it's a normal function - FuncPtr function = (FuncPtr) f->ptr; - function(cmdstream); + f->exec(args); } return; } // is it a cvar - Cvar cv = cvar::find(command); - if (cv) { + Cvar *cvar = Cvar::find(command); + if (cvar) { // cvar exists - std::string args; - if (((cv->flags() & cvar::ReadOnly) == 0) && (cmdstream >> args)) { + std::string value; + if (((cvar->flags() & Cvar::ReadOnly) == 0) && (cmdstream >> value)) { // we're setting a new value char c; while (cmdstream >> c) - args += c; - (*cv) = args; + value += c; + (*cvar) = value; } - con_print << command << " " << cv->text() << std::endl; + con_print << command << " " << cvar->str() << "\n"; return; } - con_print << "Unknown command '" << command << "'" << std::endl; + // TODO this must get forwarded to the server + + con_print << "Unknown command '" << command << "'\n"; } -void execute() +void CommandBuffer::exec() { - if (core::cmd.eof()) + if (cmdbuf.eof()) return; char line[MAXCMDSIZE]; - while (core::cmd.getline(line, MAXCMDSIZE-1)) { - exec(line); + + while (core::cmd().getline(line, MAXCMDSIZE-1)) { + exec(std::string(line)); } - cmd.clear(); + cmdbuf.clear(); } -void clear() -{ - char line[MAXCMDSIZE]; - while (core::cmd.getline(line, MAXCMDSIZE-1)); -} - -void complete(std::string &input, size_t &pos) +void CommandBuffer::complete(std::string &input, size_t &pos) { std::list match; @@ -94,20 +129,20 @@ void complete(std::string &input, size_t &pos) return; // search function registry for matches - std::map::iterator f; - for (f = func::registry.begin(); f != func::registry.end(); f++) { + std::map::iterator f; + for (f = Func::registry.begin(); f != Func::registry.end(); f++) { if (partial == (*f).first.substr(0, partial.size())) { match.push_back((*f).first); - //con_print << " " << (*f).first << std::endl; + //con_print << " " << (*f).first << "\n"; } } // search cvar registry for matches - std::map::iterator c; - for (c = cvar::registry.begin(); c != cvar::registry.end(); c++) { + std::map::iterator c; + for (c = Cvar::registry.begin(); c != Cvar::registry.end(); c++) { if (partial == (*c).first.substr(0, partial.size())) { match.push_back((*c).first); - //con_print << " " << (*c).first << std::endl; + //con_print << " " << (*c).first << "\n"; } } @@ -127,9 +162,9 @@ void complete(std::string &input, size_t &pos) if (i < maxmatch.size()) maxmatch.erase(i); } - con_print << " " << (*l) << std::endl; + con_print << " " << (*l) << "\n"; } - con_print << match.size() << " matches" << std::endl; + con_print << match.size() << " matches\n"; } @@ -141,7 +176,5 @@ void complete(std::string &input, size_t &pos) } -} // namespace commandbuffer - -} // namespace core +} -- cgit v1.2.3