2011-12-31 04:48:42 -07:00
|
|
|
#include "Core.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "Console.h"
|
|
|
|
#include "Export.h"
|
|
|
|
#include "PluginManager.h"
|
2011-12-31 04:48:42 -07:00
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
#include <Error.h>
|
|
|
|
#include <LuaTools.h>
|
|
|
|
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "modules/Gui.h"
|
2012-04-07 09:08:30 -06:00
|
|
|
#include "modules/Translation.h"
|
|
|
|
#include "modules/Units.h"
|
2012-09-05 11:27:42 -06:00
|
|
|
#include "modules/World.h"
|
2012-09-20 01:48:53 -06:00
|
|
|
#include "modules/Screen.h"
|
2012-01-14 08:31:43 -07:00
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
#include <VTableInterpose.h>
|
2023-01-05 18:11:01 -07:00
|
|
|
#include "df/plotinfost.h"
|
|
|
|
#include "df/gamest.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "df/world.h"
|
|
|
|
#include "df/squad.h"
|
|
|
|
#include "df/unit.h"
|
|
|
|
#include "df/unit_soul.h"
|
2012-02-22 06:44:36 -07:00
|
|
|
#include "df/historical_entity.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "df/historical_figure.h"
|
2012-02-20 07:04:58 -07:00
|
|
|
#include "df/historical_figure_info.h"
|
2014-07-29 15:30:22 -06:00
|
|
|
#include "df/identity.h"
|
2012-01-15 13:54:14 -07:00
|
|
|
#include "df/language_name.h"
|
2012-09-05 11:27:42 -06:00
|
|
|
#include "df/building_stockpilest.h"
|
|
|
|
#include "df/building_workshopst.h"
|
|
|
|
#include "df/building_furnacest.h"
|
|
|
|
#include "df/building_trapst.h"
|
|
|
|
#include "df/building_siegeenginest.h"
|
2012-09-20 01:48:53 -06:00
|
|
|
#include "df/building_civzonest.h"
|
|
|
|
#include "df/viewscreen_dwarfmodest.h"
|
2011-12-29 06:37:07 -07:00
|
|
|
|
2012-03-15 03:01:23 -06:00
|
|
|
#include "RemoteServer.h"
|
|
|
|
#include "rename.pb.h"
|
|
|
|
|
2012-04-05 08:10:16 -06:00
|
|
|
#include "MiscUtils.h"
|
|
|
|
|
2011-12-29 06:37:07 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
|
|
|
using std::endl;
|
|
|
|
using namespace DFHack;
|
|
|
|
using namespace df::enums;
|
2012-03-15 03:01:23 -06:00
|
|
|
using namespace dfproto;
|
2011-12-29 06:37:07 -07:00
|
|
|
|
2014-12-06 16:47:35 -07:00
|
|
|
DFHACK_PLUGIN("rename");
|
|
|
|
DFHACK_PLUGIN_IS_ENABLED(is_enabled);
|
|
|
|
|
2023-01-05 18:11:01 -07:00
|
|
|
REQUIRE_GLOBAL(plotinfo);
|
|
|
|
REQUIRE_GLOBAL(game);
|
2014-12-06 16:47:35 -07:00
|
|
|
REQUIRE_GLOBAL(world);
|
2011-12-29 06:37:07 -07:00
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event);
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static command_result rename(color_ostream &out, vector <string> & parameters);
|
2011-12-29 06:37:07 -07:00
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_init (color_ostream &out, std::vector <PluginCommand> &commands)
|
2011-12-29 06:37:07 -07:00
|
|
|
{
|
2023-01-05 18:11:01 -07:00
|
|
|
if (world && plotinfo) {
|
2012-01-14 08:31:43 -07:00
|
|
|
commands.push_back(PluginCommand(
|
2022-07-29 17:08:00 -06:00
|
|
|
"rename",
|
|
|
|
"Easily rename things.",
|
|
|
|
rename));
|
2012-09-05 11:27:42 -06:00
|
|
|
|
2012-09-20 01:48:53 -06:00
|
|
|
if (Core::getInstance().isWorldLoaded())
|
|
|
|
plugin_onstatechange(out, SC_WORLD_LOADED);
|
2011-12-29 06:37:07 -07:00
|
|
|
}
|
2012-09-05 11:27:42 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_buildings(bool enable);
|
|
|
|
|
|
|
|
DFhackCExport command_result plugin_onstatechange(color_ostream &out, state_change_event event)
|
|
|
|
{
|
|
|
|
switch (event) {
|
2012-09-20 01:48:53 -06:00
|
|
|
case SC_WORLD_LOADED:
|
2012-09-05 11:27:42 -06:00
|
|
|
init_buildings(true);
|
|
|
|
break;
|
2012-09-20 01:48:53 -06:00
|
|
|
case SC_WORLD_UNLOADED:
|
2012-09-05 11:27:42 -06:00
|
|
|
init_buildings(false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-12-29 06:37:07 -07:00
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
DFhackCExport command_result plugin_shutdown ( color_ostream &out )
|
2011-12-29 06:37:07 -07:00
|
|
|
{
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
/*
|
|
|
|
* Building renaming - it needs some per-type hacking.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define KNOWN_BUILDINGS \
|
|
|
|
BUILDING('p', building_stockpilest, "Stockpile") \
|
|
|
|
BUILDING('w', building_workshopst, NULL) \
|
|
|
|
BUILDING('e', building_furnacest, NULL) \
|
|
|
|
BUILDING('T', building_trapst, NULL) \
|
2012-09-20 01:48:53 -06:00
|
|
|
BUILDING('i', building_siegeenginest, NULL) \
|
|
|
|
BUILDING('Z', building_civzonest, "Zone")
|
2012-09-05 11:27:42 -06:00
|
|
|
|
|
|
|
#define BUILDING(code, cname, tag) \
|
|
|
|
struct cname##_hook : df::cname { \
|
|
|
|
typedef df::cname interpose_base; \
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, getName, (std::string *buf)) { \
|
|
|
|
if (!name.empty()) {\
|
|
|
|
buf->clear(); \
|
|
|
|
*buf += name; \
|
|
|
|
*buf += " ("; \
|
|
|
|
if (tag) *buf += (const char*)tag; \
|
|
|
|
else { std::string tmp; INTERPOSE_NEXT(getName)(&tmp); *buf += tmp; } \
|
|
|
|
*buf += ")"; \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
INTERPOSE_NEXT(getName)(buf); \
|
|
|
|
} \
|
|
|
|
}; \
|
2012-09-22 03:14:06 -06:00
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE_PRIO(cname##_hook, getName, 100);
|
2012-09-05 11:27:42 -06:00
|
|
|
KNOWN_BUILDINGS
|
|
|
|
#undef BUILDING
|
|
|
|
|
2012-09-20 01:48:53 -06:00
|
|
|
struct dwarf_render_zone_hook : df::viewscreen_dwarfmodest {
|
|
|
|
typedef df::viewscreen_dwarfmodest interpose_base;
|
|
|
|
|
|
|
|
DEFINE_VMETHOD_INTERPOSE(void, render, ())
|
|
|
|
{
|
|
|
|
INTERPOSE_NEXT(render)();
|
|
|
|
|
2023-01-05 18:11:01 -07:00
|
|
|
if (plotinfo->main.mode == ui_sidebar_mode::Zones &&
|
|
|
|
game && game->zone.selected &&
|
|
|
|
!game->zone.selected->name.empty())
|
2012-09-20 01:48:53 -06:00
|
|
|
{
|
|
|
|
auto dims = Gui::getDwarfmodeViewDims();
|
|
|
|
int width = dims.menu_x2 - dims.menu_x1 - 1;
|
|
|
|
|
|
|
|
Screen::Pen pen(' ',COLOR_WHITE);
|
|
|
|
Screen::fillRect(pen, dims.menu_x1, dims.y1+1, dims.menu_x2, dims.y1+1);
|
|
|
|
|
|
|
|
std::string name;
|
2023-01-05 18:11:01 -07:00
|
|
|
game->zone.selected->getName(&name);
|
2012-09-20 01:48:53 -06:00
|
|
|
Screen::paintString(pen, dims.menu_x1+1, dims.y1+1, name.substr(0, width));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IMPLEMENT_VMETHOD_INTERPOSE(dwarf_render_zone_hook, render);
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
static char getBuildingCode(df::building *bld)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(bld);
|
|
|
|
|
|
|
|
#define BUILDING(code, cname, tag) \
|
|
|
|
if (strict_virtual_cast<df::cname>(bld)) return code;
|
|
|
|
KNOWN_BUILDINGS
|
|
|
|
#undef BUILDING
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool enable_building_rename(char code, bool enable)
|
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
if (enable)
|
|
|
|
is_enabled = true;
|
|
|
|
|
2012-09-20 01:48:53 -06:00
|
|
|
if (code == 'Z')
|
|
|
|
INTERPOSE_HOOK(dwarf_render_zone_hook, render).apply(enable);
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
switch (code) {
|
|
|
|
#define BUILDING(code, cname, tag) \
|
|
|
|
case code: return INTERPOSE_HOOK(cname##_hook, getName).apply(enable);
|
|
|
|
KNOWN_BUILDINGS
|
|
|
|
#undef BUILDING
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void disable_building_rename()
|
|
|
|
{
|
2013-09-30 03:19:51 -06:00
|
|
|
is_enabled = false;
|
2012-09-20 01:48:53 -06:00
|
|
|
INTERPOSE_HOOK(dwarf_render_zone_hook, render).remove();
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
#define BUILDING(code, cname, tag) \
|
|
|
|
INTERPOSE_HOOK(cname##_hook, getName).remove();
|
|
|
|
KNOWN_BUILDINGS
|
|
|
|
#undef BUILDING
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_enabled_building(char code)
|
|
|
|
{
|
|
|
|
switch (code) {
|
|
|
|
#define BUILDING(code, cname, tag) \
|
|
|
|
case code: return INTERPOSE_HOOK(cname##_hook, getName).is_applied();
|
|
|
|
KNOWN_BUILDINGS
|
|
|
|
#undef BUILDING
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_buildings(bool enable)
|
|
|
|
{
|
|
|
|
disable_building_rename();
|
|
|
|
|
|
|
|
if (enable)
|
|
|
|
{
|
2012-10-06 03:46:20 -06:00
|
|
|
auto entry = World::GetPersistentData("rename/building_types");
|
2012-09-05 11:27:42 -06:00
|
|
|
|
|
|
|
if (entry.isValid())
|
|
|
|
{
|
|
|
|
std::string val = entry.val();
|
|
|
|
for (size_t i = 0; i < val.size(); i++)
|
|
|
|
enable_building_rename(val[i], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool canRenameBuilding(df::building *bld)
|
|
|
|
{
|
|
|
|
return getBuildingCode(bld) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isRenamingBuilding(df::building *bld)
|
|
|
|
{
|
|
|
|
return is_enabled_building(getBuildingCode(bld));
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool renameBuilding(df::building *bld, std::string name)
|
|
|
|
{
|
|
|
|
char code = getBuildingCode(bld);
|
|
|
|
if (code == 0 && !name.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!name.empty() && !is_enabled_building(code))
|
|
|
|
{
|
2012-10-06 03:46:20 -06:00
|
|
|
auto entry = World::GetPersistentData("rename/building_types", NULL);
|
2012-09-05 11:27:42 -06:00
|
|
|
if (!entry.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!enable_building_rename(code, true))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
entry.val().push_back(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
bld->name = name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-22 06:44:36 -07:00
|
|
|
static df::squad *getSquadByIndex(unsigned idx)
|
|
|
|
{
|
2023-01-05 18:11:01 -07:00
|
|
|
auto entity = df::historical_entity::find(plotinfo->group_id);
|
2012-02-22 06:44:36 -07:00
|
|
|
if (!entity)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (idx >= entity->squads.size())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return df::squad::find(entity->squads[idx]);
|
|
|
|
}
|
|
|
|
|
2012-03-17 10:04:15 -06:00
|
|
|
static command_result RenameSquad(color_ostream &stream, const RenameSquadIn *in)
|
2012-03-15 03:01:23 -06:00
|
|
|
{
|
|
|
|
df::squad *squad = df::squad::find(in->squad_id());
|
|
|
|
if (!squad)
|
|
|
|
return CR_NOT_FOUND;
|
|
|
|
|
|
|
|
if (in->has_nickname())
|
2012-04-07 09:08:30 -06:00
|
|
|
Translation::setNickname(&squad->name, UTF2DF(in->nickname()));
|
2012-03-15 03:01:23 -06:00
|
|
|
if (in->has_alias())
|
2012-04-05 08:10:16 -06:00
|
|
|
squad->alias = UTF2DF(in->alias());
|
2012-03-15 03:01:23 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-17 10:04:15 -06:00
|
|
|
static command_result RenameUnit(color_ostream &stream, const RenameUnitIn *in)
|
2012-03-15 03:01:23 -06:00
|
|
|
{
|
|
|
|
df::unit *unit = df::unit::find(in->unit_id());
|
|
|
|
if (!unit)
|
|
|
|
return CR_NOT_FOUND;
|
|
|
|
|
|
|
|
if (in->has_nickname())
|
2012-04-07 09:08:30 -06:00
|
|
|
Units::setNickname(unit, UTF2DF(in->nickname()));
|
2012-03-15 03:01:23 -06:00
|
|
|
if (in->has_profession())
|
2012-04-05 08:10:16 -06:00
|
|
|
unit->custom_profession = UTF2DF(in->profession());
|
2012-03-15 03:01:23 -06:00
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
static command_result RenameBuilding(color_ostream &stream, const RenameBuildingIn *in)
|
|
|
|
{
|
|
|
|
auto building = df::building::find(in->building_id());
|
|
|
|
if (!building)
|
|
|
|
return CR_NOT_FOUND;
|
|
|
|
|
|
|
|
if (in->has_name())
|
|
|
|
{
|
|
|
|
if (!renameBuilding(building, in->name()))
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|
|
|
|
|
2012-03-15 03:01:23 -06:00
|
|
|
DFhackCExport RPCService *plugin_rpcconnect(color_ostream &)
|
|
|
|
{
|
|
|
|
RPCService *svc = new RPCService();
|
|
|
|
svc->addFunction("RenameSquad", RenameSquad);
|
|
|
|
svc->addFunction("RenameUnit", RenameUnit);
|
2012-09-05 11:27:42 -06:00
|
|
|
svc->addFunction("RenameBuilding", RenameBuilding);
|
2012-03-15 03:01:23 -06:00
|
|
|
return svc;
|
|
|
|
}
|
|
|
|
|
2012-09-05 11:27:42 -06:00
|
|
|
DFHACK_PLUGIN_LUA_FUNCTIONS {
|
|
|
|
DFHACK_LUA_FUNCTION(canRenameBuilding),
|
|
|
|
DFHACK_LUA_FUNCTION(isRenamingBuilding),
|
|
|
|
DFHACK_LUA_FUNCTION(renameBuilding),
|
|
|
|
DFHACK_LUA_END
|
|
|
|
};
|
|
|
|
|
2012-03-10 04:55:42 -07:00
|
|
|
static command_result rename(color_ostream &out, vector <string> ¶meters)
|
2011-12-29 06:37:07 -07:00
|
|
|
{
|
2012-03-10 04:55:42 -07:00
|
|
|
CoreSuspender suspend;
|
2011-12-29 06:37:07 -07:00
|
|
|
|
|
|
|
string cmd;
|
|
|
|
if (!parameters.empty())
|
|
|
|
cmd = parameters[0];
|
|
|
|
|
2012-01-14 08:31:43 -07:00
|
|
|
if (cmd == "squad")
|
|
|
|
{
|
2011-12-29 06:37:07 -07:00
|
|
|
if (parameters.size() != 3)
|
2012-01-14 08:31:43 -07:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2011-12-29 06:37:07 -07:00
|
|
|
int id = atoi(parameters[1].c_str());
|
2012-02-22 06:44:36 -07:00
|
|
|
df::squad *squad = getSquadByIndex(id-1);
|
|
|
|
|
|
|
|
if (!squad) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Couldn't find squad with index %d.\n", id);
|
2012-01-14 08:31:43 -07:00
|
|
|
return CR_WRONG_USAGE;
|
2011-12-29 06:37:07 -07:00
|
|
|
}
|
|
|
|
|
2012-02-22 06:44:36 -07:00
|
|
|
squad->alias = parameters[2];
|
2012-01-14 08:31:43 -07:00
|
|
|
}
|
|
|
|
else if (cmd == "hotkey")
|
|
|
|
{
|
2011-12-29 06:37:07 -07:00
|
|
|
if (parameters.size() != 3)
|
2012-01-14 08:31:43 -07:00
|
|
|
return CR_WRONG_USAGE;
|
2011-12-29 06:37:07 -07:00
|
|
|
|
|
|
|
int id = atoi(parameters[1].c_str());
|
|
|
|
if (id < 1 || id > 16) {
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Invalid hotkey index\n");
|
2012-01-14 08:31:43 -07:00
|
|
|
return CR_WRONG_USAGE;
|
2011-12-29 06:37:07 -07:00
|
|
|
}
|
|
|
|
|
2023-01-05 18:11:01 -07:00
|
|
|
plotinfo->main.hotkeys[id-1].name = parameters[2];
|
2012-01-14 08:31:43 -07:00
|
|
|
}
|
|
|
|
else if (cmd == "unit")
|
|
|
|
{
|
|
|
|
if (parameters.size() != 2)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-04-17 08:57:41 -06:00
|
|
|
df::unit *unit = Gui::getSelectedUnit(out, true);
|
2012-01-14 08:31:43 -07:00
|
|
|
if (!unit)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-04-07 09:08:30 -06:00
|
|
|
Units::setNickname(unit, parameters[1]);
|
2012-01-14 08:31:43 -07:00
|
|
|
}
|
2012-01-15 04:57:33 -07:00
|
|
|
else if (cmd == "unit-profession")
|
|
|
|
{
|
|
|
|
if (parameters.size() != 2)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-04-17 08:57:41 -06:00
|
|
|
df::unit *unit = Gui::getSelectedUnit(out, true);
|
2012-01-15 04:57:33 -07:00
|
|
|
if (!unit)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
|
|
|
unit->custom_profession = parameters[1];
|
|
|
|
}
|
2012-09-05 11:27:42 -06:00
|
|
|
else if (cmd == "building")
|
|
|
|
{
|
|
|
|
if (parameters.size() != 2)
|
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-09-20 01:11:59 -06:00
|
|
|
df::building *bld = Gui::getSelectedBuilding(out, true);
|
|
|
|
if (!bld)
|
2012-09-05 11:27:42 -06:00
|
|
|
return CR_WRONG_USAGE;
|
|
|
|
|
2012-09-20 01:11:59 -06:00
|
|
|
if (!renameBuilding(bld, parameters[1]))
|
2012-09-05 11:27:42 -06:00
|
|
|
{
|
|
|
|
out.printerr("This type of building is not supported.\n");
|
|
|
|
return CR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2012-01-14 08:31:43 -07:00
|
|
|
else
|
|
|
|
{
|
2011-12-29 06:37:07 -07:00
|
|
|
if (!parameters.empty() && cmd != "?")
|
2012-03-10 04:55:42 -07:00
|
|
|
out.printerr("Invalid command: %s\n", cmd.c_str());
|
2012-01-14 08:31:43 -07:00
|
|
|
return CR_WRONG_USAGE;
|
2011-12-29 06:37:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return CR_OK;
|
|
|
|
}
|