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_LIGHTCYAN = "\033[01;36m";
const char * ANSI_WHITE = "\033[01;37m";
const char * RESETCOLOR = "\033[0m";
const char * getANSIColor(const int c)
{
@ -106,6 +107,12 @@ void Console::color(int index)
dfout << getANSIColor(index);
}
void Console::reset_color( void )
{
dfout << RESETCOLOR;
}
void Console::cursor(bool enable)
{
if(enable)

@ -50,6 +50,7 @@ duthomhas::stdiobuf * stream_o = 0;
HANDLE g_hConsoleOut; // Handle to debug console
HWND ConsoleWindow;
WORD default_attributes;
// FIXME: prime candidate for being a singleton... indeed.
Console::Console()
@ -68,6 +69,7 @@ Console::Console()
// set the screen buffer to be big enough to let us scroll text
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
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize);
@ -119,6 +121,13 @@ void Console::color(int index)
SetConsoleTextAttribute(hConsole, index);
}
void Console::reset_color( void )
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, default_attributes);
}
void Console::cursor(bool enable)
{
if(enable)

@ -66,6 +66,11 @@ int fIOthread(void * iodata)
{
Core * core = ((IODATA*) iodata)->core;
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");
//dfterm << << endl;
int clueless_counter = 0;
@ -156,10 +161,11 @@ Core::Core()
plug_mgr = new PluginManager(this);
// look for all plugins,
// create IO thread
IODATA temp;
temp.core = this;
temp.plug_mgr = plug_mgr;
DFThread * IO = SDL_CreateThread(fIOthread, (void *) &temp);
IODATA *temp = new IODATA;
temp->core = this;
temp->plug_mgr = plug_mgr;
DFThread * IO = SDL_CreateThread(fIOthread, (void *) temp);
delete temp;
// and let DF do its thing.
};

@ -119,8 +119,11 @@ Plugin::Plugin(Core * core, const std::string & file)
Plugin::~Plugin()
{
if(loaded)
{
plugin_shutdown(&Core::getInstance());
ClosePlugin(plugin_lib);
}
}
bool Plugin::isLoaded()
{
@ -143,9 +146,10 @@ PluginManager::PluginManager(Core * core)
if(hasEnding(filez[i],searchstr))
{
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);
}
@ -154,28 +158,32 @@ PluginManager::PluginManager(Core * core)
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)
{
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)
{
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())
{
return iter->second->function(c,parameters);
}
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);
/// Set color (ANSI color number)
void color(int index);
/// Reset color to default
void reset_color(void);
/// Enable or disable the caret/cursor
void cursor(bool enable = true);
/// Waits given number of milliseconds before continuing.

@ -67,11 +67,14 @@ namespace DFHack
Plugin(DFHack::Core* core, const std::string& file);
~Plugin();
bool isLoaded ();
/*
bool Load ();
bool Unload ();
std::string Status ();
*/
const PluginCommand& operator[] (std::size_t index)
{
return commands[index];
};
std::size_t size()
{
return commands.size();
}
private:
std::vector <PluginCommand> commands;
std::string filename;
@ -89,8 +92,19 @@ namespace DFHack
~PluginManager();
Plugin *getPluginByName (const std::string & name);
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:
std::map <std::string, PluginCommand *> commands;
std::map <std::string, const PluginCommand *> commands;
std::vector <Plugin *> all_plugins;
std::string plugin_path;
};

@ -61,6 +61,8 @@ DFhackCExport command_result kittens (Core * c, vector <string> & parameters)
if(shutdown_flag)
{
final_flag = true;
c->con->reset_color();
dfout << std::endl << "MEOW!" << std::endl << std::flush;
return CR_OK;
}
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 unreveal(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 )
{
@ -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("unreveal","Revert the map to its previous state.",unreveal));
commands.push_back(PluginCommand("revealtoggle","Reveal/unreveal depending on state.",revealtoggle));
//commands.push_back(PluginCommand("revealclear","Reset the reveal tool.",revealclear));
return CR_OK;
}
@ -68,7 +66,6 @@ DFhackCExport command_result reveal(DFHack::Core * c, std::vector<std::string> &
return CR_FAILURE;
}
dfout << "Revealing, please wait..." << std::endl;
Maps->getSize(x_max,y_max,z_max);
hidesaved.reserve(x_max * y_max * z_max);
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();
dfout << "Map revealed. The game has been paused for you." << 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 'reveal' again to revert to previous state." << std::endl;
dfout << "Run 'unreveal' to revert to previous state." << std::endl;
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
dfout << "Unrevealing... please wait." << std::endl;
for(size_t i = 0; i < hidesaved.size();i++)
{
hideblock & hb = hidesaved[i];
@ -153,6 +148,7 @@ DFhackCExport command_result unreveal(DFHack::Core * c, std::vector<std::string>
// give back memory.
hidesaved.clear();
revealed = false;
dfout << "Map hidden!" << std::endl;
c->Resume();
return CR_OK;
}