Added destructor code for PluginManager, console color reset funstion.

develop
Petr Mrázek 2011-06-25 08:05:17 +02:00
parent 6fd7d42f00
commit 722531f590
8 changed files with 74 additions and 30 deletions

@ -76,6 +76,7 @@ const char * ANSI_LIGHTBLUE = "\033[01;34m";
const char * ANSI_LIGHTMAGENTA = "\033[01;35m"; const char * ANSI_LIGHTMAGENTA = "\033[01;35m";
const char * ANSI_LIGHTCYAN = "\033[01;36m"; const char * ANSI_LIGHTCYAN = "\033[01;36m";
const char * ANSI_WHITE = "\033[01;37m"; const char * ANSI_WHITE = "\033[01;37m";
const char * RESETCOLOR = "\033[0m";
const char * getANSIColor(const int c) const char * getANSIColor(const int c)
{ {
@ -106,6 +107,12 @@ void Console::color(int index)
dfout << getANSIColor(index); dfout << getANSIColor(index);
} }
void Console::reset_color( void )
{
dfout << RESETCOLOR;
}
void Console::cursor(bool enable) void Console::cursor(bool enable)
{ {
if(enable) if(enable)

@ -50,6 +50,7 @@ duthomhas::stdiobuf * stream_o = 0;
HANDLE g_hConsoleOut; // Handle to debug console HANDLE g_hConsoleOut; // Handle to debug console
HWND ConsoleWindow; HWND ConsoleWindow;
WORD default_attributes;
// FIXME: prime candidate for being a singleton... indeed. // FIXME: prime candidate for being a singleton... indeed.
Console::Console() Console::Console()
@ -68,6 +69,7 @@ Console::Console()
// set the screen buffer to be big enough to let us scroll text // set the screen buffer to be big enough to let us scroll text
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
default_attributes = coninfo.wAttributes;
coninfo.dwSize.Y = MAX_CONSOLE_LINES; // How many lines do you want to have in the console buffer coninfo.dwSize.Y = MAX_CONSOLE_LINES; // How many lines do you want to have in the console buffer
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
@ -119,6 +121,13 @@ void Console::color(int index)
SetConsoleTextAttribute(hConsole, index); SetConsoleTextAttribute(hConsole, index);
} }
void Console::reset_color( void )
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, default_attributes);
}
void Console::cursor(bool enable) void Console::cursor(bool enable)
{ {
if(enable) if(enable)

@ -66,6 +66,11 @@ int fIOthread(void * iodata)
{ {
Core * core = ((IODATA*) iodata)->core; Core * core = ((IODATA*) iodata)->core;
PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr; PluginManager * plug_mgr = ((IODATA*) iodata)->plug_mgr;
if(plug_mgr == 0)
{
dfout << "Something horrible happened to the plugin manager in Core's constructor..." << std::endl;
return 0;
}
fprintf(dfout_C,"DFHack is ready. Have a nice day! Type in '?' or 'help' for help.\n"); fprintf(dfout_C,"DFHack is ready. Have a nice day! Type in '?' or 'help' for help.\n");
//dfterm << << endl; //dfterm << << endl;
int clueless_counter = 0; int clueless_counter = 0;
@ -156,10 +161,11 @@ Core::Core()
plug_mgr = new PluginManager(this); plug_mgr = new PluginManager(this);
// look for all plugins, // look for all plugins,
// create IO thread // create IO thread
IODATA temp; IODATA *temp = new IODATA;
temp.core = this; temp->core = this;
temp.plug_mgr = plug_mgr; temp->plug_mgr = plug_mgr;
DFThread * IO = SDL_CreateThread(fIOthread, (void *) &temp); DFThread * IO = SDL_CreateThread(fIOthread, (void *) temp);
delete temp;
// and let DF do its thing. // and let DF do its thing.
}; };

@ -119,8 +119,11 @@ Plugin::Plugin(Core * core, const std::string & file)
Plugin::~Plugin() Plugin::~Plugin()
{ {
if(loaded) if(loaded)
{
plugin_shutdown(&Core::getInstance());
ClosePlugin(plugin_lib); ClosePlugin(plugin_lib);
} }
}
bool Plugin::isLoaded() bool Plugin::isLoaded()
{ {
@ -143,9 +146,10 @@ PluginManager::PluginManager(Core * core)
if(hasEnding(filez[i],searchstr)) if(hasEnding(filez[i],searchstr))
{ {
Plugin * p = new Plugin(core, path + filez[i]); Plugin * p = new Plugin(core, path + filez[i]);
for(int j = 0; j < p->commands.size();j++) Plugin & pr = *p;
for(int j = 0; j < pr.size();j++)
{ {
commands[p->commands[j].name] = &p->commands[j]; commands[p->commands[j].name] = &pr[j];
} }
all_plugins.push_back(p); all_plugins.push_back(p);
} }
@ -154,28 +158,32 @@ PluginManager::PluginManager(Core * core)
PluginManager::~PluginManager() PluginManager::~PluginManager()
{ {
commands.clear();
for(int i = 0; i < all_plugins.size();i++)
{
delete all_plugins[i];
}
all_plugins.clear();
} }
Plugin *PluginManager::getPluginByName (const std::string & name) Plugin *PluginManager::getPluginByName (const std::string & name)
{ {
for(int i = 0; i < all_plugins.size(); i++)
{
if(name == all_plugins[i]->name)
return all_plugins[i];
}
return 0;
} }
// FIXME: handle name collisions...
command_result PluginManager::InvokeCommand( std::string & command, std::vector <std::string> & parameters) command_result PluginManager::InvokeCommand( std::string & command, std::vector <std::string> & parameters)
{ {
Core * c = &Core::getInstance(); Core * c = &Core::getInstance();
map <string, PluginCommand *>::iterator iter = commands.find(command); map <string, const PluginCommand *>::iterator iter = commands.find(command);
if(iter != commands.end()) if(iter != commands.end())
{ {
return iter->second->function(c,parameters); return iter->second->function(c,parameters);
} }
return CR_NOT_IMPLEMENTED; return CR_NOT_IMPLEMENTED;
} }
/*
for (map <string, int (*)(Core *)>::iterator iter = plugins.begin(); iter != plugins.end(); iter++)
{
dfout << iter->first << endl;
}
*/
/*
*/

@ -44,6 +44,8 @@ namespace DFHack
void gotoxy(int x, int y); void gotoxy(int x, int y);
/// Set color (ANSI color number) /// Set color (ANSI color number)
void color(int index); void color(int index);
/// Reset color to default
void reset_color(void);
/// Enable or disable the caret/cursor /// Enable or disable the caret/cursor
void cursor(bool enable = true); void cursor(bool enable = true);
/// Waits given number of milliseconds before continuing. /// Waits given number of milliseconds before continuing.

@ -67,11 +67,14 @@ namespace DFHack
Plugin(DFHack::Core* core, const std::string& file); Plugin(DFHack::Core* core, const std::string& file);
~Plugin(); ~Plugin();
bool isLoaded (); bool isLoaded ();
/* const PluginCommand& operator[] (std::size_t index)
bool Load (); {
bool Unload (); return commands[index];
std::string Status (); };
*/ std::size_t size()
{
return commands.size();
}
private: private:
std::vector <PluginCommand> commands; std::vector <PluginCommand> commands;
std::string filename; std::string filename;
@ -89,8 +92,19 @@ namespace DFHack
~PluginManager(); ~PluginManager();
Plugin *getPluginByName (const std::string & name); Plugin *getPluginByName (const std::string & name);
command_result InvokeCommand( std::string & command, std::vector <std::string> & parameters); command_result InvokeCommand( std::string & command, std::vector <std::string> & parameters);
//FIXME: how do we deal with errors inside DF? Unhandled exceptions are deadly.
const Plugin* operator[] (std::size_t index)
{
if(index >= all_plugins.size())
return 0;
return all_plugins[index];
};
std::size_t size()
{
return all_plugins.size();
}
private: private:
std::map <std::string, PluginCommand *> commands; std::map <std::string, const PluginCommand *> commands;
std::vector <Plugin *> all_plugins; std::vector <Plugin *> all_plugins;
std::string plugin_path; std::string plugin_path;
}; };

@ -61,6 +61,8 @@ DFhackCExport command_result kittens (Core * c, vector <string> & parameters)
if(shutdown_flag) if(shutdown_flag)
{ {
final_flag = true; final_flag = true;
c->con->reset_color();
dfout << std::endl << "MEOW!" << std::endl << std::flush;
return CR_OK; return CR_OK;
} }
con->color(color); con->color(color);

@ -27,7 +27,6 @@ bool revealed = false;
DFhackCExport command_result reveal(DFHack::Core * c, std::vector<std::string> & params); DFhackCExport command_result reveal(DFHack::Core * c, std::vector<std::string> & params);
DFhackCExport command_result unreveal(DFHack::Core * c, std::vector<std::string> & params); DFhackCExport command_result unreveal(DFHack::Core * c, std::vector<std::string> & params);
DFhackCExport command_result revealtoggle(DFHack::Core * c, std::vector<std::string> & params); DFhackCExport command_result revealtoggle(DFHack::Core * c, std::vector<std::string> & params);
//DFhackCExport command_result revealclear(DFHack::Core * c, std::vector<std::string> & params);
DFhackCExport const char * plugin_name ( void ) DFhackCExport const char * plugin_name ( void )
{ {
@ -40,7 +39,6 @@ DFhackCExport command_result plugin_init ( Core * c, std::vector <PluginCommand>
commands.push_back(PluginCommand("reveal","Reveal the map.",reveal)); commands.push_back(PluginCommand("reveal","Reveal the map.",reveal));
commands.push_back(PluginCommand("unreveal","Revert the map to its previous state.",unreveal)); commands.push_back(PluginCommand("unreveal","Revert the map to its previous state.",unreveal));
commands.push_back(PluginCommand("revealtoggle","Reveal/unreveal depending on state.",revealtoggle)); commands.push_back(PluginCommand("revealtoggle","Reveal/unreveal depending on state.",revealtoggle));
//commands.push_back(PluginCommand("revealclear","Reset the reveal tool.",revealclear));
return CR_OK; return CR_OK;
} }
@ -68,7 +66,6 @@ DFhackCExport command_result reveal(DFHack::Core * c, std::vector<std::string> &
return CR_FAILURE; return CR_FAILURE;
} }
dfout << "Revealing, please wait..." << std::endl;
Maps->getSize(x_max,y_max,z_max); Maps->getSize(x_max,y_max,z_max);
hidesaved.reserve(x_max * y_max * z_max); hidesaved.reserve(x_max * y_max * z_max);
for(uint32_t x = 0; x< x_max;x++) for(uint32_t x = 0; x< x_max;x++)
@ -103,8 +100,7 @@ DFhackCExport command_result reveal(DFHack::Core * c, std::vector<std::string> &
c->Resume(); c->Resume();
dfout << "Map revealed. The game has been paused for you." << std::endl; dfout << "Map revealed. The game has been paused for you." << std::endl;
dfout << "Unpausing can unleash the forces of hell!" << std::endl; dfout << "Unpausing can unleash the forces of hell!" << std::endl;
dfout << "Saving will make this state permanent. Don't do it." << std::endl << std::endl; dfout << "Run 'unreveal' to revert to previous state." << std::endl;
dfout << "Run 'reveal' again to revert to previous state." << std::endl;
return CR_OK; return CR_OK;
} }
@ -139,7 +135,6 @@ DFhackCExport command_result unreveal(DFHack::Core * c, std::vector<std::string>
// FIXME: add more sanity checks / MAP ID // FIXME: add more sanity checks / MAP ID
dfout << "Unrevealing... please wait." << std::endl;
for(size_t i = 0; i < hidesaved.size();i++) for(size_t i = 0; i < hidesaved.size();i++)
{ {
hideblock & hb = hidesaved[i]; hideblock & hb = hidesaved[i];
@ -153,6 +148,7 @@ DFhackCExport command_result unreveal(DFHack::Core * c, std::vector<std::string>
// give back memory. // give back memory.
hidesaved.clear(); hidesaved.clear();
revealed = false; revealed = false;
dfout << "Map hidden!" << std::endl;
c->Resume(); c->Resume();
return CR_OK; return CR_OK;
} }