diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/application.cc | 23 | ||||
-rw-r--r-- | src/core/application.h | 5 | ||||
-rw-r--r-- | src/core/commandbuffer.cc | 12 | ||||
-rw-r--r-- | src/core/cvar.h | 2 | ||||
-rw-r--r-- | src/core/func.cc | 54 | ||||
-rw-r--r-- | src/core/func.h | 34 | ||||
-rw-r--r-- | src/core/gameinterface.h | 6 |
7 files changed, 116 insertions, 20 deletions
diff --git a/src/core/application.cc b/src/core/application.cc index c9239cf..9fea8ee 100644 --- a/src/core/application.cc +++ b/src/core/application.cc @@ -18,10 +18,13 @@ #include "core/core.h" #include "core/entity.h" #include "core/func.h" +#include "core/cvar.h" namespace core { +Cvar sv_dedicated; + // --------------- engine functions ------------------------------ void func_print(std::stringstream &args) { @@ -83,16 +86,16 @@ extern "C" void signal_handler(int signum) case SIGQUIT: case SIGTERM: if (Application::instance()) { - con_warn << "received signal " << signum << ", shutting down..." << std::endl; + con_warn << "Received signal " << signum << ", shutting down..." << std::endl; Application::instance()->shutdown(); Application::instance()->quit(0); } else { - std::cerr << "received signal " << signum << ", terminated..." << std::endl; + std::cerr << "Received signal " << signum << ", terminated..." << std::endl; sys::quit(1); } break; default: - std::cerr << "received signal " << signum << ", terminated..." << std::endl; + std::cerr << "Received signal " << signum << ", terminated..." << std::endl; sys::quit(1); break; } @@ -133,6 +136,14 @@ void Application::init() // initialize core subsystems filesystem::init(); + + // dedicated or not + core::sv_dedicated = core::cvar::get("sv_dedicated", "1", core::cvar::ReadOnly); + if (sv_dedicated->value()) + localplayer.name = "Console"; + else + localplayer.name = "Client"; + // register our functions func::add("print", func_print); @@ -145,7 +156,7 @@ void Application::init() func::add("list_var", func_list_var); func::add("list_func", func_list_func); func::add("list_ent", func_list_ent); - + if (game()) game()->connected = false; current_time = 0; @@ -207,6 +218,10 @@ void Application::disconnect() game()->current_time = 0; entity::clear(); + + // TODO remove all game functions + + // TODO remove all game cvars con_print << "Disconnected." << std::endl; } diff --git a/src/core/application.h b/src/core/application.h index 1e2869a..d5c83d6 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -7,9 +7,14 @@ #ifndef __INCLUDED_CORE_APPLICATION_H__ #define __INCLUDED_CORE_APPLICATION_H__ +#include "core/cvar.h" + namespace core { +/// cvar to indicate dedicated server status +extern Cvar sv_dedicated; + /// core interface for the client and server Application classes class Application { diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc index c381d38..7f1cda0 100644 --- a/src/core/commandbuffer.cc +++ b/src/core/commandbuffer.cc @@ -32,7 +32,17 @@ void exec(const char *text) Func f = func::find(cmdname); if (f) { // function exists, execute it - f(cmdstream); + if (f->flags && func::Game) { + // it's a game function + if (connected()) { + GameFuncPtr function = (GameFuncPtr) f->ptr; + function(localplayer, cmdstream); + } + } else { + // it's a normal function + FuncPtr function = (FuncPtr) f->ptr; + function(cmdstream); + } return; } diff --git a/src/core/cvar.h b/src/core/cvar.h index 9b23563..16ceb46 100644 --- a/src/core/cvar.h +++ b/src/core/cvar.h @@ -43,7 +43,7 @@ namespace cvar { /// cvar flags -enum Flags {Archive=2, ReadOnly=4}; +enum Flags {Archive=1, ReadOnly=2, Game=4}; /// get a cvar value from the registry /** If the a cvar with the given name already exists in the registry, diff --git a/src/core/func.cc b/src/core/func.cc index 9f64906..af811cd 100644 --- a/src/core/func.cc +++ b/src/core/func.cc @@ -16,20 +16,57 @@ namespace func std::map<std::string, Func> registry; +void add(const char * functionname, FuncPtr functionptr, unsigned int flags) +{ + std::map<std::string, Func>::iterator it = registry.find(functionname); + Func f; + if (it == registry.end()) { + // function does not yet exist in the registry + f = new Func_t(); + registry[std::string(functionname)] = f; + } else { + f = (*it).second; + } -void add(const char * functionname, Func functionptr) + f->name = functionname; + f->ptr = (void *)functionptr; + f->flags = flags; +} + +void add(const char * functionname, GameFuncPtr gamefunctionptr, unsigned int flags) { - registry[std::string(functionname)] = functionptr; + std::map<std::string, Func>::iterator it = registry.find(functionname); + Func f; + if (it == registry.end()) { + // function does not yet exist in the registry + f = new Func_t(); + registry[std::string(functionname)] = f; + } else { + f = (*it).second; + } + + f->name = functionname; + f->ptr = (void *)gamefunctionptr; + f->flags = flags & func::Game; } void remove(const char *functionname) { - registry.erase(std::string(functionname)); + std::map<std::string, Func>::iterator it = registry.find(functionname); + if (it != registry.end()) { + delete (*it).second; + registry.erase(std::string(functionname)); + } } void remove(const std::string &functionname) { - registry.erase(functionname); + std::map<std::string, Func>::iterator it = registry.find(functionname); + if (it != registry.end()) { + delete (*it).second; + registry.erase(std::string(functionname)); + } + } Func find(const std::string &functionname) @@ -43,10 +80,17 @@ Func find(const std::string &functionname) void list() { + char typeindicator; + std::map<std::string, Func>::iterator it; for (it = registry.begin(); it != registry.end(); it++) { - con_print << " " << (*it).first << std::endl; + if ((*it).second->flags & func::Game) + typeindicator = 'G'; + else + typeindicator = ' '; + con_print << " " << typeindicator << " " << (*it).first << std::endl; } + con_print << registry.size() << " registered functions" << std::endl; } diff --git a/src/core/func.h b/src/core/func.h index ae158b5..d98e954 100644 --- a/src/core/func.h +++ b/src/core/func.h @@ -8,6 +8,7 @@ #define __INCLUDED_CORE_FUNC_H__ #include "sys/sys.h" +#include "core/player.h" #include <sstream> #include <string> @@ -16,14 +17,41 @@ namespace core { -/// function pointer type -typedef void(* Func)(std::stringstream &args); +class Func_t +{ +public: + /// pointer to the function + void *ptr; + /// name of the function + std::string name; + /// flags + unsigned int flags; +}; + +/// function type +typedef Func_t *Func; + +/// function pointer type for local functions +typedef void(* FuncPtr)(std::stringstream &args); + +/// fuction pointer for game functions +typedef void(* GameFuncPtr)(Player &player, std::stringstream &args); /// the function registry namespace func { + +/// function flags +enum Flags {Game=1}; + /// add a function to the registry -void add(const char *functionname, Func functionptr); +void add(const char *functionname, FuncPtr functionptr, unsigned int flags=0); + +/** + * @brief add a game function to the registry + * the flag core::func::Game will automaticly be set + */ +void add(const char *functionname, GameFuncPtr gamefunctionptr, unsigned int flags=0); /// remove a function from the registry void remove(const char *functionname); diff --git a/src/core/gameinterface.h b/src/core/gameinterface.h index 3e2a5f7..6e53575 100644 --- a/src/core/gameinterface.h +++ b/src/core/gameinterface.h @@ -34,12 +34,6 @@ public: */ virtual void frame(float seconds) = 0; - /// a player joins the game - virtual void event_join(Player *player) = 0; - - /// a player leaves the game - virtual void event_leave(Player *player) = 0; - /// a pointer to the current game instance static GameInterface * instance(); |