From 9b041ac2e075bfb3a30397a8acdc5432860d2c84 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 22 Feb 2008 21:53:36 +0000 Subject: save and load configuration implemented --- INSTALL | 4 ++-- osirion.kdevelop.pcs | Bin 520260 -> 456490 bytes osirion.kdevses | 23 +++++++++++++++------- src/client/client.cc | 4 ++-- src/core/application.cc | 49 +++++++++++++++++++++++++++++++++++++++++++++- src/core/application.h | 6 ++++++ src/core/commandbuffer.cc | 38 ++++++++++++++++++++++++++++------- src/core/func.cc | 6 +++--- 8 files changed, 108 insertions(+), 22 deletions(-) diff --git a/INSTALL b/INSTALL index 8137359..25a08be 100644 --- a/INSTALL +++ b/INSTALL @@ -32,7 +32,7 @@ Compiling the source code At present, 'make install' is neither tested nor supported. -Game Data +Installing game data The game data should be located in the 'data' subdirectory of the main distribution. @@ -66,7 +66,7 @@ Updating Rebuild the game: - autoreconf + autoreconf -fi ./configure make diff --git a/osirion.kdevelop.pcs b/osirion.kdevelop.pcs index a21a08c..d218712 100644 Binary files a/osirion.kdevelop.pcs and b/osirion.kdevelop.pcs differ diff --git a/osirion.kdevses b/osirion.kdevses index 52484c5..98b982a 100644 --- a/osirion.kdevses +++ b/osirion.kdevses @@ -1,16 +1,25 @@ - - - + + + - - + + - - + + + + + + + + + + + diff --git a/src/client/client.cc b/src/client/client.cc index 4874e55..f232c12 100644 --- a/src/client/client.cc +++ b/src/client/client.cc @@ -88,8 +88,8 @@ void Client::init() core::Application::init(); // client variables - core::Cvar::get("cl_name", "Player"); - core::Cvar::get("cl_color", "1.0 1.0 1.0"); + core::Cvar::get("cl_name", "Player", core::Cvar::Archive); + core::Cvar::get("cl_color", "1.0 1.0 1.0", core::Cvar::Archive); // initialize SDL, but do not initialize any subsystems SDL_Init(0); diff --git a/src/core/application.cc b/src/core/application.cc index b55df34..7d6f62a 100644 --- a/src/core/application.cc +++ b/src/core/application.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include "sys/sys.h" #include "math/mathlib.h" @@ -152,11 +153,15 @@ void Application::init() filesystem::init(); CommandBuffer::init(); - // dedicated server has set this to 1 Cvar::sv_dedicated = Cvar::get("sv_dedicated", "0", Cvar::ReadOnly); // client can set this to 1 Cvar::sv_private = Cvar::get("sv_private", "0"); + + // load save cvars + load_config(); + + // framerate settings Cvar::sv_framerate = Cvar::get("sv_framerate", "25"); // network settings @@ -186,6 +191,8 @@ void Application::shutdown() application_game = 0; } + save_config(); + // remove our engine functions Func::remove("print"); Func::remove("help"); @@ -272,4 +279,44 @@ void Application::frame(float seconds) disconnect(); } +void Application::save_config() +{ + std::string filename(filesystem::writedir); + filename.append("config.txt"); + std::ofstream ofs(filename.c_str()); + + if (!ofs.is_open()) { + con_warn << "Could not write " << filename << std::endl; + return; + } + + std::map::iterator it; + for (it = Cvar::registry.begin(); it != Cvar::registry.end(); it++) { + + if (((*it).second->flags() & Cvar::Archive) == Cvar::Archive) + ofs << "set " << (*it).first << " " << (*it).second->str() << std::endl; + } + ofs.close(); +} + +void Application::load_config() +{ + std::string filename(filesystem::writedir); + filename.append("config.txt"); + std::ifstream ifs(filename.c_str(), std::ifstream::in); + + if (!ifs.is_open()) { + con_warn << "Could not read " << filename << std::endl; + return; + } + + char line[MAXCMDSIZE]; + while (ifs.getline(line, MAXCMDSIZE-1)) { + cmd() << line << "\n"; + + } + + // execute commands in the buffer + CommandBuffer::exec(); +} } diff --git a/src/core/application.h b/src/core/application.h index de0ab35..f0f03b5 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -64,6 +64,12 @@ protected: /// run a core frame virtual void frame(float seconds); + /// load cvar config + void load_config(); + + /// save cvar config + void save_config(); + private: /// time the core has been running float application_time; diff --git a/src/core/commandbuffer.cc b/src/core/commandbuffer.cc index 831489f..2cf8d26 100644 --- a/src/core/commandbuffer.cc +++ b/src/core/commandbuffer.cc @@ -18,36 +18,60 @@ namespace core { -void func_list_func(std::istringstream const &args) +void func_list_func(std::string const &args) { Func::list(); } -void func_list_var(std::istringstream const &args) +void func_list_var(std::string const &args) { Cvar::list(); } -void func_list_ent(std::istringstream const &args) +void func_list_ent(std::string const &args) { Entity::list(); } +void func_set(std::string const &args) +{ + std::istringstream argstream(args); + std::string varname; + if (!(argstream >> varname)) + return; + + // we're setting a new value + std::string value; + if (!(argstream >> value)) + return; + + char c; + while (argstream.get(c)) + value += c; + + Cvar *cvar = Cvar::set(varname.c_str(), value.c_str(), Cvar::Archive); + + con_print << cvar->name() << " " << cvar->str() << "\n"; + return; +} + std::stringstream CommandBuffer::cmdbuf(std::stringstream::in | std::stringstream::out); void CommandBuffer::init() { - con_debug << "Initializing command buffer...\n"; + //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); + Func::add("list_func", (FuncPtr)func_list_func); + Func::add("list_var", (FuncPtr)func_list_var); + Func::add("set", (FuncPtr)func_set); } void CommandBuffer::shutdown() { - con_debug << "Shutting down command buffer...\n"; + //con_debug << "Shutting down command buffer...\n"; + Func::remove("set"); Func::remove("list_var"); Func::remove("list_func"); Func::remove("list_ent"); diff --git a/src/core/func.cc b/src/core/func.cc index afe4787..4db8dcf 100644 --- a/src/core/func.cc +++ b/src/core/func.cc @@ -33,7 +33,7 @@ void Func::add(const char *name, GameFuncPtr gamefunctionptr, unsigned int flags std::map::iterator it = registry.find(name); if (it == registry.end()) { registry[std::string(name)] = new Func(name, (void *)gamefunctionptr, flags | Func::Game); - con_debug << "Function '" << name << "' registered." << std::endl; + //con_debug << "Function '" << name << "' registered." << std::endl; } else { con_warn << "Function '" << name << "' already registered!" << std::endl; } @@ -45,7 +45,7 @@ void Func::remove(const char *name) if (it != registry.end()) { delete (*it).second; registry.erase(it); - con_debug << "Function '" << name << "' unregistered." << std::endl; + //con_debug << "Function '" << name << "' unregistered." << std::endl; } } @@ -55,7 +55,7 @@ void Func::remove(const std::string &name) if (it != registry.end()) { delete (*it).second; registry.erase(it); - con_debug << "Function '" << name << "' unregistered." << std::endl; + //con_debug << "Function '" << name << "' unregistered." << std::endl; } } -- cgit v1.2.3