Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f970f4

Browse files
committed
convert rename.cpp into an example for how to do rpc
1 parent b6b61d9 commit 1f970f4

7 files changed

Lines changed: 82 additions & 495 deletions

File tree

docs/about/Removed.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ plants
314314
======
315315
Renamed to `plant`.
316316

317+
.. _rename:
318+
319+
rename
320+
======
321+
Superseded by vanilla rename capabilities and `gui/rename`.
322+
317323
.. _resume:
318324

319325
resume

docs/plugins/rename.rst

Lines changed: 0 additions & 33 deletions
This file was deleted.

plugins/CMakeLists.txt

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,16 @@ if(INSTALL_DATA_FILES)
2222
FILES_MATCHING PATTERN "*.diff")
2323
endif()
2424

25-
# Protobuf
26-
file(GLOB PROJECT_PROTOS ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)
27-
28-
string(REPLACE ".proto" ".pb.cc" PROJECT_PROTO_SRCS "${PROJECT_PROTOS}")
29-
string(REPLACE ".proto" ".pb.h" PROJECT_PROTO_HDRS "${PROJECT_PROTOS}")
30-
string(REPLACE "/proto/" "/proto/tmp/" PROJECT_PROTO_TMP_FILES "${PROJECT_PROTO_SRCS};${PROJECT_PROTO_HDRS}")
31-
set_source_files_properties(${PROJECT_PROTO_SRCS} ${PROJECT_PROTO_HDRS}
32-
PROPERTIES GENERATED TRUE)
33-
34-
# Force a re-gen if any *.pb.* files are missing
35-
# (only runs when cmake is run, but better than nothing)
36-
foreach(file IN LISTS PROJECT_PROTO_SRCS PROJECT_PROTO_HDRS)
37-
if(NOT EXISTS ${file})
38-
# message("Resetting generate_proto because '${file}' is missing")
39-
file(REMOVE ${PROJECT_PROTO_TMP_FILES})
40-
break()
41-
endif()
42-
endforeach()
43-
44-
add_custom_command(
45-
OUTPUT ${PROJECT_PROTO_TMP_FILES}
46-
COMMAND protoc-bin -I=${dfhack_SOURCE_DIR}/library/proto/
47-
-I=${CMAKE_CURRENT_SOURCE_DIR}/proto/
48-
--cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/proto/tmp/
49-
${PROJECT_PROTOS}
50-
COMMAND ${PERL_EXECUTABLE} ${dfhack_SOURCE_DIR}/depends/copy-if-different.pl
51-
${PROJECT_PROTO_TMP_FILES}
52-
${CMAKE_CURRENT_SOURCE_DIR}/proto/
53-
COMMENT "Generating plugin protobufs"
54-
DEPENDS protoc-bin ${PROJECT_PROTOS}
55-
)
56-
add_custom_target(generate_proto DEPENDS ${PROJECT_PROTO_TMP_FILES})
57-
5825
set_source_files_properties( Brushes.h PROPERTIES HEADER_FILE_ONLY TRUE )
5926

6027
# Plugins
6128
# If you are adding a plugin that you do not intend to commit to the DFHack repo,
6229
# see instructions for adding "external" plugins at the end of this file.
6330

31+
# Example plugin that uses protobufs
32+
# proto file must be in the proto/ folder
33+
# dfhack_plugin(rename rename.cpp LINK_LIBRARIES lua PROTOBUFS rename)
34+
6435
option(BUILD_SUPPORTED "Build the supported plugins (reveal, probe, etc.)." ON)
6536
if(BUILD_SUPPORTED)
6637
dfhack_plugin(3dveins 3dveins.cpp)
@@ -133,7 +104,6 @@ if(BUILD_SUPPORTED)
133104
#dfhack_plugin(power-meter power-meter.cpp LINK_LIBRARIES lua)
134105
dfhack_plugin(regrass regrass.cpp LINK_LIBRARIES lua)
135106
add_subdirectory(remotefortressreader)
136-
#dfhack_plugin(rename rename.cpp LINK_LIBRARIES lua PROTOBUFS rename)
137107
#add_subdirectory(rendermax)
138108
dfhack_plugin(reveal reveal.cpp LINK_LIBRARIES lua)
139109
dfhack_plugin(seedwatch seedwatch.cpp LINK_LIBRARIES lua)

