Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/application.cc33
-rw-r--r--src/core/commandbuffer.cc18
-rw-r--r--src/core/core.h1
-rw-r--r--src/core/cvar.cc103
-rw-r--r--src/core/cvar.h62
-rw-r--r--src/core/func.cc19
-rw-r--r--src/core/func.h13
7 files changed, 177 insertions, 72 deletions
diff --git a/src/core/application.cc b/src/core/application.cc
index 810bacb..a5d2937 100644
--- a/src/core/application.cc
+++ b/src/core/application.cc
@@ -19,9 +19,12 @@ namespace core
extern "C" void func_print(std::stringstream &args)
{
char text[MAXCMDSIZE];
- // FIXME leading space
- if (args.getline(text, MAXCMDSIZE))
- con_print << text << std::endl;
+ if (args.getline(text, MAXCMDSIZE)) {
+ // remove the leading space
+ if (text[0] && text[1]) {
+ con_print << text+1 << std::endl;
+ }
+ }
}
extern "C" void func_help(std::stringstream &args)
@@ -49,6 +52,16 @@ extern "C" void func_disconnect(std::stringstream &args)
Application::instance()->disconnect();
}
+extern "C" void func_listfunc(std::stringstream &args)
+{
+ func::list();
+}
+
+extern "C" void func_listcvar(std::stringstream &args)
+{
+ cvar::list();
+}
+
// --------------- signal_handler -----------------------------------
extern "C" void signal_handler(int signum)
@@ -118,10 +131,11 @@ void Application::init()
func::add("connect", func_connect);
func::add("disconnect", func_disconnect);
+ func::add("listcvar", func_listcvar);
+ func::add("listfunc", func_listfunc);
+
if (game())
game()->connected = false;
- else
- con_warn << "No game module loaded!" << std::endl;
current_time = 0;
}
@@ -129,11 +143,10 @@ void Application::shutdown()
{
con_print << "Shutting down core..." << std::endl;
- if (game())
- if (game()->connected)
- disconnect();
- else
- con_warn << "No game module loaded!" << std::endl;
+ if (game() && game()->connected)
+ disconnect();
+
+ //if (game()) unload();
filesystem::shutdown();
}
diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc
index 7ba1e08..67e90d9 100644
--- a/src/core/commandbuffer.cc
+++ b/src/core/commandbuffer.cc
@@ -25,13 +25,29 @@ void exec(const char *text)
cmdstream >> cmdname;
+ // is it a function
Func f = func::find(cmdname);
-
if (f) {
+ // function exists, execute it
f(cmdstream);
return;
}
+ // is it a cvar
+ Cvar cv = cvar::find(cmdname);
+ if (cv) {
+ // cvar exists
+ std::string args;
+ if (cmdstream >> args) {
+ // we're setting a new value
+ char c;
+ while(cmdstream >> c)
+ args += c;
+ (*cv) = args;
+ }
+ con_print << cmdname << cv->text() << std::endl;
+ }
+
con_print << "unknown command '" << cmdname << "'" << std::endl;
}
diff --git a/src/core/core.h b/src/core/core.h
index 3802cca..688d4c9 100644
--- a/src/core/core.h
+++ b/src/core/core.h
@@ -27,6 +27,7 @@ namespace core
};
#include "core/commandbuffer.h"
+#include "core/cvar.h"
#include "core/func.h"
#endif // __INCLUDED_CORE_H__
diff --git a/src/core/cvar.cc b/src/core/cvar.cc
index 21105b4..f49438c 100644
--- a/src/core/cvar.cc
+++ b/src/core/cvar.cc
@@ -21,96 +21,131 @@ Cvar_t::Cvar_t(unsigned int cvarflags)
cvar_flags = cvarflags;
}
-Cvar_t & Cvar_t::operator=(const std::string &cvarvalue)
+Cvar_t & Cvar_t::operator=(const std::string &other)
{
- cvar_value = cvarvalue;
+ cvar_text = other;
+ std::stringstream s(cvar_text);
+ if (!(s >> cvar_value))
+ cvar_value = cvar_text.size();
return (*this);
}
-Cvar_t & Cvar_t::operator=(const char *cvarvalue)
+Cvar_t & Cvar_t::operator=(const char *other)
{
- cvar_value.assign(cvarvalue);
+ return ((*this) = std::string(other));
+}
+
+Cvar_t & Cvar_t::operator=(int other)
+{
+ std::stringstream s;
+ s << other;
+ s >> cvar_text;
+ cvar_value = (float) other;
return (*this);
}
-Cvar_t & Cvar_t::operator=(int cvarvalue)
+Cvar_t & Cvar_t::operator=(float other)
{
std::stringstream s;
- s << cvarvalue;
- s >> cvar_value;
+ s << other;
+ s >> cvar_text;
+ cvar_value = other;
return (*this);
}
-namespace cvar
+namespace cvar
{
-std::map<std::string, Cvar> registry;
+std::map<std::string, Cvar> cvarregistry;
-Cvar set(const char *cvarname, const char *cvarvalue, int cvarflags)
+Cvar get(const char *name, const char *value, int flags)
{
- Cvar c = registry[std::string(cvarname)];
+ Cvar c = cvarregistry[std::string(name)];
if (c) {
- con_debug << "cvar::set " << cvarname << " already exist with value " << cvarvalue << std::endl;
+ con_debug << "cvar::get " << name << " already exist with value " << value << std::endl;
} else {
- con_debug << "cvar::set " << cvarname << " with value " << cvarvalue << std::endl;
- c = new Cvar_t(cvarflags);
- registry[std::string(cvarname)] = c;
- (*c) = cvarvalue;
+ con_debug << "cvar::get " << name << " " << value << std::endl;
+ c = new Cvar_t(flags);
+ cvarregistry[std::string(name)] = c;
+ (*c) = value;
}
return c;
}
-Cvar set(const char *cvarname, int cvarvalue, int cvarflags)
+Cvar get(const char *name, int value, int flags)
{
- Cvar c = registry[std::string(cvarname)];
+ Cvar c = cvarregistry[std::string(name)];
if (c) {
- con_debug << "cvar::set " << cvarname << " already exist with value " << cvarvalue << std::endl;
+ con_debug << "cvar::get " << name << " already exist with value " << value << std::endl;
} else {
- con_debug << "cvar::set " << cvarname << " with value " << cvarvalue << std::endl;
- c = new Cvar_t(cvarflags);
- registry[std::string(cvarname)] = c;
- (*c) = cvarvalue;
+ con_debug << "cvar::get " << name << " " << value << std::endl;
+ c = new Cvar_t(flags);
+ cvarregistry[std::string(name)] = c;
+ (*c) = value;
+ }
+ return c;
+}
+
+Cvar set(const char *name, const char *value, int flags)
+{
+ Cvar c = cvarregistry[std::string(name)];
+ if (!c) {
+ c = new Cvar_t(flags);
+ cvarregistry[std::string(name)] = c;
+ }
+ con_debug << "cvar::set " << name << " " << value << std::endl;
+ (*c) = value;
+ return c;
+}
+
+Cvar set(const char *name, int value, int flags)
+{
+ Cvar c = cvarregistry[std::string(name)];
+ if (!c) {
+ c = new Cvar_t(flags);
+ cvarregistry[std::string(name)] = c;
}
+ con_debug << "cvar::set " << name << " " << value << std::endl;
+ (*c) = value;
return c;
}
void unset(const char *cvarname)
{
- Cvar c = registry[std::string(cvarname)];
+ Cvar c = cvarregistry[std::string(cvarname)];
if (c) {
con_debug << "cvar::unset " << cvarname << std::endl;
- registry.erase(std::string(cvarname));
+ cvarregistry.erase(std::string(cvarname));
delete c;
}
}
void unset(const std::string &cvarname)
{
- Cvar c = registry[cvarname];
+ Cvar c = cvarregistry[cvarname];
if (c) {
con_debug << "cvar::unset " << cvarname << std::endl;
- registry.erase(cvarname);
+ cvarregistry.erase(cvarname);
delete c;
}
}
Cvar find(const std::string &cvarname)
{
- return registry[cvarname];
+ return cvarregistry[cvarname];
}
Cvar find(const char *cvarname)
{
- return registry[std::string(cvarname)];
+ return cvarregistry[std::string(cvarname)];
}
void list()
{
- std::map<std::string, Cvar>::iterator registryiterator;
- for (registryiterator = registry.begin(); registryiterator != registry.end(); registryiterator++) {
- con_print << std::setw(4) << (*registryiterator).second->flags()
- << " " << (*registryiterator).first
- << " " << (*registryiterator).second->value() << std::endl;
+ con_print << "-- listcvar -----------------" << std::endl;
+ std::map<std::string, Cvar>::iterator cvarregistryiterator;
+ for (cvarregistryiterator = cvarregistry.begin(); cvarregistryiterator != cvarregistry.end(); cvarregistryiterator++) {
+ con_print << " "<< (*cvarregistryiterator).first << " " << (*cvarregistryiterator).second->text() << std::endl;
}
}
diff --git a/src/core/cvar.h b/src/core/cvar.h
index 8ad0fa1..fa04c49 100644
--- a/src/core/cvar.h
+++ b/src/core/cvar.h
@@ -11,40 +11,61 @@
namespace core {
-/// Don't use - need a decent cvar
+/// the cvar container class
class Cvar_t {
public:
- Cvar_t(unsigned int cvarflags = 0);
+ Cvar_t(unsigned int cvflags = 0);
- inline const std::string &value() const {return cvar_value; }
- inline unsigned int flags() const { return cvar_flags; }
-
- Cvar_t &operator=(const char *cvarvalue);
- Cvar_t &operator=(const std::string &cvarvalue);
- Cvar_t &operator=(int cvarvalue);
+ Cvar_t &operator=(const char *other);
+ Cvar_t &operator=(const std::string &other);
+ Cvar_t &operator=(int other);
+ Cvar_t &operator=(float other);
+ inline unsigned int flags() const { return cvar_flags; }
+ inline float value() const { return(cvar_value); }
+ const std::string &text() const { return(cvar_text); }
+
private:
- std::string cvar_value;
- unsigned int cvar_flags;
+ std::string cvar_text;
+ float cvar_value;
+ unsigned int cvar_flags;
};
/// general cvar type
typedef Cvar_t *Cvar;
-/// Don't use - need a decent cvar
+/// the cvar registry
namespace cvar
{
-/// create a new cvar containing a string value
-Cvar set(const char *cvarname, const char *cvarvalue, int cvarflags=0);
-
-/// create a new cvar containing an integer value
-Cvar set(const char *cvarname, int cvarvalue, int cvarflags=0);
-
-/// delete a cvar
+/// get a cvar value from the registry
+/** If the a cvar with the given name already exists in the registry,
+ * its value will not be changed. If the cvar does not exist,
+ * it will be created
+ */
+Cvar get(const char *name, const char *value, int flags=0);
+/// get a cvar value from the registry
+/** If the a cvar with the given name already exists in the registry,
+ * its value will not be changed. If the cvar does not exist,
+ * it will be created
+ */
+Cvar get(const char *name, int value, int flags=0);
+
+/// set a cvar value
+/** If the a cvar with the given name already exists in the registry,
+ * its value will be replaced
+ */
+Cvar set(const char *name, const char *value, int flags=0);
+/// set a cvar value
+/** If the a cvar with the given name already exists in the registry,
+ * its value will be replaced
+ */
+Cvar set(const char *name, int value, int flags=0);
+
+/// delete a cvar from the registry
void unset(const char *cvarname);
-/// delete a cvar
+/// delete a cvar from the registry
void unset(const std::string &cvarname);
/// search for a named cvar, returns 0 if not found
@@ -53,6 +74,9 @@ Cvar find(const std::string &cvarname);
/// search for a named cvar, returns 0 if not found
Cvar find(const char *cvarname);
+/// list the cvar registry
+void list();
+
} // namespace cvar
} // namespace core
diff --git a/src/core/func.cc b/src/core/func.cc
index bcc6f50..c1ed826 100644
--- a/src/core/func.cc
+++ b/src/core/func.cc
@@ -14,26 +14,35 @@ namespace core
namespace func
{
-std::map<std::string, Func> registry;
+std::map<std::string, Func> funcregistry;
void add(const char * functionname, Func functionptr)
{
- registry[std::string(functionname)] = functionptr;
+ funcregistry[std::string(functionname)] = functionptr;
}
void remove(const char *functionname)
{
- registry.erase(std::string(functionname));
+ funcregistry.erase(std::string(functionname));
}
void remove(const std::string &functionname)
{
- registry.erase(functionname);
+ funcregistry.erase(functionname);
}
Func find(const std::string &functionname)
{
- return registry[functionname];
+ return funcregistry[functionname];
+}
+
+void list()
+{
+ con_print << "-- listfunc -----------------" << std::endl;
+ std::map<std::string, Func>::iterator funcregistryiterator;
+ for (funcregistryiterator = funcregistry.begin(); funcregistryiterator != funcregistry.end(); funcregistryiterator++) {
+ con_print << " " << (*funcregistryiterator).first << std::endl;
+ }
}
} // namespace func
diff --git a/src/core/func.h b/src/core/func.h
index 1af69c0..ad189b8 100644
--- a/src/core/func.h
+++ b/src/core/func.h
@@ -1,5 +1,5 @@
/*
- core/funct.h
+ core/func.h
This file is part of the Osirion project and is distributed under
the terms of the GNU General Public License version 2
*/
@@ -7,6 +7,8 @@
#ifndef __INCLUDED_CORE_FUNC_H__
#define __INCLUDED_CORE_FUNC_H__
+#include "sys/sys.h"
+
#include <sstream>
namespace core
@@ -28,8 +30,13 @@ void remove(const std::string &functionname);
/** Returns 0 if the function pointer could not be found
*/
Func find(const std::string &functionname);
-}
-}
+/// list the function registry
+void list();
+
+} // namespace func
+
+} // namespace core
#endif // __INCLUDED_CORE_FUNC_H__
+