Skip to content

Commit

Permalink
convert rename.cpp into an example for how to do rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Jan 3, 2025
1 parent b6b61d9 commit 1f970f4
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 495 deletions.
6 changes: 6 additions & 0 deletions docs/about/Removed.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,12 @@ plants
======
Renamed to `plant`.

.. _rename:

rename
======
Superseded by vanilla rename capabilities and `gui/rename`.

.. _resume:

resume
Expand Down
33 changes: 0 additions & 33 deletions docs/plugins/rename.rst

This file was deleted.

38 changes: 4 additions & 34 deletions plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,16 @@ if(INSTALL_DATA_FILES)
FILES_MATCHING PATTERN "*.diff")
endif()

# Protobuf
file(GLOB PROJECT_PROTOS ${CMAKE_CURRENT_SOURCE_DIR}/proto/*.proto)

string(REPLACE ".proto" ".pb.cc" PROJECT_PROTO_SRCS "${PROJECT_PROTOS}")
string(REPLACE ".proto" ".pb.h" PROJECT_PROTO_HDRS "${PROJECT_PROTOS}")
string(REPLACE "/proto/" "/proto/tmp/" PROJECT_PROTO_TMP_FILES "${PROJECT_PROTO_SRCS};${PROJECT_PROTO_HDRS}")
set_source_files_properties(${PROJECT_PROTO_SRCS} ${PROJECT_PROTO_HDRS}
PROPERTIES GENERATED TRUE)

# Force a re-gen if any *.pb.* files are missing
# (only runs when cmake is run, but better than nothing)
foreach(file IN LISTS PROJECT_PROTO_SRCS PROJECT_PROTO_HDRS)
if(NOT EXISTS ${file})
# message("Resetting generate_proto because '${file}' is missing")
file(REMOVE ${PROJECT_PROTO_TMP_FILES})
break()
endif()
endforeach()

add_custom_command(
OUTPUT ${PROJECT_PROTO_TMP_FILES}
COMMAND protoc-bin -I=${dfhack_SOURCE_DIR}/library/proto/
-I=${CMAKE_CURRENT_SOURCE_DIR}/proto/
--cpp_out=${CMAKE_CURRENT_SOURCE_DIR}/proto/tmp/
${PROJECT_PROTOS}
COMMAND ${PERL_EXECUTABLE} ${dfhack_SOURCE_DIR}/depends/copy-if-different.pl
${PROJECT_PROTO_TMP_FILES}
${CMAKE_CURRENT_SOURCE_DIR}/proto/
COMMENT "Generating plugin protobufs"
DEPENDS protoc-bin ${PROJECT_PROTOS}
)
add_custom_target(generate_proto DEPENDS ${PROJECT_PROTO_TMP_FILES})

set_source_files_properties( Brushes.h PROPERTIES HEADER_FILE_ONLY TRUE )

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

# Example plugin that uses protobufs
# proto file must be in the proto/ folder
# dfhack_plugin(rename rename.cpp LINK_LIBRARIES lua PROTOBUFS rename)

option(BUILD_SUPPORTED "Build the supported plugins (reveal, probe, etc.)." ON)
if(BUILD_SUPPORTED)
dfhack_plugin(3dveins 3dveins.cpp)
Expand Down Expand Up @@ -133,7 +104,6 @@ if(BUILD_SUPPORTED)
#dfhack_plugin(power-meter power-meter.cpp LINK_LIBRARIES lua)
dfhack_plugin(regrass regrass.cpp LINK_LIBRARIES lua)
add_subdirectory(remotefortressreader)
#dfhack_plugin(rename rename.cpp LINK_LIBRARIES lua PROTOBUFS rename)
#add_subdirectory(rendermax)
dfhack_plugin(reveal reveal.cpp LINK_LIBRARIES lua)
dfhack_plugin(seedwatch seedwatch.cpp LINK_LIBRARIES lua)
Expand Down
68 changes: 68 additions & 0 deletions plugins/examples/rpc_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "PluginManager.h"

#include "RemoteServer.h"
#include "example.pb.h" // see plugins/proto/example.proto

using std::vector;
using std::string;

using namespace DFHack;
using namespace dfproto;

DFHACK_PLUGIN("example_rpc");

DFhackCExport command_result plugin_init (color_ostream &out, std::vector<PluginCommand> &commands) {
return CR_OK;
}

DFhackCExport command_result plugin_shutdown (color_ostream &out) {
return CR_OK;
}

static command_result RenameSquad(color_ostream &stream, const RenameSquadIn *in) {
df::squad *squad = df::squad::find(in->squad_id());
if (!squad)
return CR_NOT_FOUND;

if (in->has_nickname())
Translation::setNickname(&squad->name, UTF2DF(in->nickname()));
if (in->has_alias())
squad->alias = UTF2DF(in->alias());

return CR_OK;
}

static command_result RenameUnit(color_ostream &stream, const RenameUnitIn *in) {
df::unit *unit = df::unit::find(in->unit_id());
if (!unit)
return CR_NOT_FOUND;

if (in->has_nickname())
Units::setNickname(unit, UTF2DF(in->nickname()));
if (in->has_profession())
unit->custom_profession = UTF2DF(in->profession());

return CR_OK;
}

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;
}

DFhackCExport RPCService *plugin_rpcconnect(color_ostream &out) {
RPCService *svc = new RPCService();
svc->addFunction("RenameSquad", RenameSquad);
svc->addFunction("RenameUnit", RenameUnit);
svc->addFunction("RenameBuilding", RenameBuilding);
return svc;
}
13 changes: 0 additions & 13 deletions plugins/lua/rename.lua

This file was deleted.

11 changes: 4 additions & 7 deletions plugins/proto/rename.proto → plugins/proto/example.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ package dfproto;

option optimize_for = LITE_RUNTIME;

// DISABLED Plugin: rename
// Plugin: mypluginname

// DISABLED RPC RenameSquad : RenameSquadIn -> EmptyMessage
// RPC RenameSquad : RenameSquadIn -> EmptyMessage
message RenameSquadIn {
required int32 squad_id = 1;

optional string nickname = 2;
optional string alias = 3;
}

// DISABLED RPC RenameUnit : RenameUnitIn -> EmptyMessage
// RPC RenameUnit : RenameUnitIn -> EmptyMessage
message RenameUnitIn {
required int32 unit_id = 1;

optional string nickname = 2;
optional string profession = 3;
}

// DISABLED RPC RenameBuilding : RenameBuildingIn -> EmptyMessage
// RPC RenameBuilding : RenameBuildingIn -> EmptyMessage
message RenameBuildingIn {
required int32 building_id = 1;

optional string name = 2;
}
Loading

0 comments on commit 1f970f4

Please sign in to comment.