summaryrefslogtreecommitdiff
path: root/plugins/rename.cpp
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-03-15 13:01:23 +0400
committerAlexander Gavrilov2012-03-15 13:01:23 +0400
commit87f925e72e7c6f923a595cc930728483a3f1422e (patch)
tree4da81e9346e9f21599cea8c7a2d95d8da4628e2e /plugins/rename.cpp
parente7851f5abdd1a84d29020f2789efc0f932284bf0 (diff)
downloaddfhack-87f925e72e7c6f923a595cc930728483a3f1422e.tar.gz
dfhack-87f925e72e7c6f923a595cc930728483a3f1422e.tar.bz2
dfhack-87f925e72e7c6f923a595cc930728483a3f1422e.tar.xz
Add support for exporting functions from plugins, with example in rename.
TODO: test by actually calling them remotely.
Diffstat (limited to 'plugins/rename.cpp')
-rw-r--r--plugins/rename.cpp107
1 files changed, 84 insertions, 23 deletions
diff --git a/plugins/rename.cpp b/plugins/rename.cpp
index 1a1a8cc3..ae6edc65 100644
--- a/plugins/rename.cpp
+++ b/plugins/rename.cpp
@@ -17,6 +17,9 @@
#include "df/assumed_identity.h"
#include "df/language_name.h"
+#include "RemoteServer.h"
+#include "rename.pb.h"
+
#include <stdlib.h>
using std::vector;
@@ -24,6 +27,7 @@ using std::string;
using std::endl;
using namespace DFHack;
using namespace df::enums;
+using namespace dfproto;
using df::global::ui;
using df::global::world;
@@ -79,6 +83,85 @@ static df::squad *getSquadByIndex(unsigned idx)
return df::squad::find(entity->squads[idx]);
}
+void setUnitNickname(df::unit *unit, const std::string &nick)
+{
+ // There are >=3 copies of the name, and the one
+ // in the unit is not the authoritative one.
+ // This is the reason why military units often
+ // lose nicknames set from Dwarf Therapist.
+ set_nickname(&unit->name, nick);
+
+ if (unit->status.current_soul)
+ set_nickname(&unit->status.current_soul->name, nick);
+
+ df::historical_figure *figure = df::historical_figure::find(unit->hist_figure_id);
+ if (figure)
+ {
+ set_nickname(&figure->name, nick);
+
+ // v0.34.01: added the vampire's assumed identity
+ if (figure->info && figure->info->reputation)
+ {
+ auto identity = df::assumed_identity::find(figure->info->reputation->cur_identity);
+
+ if (identity)
+ {
+ auto id_hfig = df::historical_figure::find(identity->histfig_id);
+
+ if (id_hfig)
+ {
+ // Even DF doesn't do this bit, because it's apparently
+ // only used for demons masquerading as gods, so you
+ // can't ever change their nickname in-game.
+ set_nickname(&id_hfig->name, nick);
+ }
+ else
+ set_nickname(&identity->name, nick);
+ }
+ }
+ }
+}
+
+static command_result RenameSquad(color_ostream &stream, const RenameSquadRq *in)
+{
+ CoreSuspender suspend;
+
+ df::squad *squad = df::squad::find(in->squad_id());
+ if (!squad)
+ return CR_NOT_FOUND;
+
+ if (in->has_nickname())
+ set_nickname(&squad->name, in->nickname());
+ if (in->has_alias())
+ squad->alias = in->alias();
+
+ return CR_OK;
+}
+
+static command_result RenameUnit(color_ostream &stream, const RenameUnitRq *in)
+{
+ CoreSuspender suspend;
+
+ df::unit *unit = df::unit::find(in->unit_id());
+ if (!unit)
+ return CR_NOT_FOUND;
+
+ if (in->has_nickname())
+ setUnitNickname(unit, in->nickname());
+ if (in->has_profession())
+ unit->custom_profession = in->profession();
+
+ return CR_OK;
+}
+
+DFhackCExport RPCService *plugin_rpcconnect(color_ostream &)
+{
+ RPCService *svc = new RPCService();
+ svc->addFunction("RenameSquad", RenameSquad);
+ svc->addFunction("RenameUnit", RenameUnit);
+ return svc;
+}
+
static command_result rename(color_ostream &out, vector <string> &parameters)
{
CoreSuspender suspend;
@@ -124,29 +207,7 @@ static command_result rename(color_ostream &out, vector <string> &parameters)
if (!unit)
return CR_WRONG_USAGE;
- // There are 3 copies of the name, and the one
- // in the unit is not the authoritative one.
- // This is the reason why military units often
- // lose nicknames set from Dwarf Therapist.
- set_nickname(&unit->name, parameters[1]);
-
- if (unit->status.current_soul)
- set_nickname(&unit->status.current_soul->name, parameters[1]);
-
- df::historical_figure *figure = df::historical_figure::find(unit->hist_figure_id);
- if (figure)
- {
- set_nickname(&figure->name, parameters[1]);
-
- // v0.34.01: added the vampire's assumed identity
- if (figure->info && figure->info->reputation)
- {
- auto identity = df::assumed_identity::find(figure->info->reputation->cur_identity);
-
- if (identity)
- set_nickname(&identity->name, parameters[1]);
- }
- }
+ setUnitNickname(unit, parameters[1]);
}
else if (cmd == "unit-profession")
{