plugins/examples/rpc_example.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include "PluginManager.h"
2+
3+
#include "RemoteServer.h"
4+
#include "example.pb.h" // see plugins/proto/example.proto
5+
6+
using std::vector;
7+
using std::string;
8+
9+
using namespace DFHack;
10+
using namespace dfproto;
11+
12+
DFHACK_PLUGIN("example_rpc");
13+
14+
DFhackCExport command_result plugin_init (color_ostream &out, std::vector<PluginCommand> &commands) {
15+
return CR_OK;
16+
}
17+
18+
DFhackCExport command_result plugin_shutdown (color_ostream &out) {
19+
return CR_OK;
20+
}
21+
22+
static command_result RenameSquad(color_ostream &stream, const RenameSquadIn *in) {
23+
df::squad *squad = df::squad::find(in->squad_id());
24+
if (!squad)
25+
return CR_NOT_FOUND;
26+
27+
if (in->has_nickname())
28+
Translation::setNickname(&squad->name, UTF2DF(in->nickname()));
29+
if (in->has_alias())
30+
squad->alias = UTF2DF(in->alias());
31+
32+
return CR_OK;
33+
}
34+
35+
static command_result RenameUnit(color_ostream &stream, const RenameUnitIn *in) {
36+
df::unit *unit = df::unit::find(in->unit_id());
37+
if (!unit)
38+
return CR_NOT_FOUND;
39+
40+
if (in->has_nickname())
41+
Units::setNickname(unit, UTF2DF(in->nickname()));
42+
if (in->has_profession())
43+
unit->custom_profession = UTF2DF(in->profession());
44+
45+
return CR_OK;
46+
}
47+
48+
static command_result RenameBuilding(color_ostream &stream, const RenameBuildingIn *in) {
49+
auto building = df::building::find(in->building_id());
50+
if (!building)
51+
return CR_NOT_FOUND;
52+
53+
if (in->has_name())
54+
{
55+
if (!renameBuilding(building, in->name()))
56+
return CR_FAILURE;
57+
}
58+
59+
return CR_OK;
60+
}
61+
62+
DFhackCExport RPCService *plugin_rpcconnect(color_ostream &out) {
63+
RPCService *svc = new RPCService();
64+
svc->addFunction("RenameSquad", RenameSquad);
65+
svc->addFunction("RenameUnit", RenameUnit);
66+
svc->addFunction("RenameBuilding", RenameBuilding);
67+
return svc;
68+
}

plugins/lua/rename.lua

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,24 @@ package dfproto;
44

55
option optimize_for = LITE_RUNTIME;
66

7-
// DISABLED Plugin: rename
7+
// Plugin: mypluginname
88

9-
// DISABLED RPC RenameSquad : RenameSquadIn -> EmptyMessage
9+
// RPC RenameSquad : RenameSquadIn -> EmptyMessage
1010
message RenameSquadIn {
1111
required int32 squad_id = 1;
12-
1312
optional string nickname = 2;
1413
optional string alias = 3;
1514
}
1615

17-
// DISABLED RPC RenameUnit : RenameUnitIn -> EmptyMessage
16+
// RPC RenameUnit : RenameUnitIn -> EmptyMessage
1817
message RenameUnitIn {
1918
required int32 unit_id = 1;
20-
2119
optional string nickname = 2;
2220
optional string profession = 3;
2321
}
2422

25-
// DISABLED RPC RenameBuilding : RenameBuildingIn -> EmptyMessage
23+
// RPC RenameBuilding : RenameBuildingIn -> EmptyMessage
2624
message RenameBuildingIn {
2725
required int32 building_id = 1;
28-
2926
optional string name = 2;
3027
}

0 commit comments

Comments
 (0)