Hello!
While working on my mouse-driven project, I was attempting to make a plugin to do the very simple task of outputting the current screen, like it does if you run 'keybinding' without any arguments. instead of parsing all the text, and extracting the last line, I wanted to make it only export the current screen.
I am running into a problem, and in this case, I know what it is: I don't know C++.
Here is the code:
// This is a generic plugin that does nothing useful apart from acting as an example... of a plugin that does nothing :D
// some headers required for a plugin. Nothing special, just the basics.
#include "Core.h"
#include <Console.h>
#include <Export.h>
#include <PluginManager.h>
// DF data structure definition headers
#include "DataDefs.h"
//#include "df/world.h"
#include "modules/Gui.h"
using std::vector;
using std::string;
using namespace DFHack;
using namespace df::enums;
// our own, empty header.
//#include "skeleton.h"
// Here go all the command declarations...
// mostly to allow having the mandatory stuff on top of the file and commands on the bottom
command_result mousestatus (color_ostream &out, std::vector <std::string> & parameters);
// A plugin must be able to return its name and version.
// The name string provided must correspond to the filename - skeleton.plug.so or skeleton.plug.dll in this case
DFHACK_PLUGIN("mousestatus");
// Mandatory init function. If you have some global state, create it here.
DFhackCExport command_result plugin_init ( color_ostream &out, std::vector <PluginCommand> &commands)
{
// Fill the command list with your commands.
commands.push_back(PluginCommand(
"mousestatus", "Return the current game screen.",
mousestatus, false, /* true means that the command can't be used from non-interactive user interface */
// Extended help string. Used by CR_WRONG_USAGE and the help command:
" This command returns the game screen DF is currently on.\n"
"Example:\n"
" mousestatus\n"
" mainmenu\n"
));
return CR_OK;
}
// This is called right before the plugin library is removed from memory.
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
{
// You *MUST* kill all threads you created before this returns.
// If everything fails, just return CR_FAILURE. Your plugin will be
// in a zombie state, but things won't crash.
return CR_OK;
}
// Called to notify the plugin about important state changes.
// Invoked with DF suspended, and always before the matching plugin_onupdate.
// More event codes may be added in the future.
/*
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
{
switch (event) {
case SC_GAME_LOADED:
// initialize from the world just loaded
break;
case SC_GAME_UNLOADED:
// cleanup
break;
default:
break;
}
return CR_OK;
}
*/
// Whatever you put here will be done in each game step. Don't abuse it.
// It's optional, so you can just comment it out like this if you don't need it.
/*
DFhackCExport command_result plugin_onupdate ( color_ostream &out )
{
// whetever. You don't need to suspend DF execution here.
return CR_OK;
}
*/
// A command! It sits around and looks pretty. And it's nice and friendly.
command_result mousestatus (color_ostream &out, std::vector <std::string> & parameters)
{
// It's nice to print a help message you get invalid options
// from the user instead of just acting strange.
// This can be achieved by adding the extended help string to the
// PluginCommand registration as show above, and then returning
// CR_WRONG_USAGE from the function. The same string will also
// be used by 'help your-command'.
out.print(Gui::getFocusString(Core::getTopViewscreen()));
// Commands are called from threads other than the DF one.
// Suspend this thread until DF has time for us. If you
// use CoreSuspender, it'll automatically resume DF when
// execution leaves the current scope.
CoreSuspender suspend;
// Actually do something here. Yay.
//out.print("Hello! I do nothing, remember?\n");
// Give control back to DF.
return CR_OK;
}
and here is the error:
1>------ Build started: Project: ZERO_CHECK, Configuration: Release Win32 ------
2>------ Build started: Project: generate_headers, Configuration: Release Win32 ------
3>------ Build started: Project: generate_proto, Configuration: Release Win32 ------
4>------ Build started: Project: mousestatus, Configuration: Release Win32 ------
4> mousestatus.cpp
4>..\..\..\plugins\mousestatus.cpp(96): error C2664: 'DFHack::color_ostream::print' : cannot convert parameter 1 from 'std::string' to 'const char *'
4> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
========== Build: 3 succeeded, 1 failed, 9 up-to-date, 0 skipped ==========
Now, from the little I remember from my computer science classes, It has to do with the function call
command_result mousestatus () being wrong. I know its probably a simple fix, but I don't know where to go from here. I think its not declaring what it needs properly, but I'm not sure what it should be.
I edited the skeleton plugin, and then copied the line from Core.cpp that listed the GUI's current screen. I changed it to an out.print command instead of con <<, because that is what was being used earlier in the skeleton plugin to display text. I included another library at the top too, but I don't think I need it, and it most likely should be removed.
Long term, I'm looking to make a plugin-assisted AutoHotKey menu that allows the player to right click, and get a set commands based on what they are doing. In the case of designations, I would like to allow box drawing like Falconne's construction additions to automaterial. Perhaps some sort of 'macro' or run history of commands as well, which would allow the user to save a list of commonly used commands, and execute them at will, instead of just mapping them to keys. In case you are wondering, I am only using AutoHotKey because I know it already, and its easy to use. After I get something working, I would like to eventually port it to something a little more universal, but that is a long way out.
If somebody could point me in the right direction, or better yet, reply with fixed code, I would be very thankful. Either way, thanks for looking!