From aa63493bb81160e924382e617ac48431f1fba0c7 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Sat, 17 Mar 2012 20:04:15 +0400 Subject: [PATCH] Add a ListSquads request. --- library/RemoteServer.cpp | 3 ++- library/RemoteTools.cpp | 45 ++++++++++++++++++++++++++----- library/include/RemoteServer.h | 48 +++++++++++++++++++++------------- library/proto/Basic.proto | 24 ++++++++++++----- library/proto/BasicApi.proto | 17 +++++++----- plugins/proto/rename.proto | 4 +-- plugins/rename.cpp | 4 +-- 7 files changed, 103 insertions(+), 42 deletions(-) diff --git a/library/RemoteServer.cpp b/library/RemoteServer.cpp index 8c486aff9..e12b8b043 100644 --- a/library/RemoteServer.cpp +++ b/library/RemoteServer.cpp @@ -331,7 +331,8 @@ void ServerConnection::threadFn(void *arg) // Cleanup if (fn) { - fn->reset(out_size > 128*1024 || in_size > 32*1024); + fn->reset((fn->flags & SF_CALLED_ONCE) || + (out_size > 128*1024 || in_size > 32*1024)); } } diff --git a/library/RemoteTools.cpp b/library/RemoteTools.cpp index ae955012c..1fdd26c74 100644 --- a/library/RemoteTools.cpp +++ b/library/RemoteTools.cpp @@ -54,6 +54,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "modules/Units.h" #include "DataDefs.h" +#include "df/ui.h" #include "df/unit.h" #include "df/unit_soul.h" #include "df/unit_skill.h" @@ -63,6 +64,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "df/creature_raw.h" #include "df/plant_raw.h" #include "df/historical_figure.h" +#include "df/historical_entity.h" +#include "df/squad.h" +#include "df/squad_position.h" #include "BasicApi.pb.h" @@ -268,7 +272,7 @@ void DFHack::describeUnit(BasicUnitInfo *info, df::unit *unit, } static command_result ListEnums(color_ostream &stream, - const EmptyMessage *, ListEnumsRes *out) + const EmptyMessage *, ListEnumsOut *out) { #define ENUM(name) describe_enum(out->mutable_##name()); #define BITFIELD(name) describe_bitfield(out->mutable_##name()); @@ -283,7 +287,7 @@ static command_result ListEnums(color_ostream &stream, #undef BITFIELD } -static void listMaterial(ListMaterialsRes *out, int type, int index, const BasicMaterialInfoMask *mask) +static void listMaterial(ListMaterialsOut *out, int type, int index, const BasicMaterialInfoMask *mask) { MaterialInfo info(type, index); if (info.isValid()) @@ -291,7 +295,7 @@ static void listMaterial(ListMaterialsRes *out, int type, int index, const Basic } static command_result ListMaterials(color_ostream &stream, - const ListMaterialsRq *in, ListMaterialsRes *out) + const ListMaterialsIn *in, ListMaterialsOut *out) { CoreSuspender suspend; @@ -344,7 +348,7 @@ static command_result ListMaterials(color_ostream &stream, } static command_result ListUnits(color_ostream &stream, - const ListUnitsRq *in, ListUnitsRes *out) + const ListUnitsIn *in, ListUnitsOut *out) { CoreSuspender suspend; @@ -379,6 +383,34 @@ static command_result ListUnits(color_ostream &stream, return out->value_size() ? CR_OK : CR_NOT_FOUND; } +static command_result ListSquads(color_ostream &stream, + const ListSquadsIn *in, ListSquadsOut *out) +{ + auto entity = df::historical_entity::find(df::global::ui->group_id); + if (!entity) + return CR_NOT_FOUND; + + for (size_t i = 0; i < entity->squads.size(); i++) + { + auto squad = df::squad::find(entity->squads[i]); + if (!squad) + continue; + + auto item = out->add_value(); + item->set_squad_id(squad->id); + + if (squad->name.has_name) + describeName(item->mutable_name(), &squad->name); + if (!squad->alias.empty()) + item->set_alias(squad->alias); + + for (size_t j = 0; j < squad->positions.size(); j++) + item->add_members(squad->positions[j]->occupant); + } + + return CR_OK; +} + CoreService::CoreService() { suspend_depth = 0; @@ -390,9 +422,10 @@ CoreService::CoreService() { addMethod("CoreSuspend", &CoreService::CoreSuspend); addMethod("CoreResume", &CoreService::CoreResume); - addFunction("ListEnums", ListEnums); - addFunction("ListMaterials", ListMaterials); + addFunction("ListEnums", ListEnums, SF_CALLED_ONCE); + addFunction("ListMaterials", ListMaterials, SF_CALLED_ONCE); addFunction("ListUnits", ListUnits); + addFunction("ListSquads", ListSquads); } CoreService::~CoreService() diff --git a/library/include/RemoteServer.h b/library/include/RemoteServer.h index 7e45ba560..d7b216f7f 100644 --- a/library/include/RemoteServer.h +++ b/library/include/RemoteServer.h @@ -40,9 +40,16 @@ namespace DFHack class DFHACK_EXPORT RPCService; + enum ServerFunctionFlags { + // The function is expected to be called only once per client, + // so always delete all cached buffers after processing. + SF_CALLED_ONCE = 1 + }; + class DFHACK_EXPORT ServerFunctionBase : public RPCFunctionBase { public: const char *const name; + const int flags; virtual command_result execute(color_ostream &stream) = 0; @@ -51,8 +58,9 @@ namespace DFHack protected: friend class RPCService; - ServerFunctionBase(const message_type *in, const message_type *out, RPCService *owner, const char *name) - : RPCFunctionBase(in, out), name(name), owner(owner), id(-1) + ServerFunctionBase(const message_type *in, const message_type *out, + RPCService *owner, const char *name, int flags) + : RPCFunctionBase(in, out), name(name), flags(flags), owner(owner), id(-1) {} RPCService *owner; @@ -67,8 +75,8 @@ namespace DFHack In *in() { return static_cast(RPCFunctionBase::in()); } Out *out() { return static_cast(RPCFunctionBase::out()); } - ServerFunction(RPCService *owner, const char *name, function_type fptr) - : ServerFunctionBase(&In::default_instance(), &Out::default_instance(), owner, name), + ServerFunction(RPCService *owner, const char *name, int flags, function_type fptr) + : ServerFunctionBase(&In::default_instance(), &Out::default_instance(), owner, name, flags), fptr(fptr) {} virtual command_result execute(color_ostream &stream) { return fptr(stream, in(), out()); } @@ -84,8 +92,8 @@ namespace DFHack In *in() { return static_cast(RPCFunctionBase::in()); } - VoidServerFunction(RPCService *owner, const char *name, function_type fptr) - : ServerFunctionBase(&In::default_instance(), &EmptyMessage::default_instance(), owner, name), + VoidServerFunction(RPCService *owner, const char *name, int flags, function_type fptr) + : ServerFunctionBase(&In::default_instance(), &EmptyMessage::default_instance(), owner, name, flags), fptr(fptr) {} virtual command_result execute(color_ostream &stream) { return fptr(stream, in()); } @@ -102,8 +110,8 @@ namespace DFHack In *in() { return static_cast(RPCFunctionBase::in()); } Out *out() { return static_cast(RPCFunctionBase::out()); } - ServerMethod(RPCService *owner, const char *name, function_type fptr) - : ServerFunctionBase(&In::default_instance(), &Out::default_instance(), owner, name), + ServerMethod(RPCService *owner, const char *name, int flags, function_type fptr) + : ServerFunctionBase(&In::default_instance(), &Out::default_instance(), owner, name, flags), fptr(fptr) {} virtual command_result execute(color_ostream &stream) { @@ -121,8 +129,8 @@ namespace DFHack In *in() { return static_cast(RPCFunctionBase::in()); } - VoidServerMethod(RPCService *owner, const char *name, function_type fptr) - : ServerFunctionBase(&In::default_instance(), &EmptyMessage::default_instance(), owner, name), + VoidServerMethod(RPCService *owner, const char *name, int flags, function_type fptr) + : ServerFunctionBase(&In::default_instance(), &EmptyMessage::default_instance(), owner, name, flags), fptr(fptr) {} virtual command_result execute(color_ostream &stream) { @@ -154,19 +162,21 @@ namespace DFHack template void addFunction( const char *name, - command_result (*fptr)(color_ostream &out, const In *input, Out *output) + command_result (*fptr)(color_ostream &out, const In *input, Out *output), + int flags = 0 ) { assert(!owner); - functions.push_back(new ServerFunction(this, name, fptr)); + functions.push_back(new ServerFunction(this, name, flags, fptr)); } template void addFunction( const char *name, - command_result (*fptr)(color_ostream &out, const In *input) + command_result (*fptr)(color_ostream &out, const In *input), + int flags = 0 ) { assert(!owner); - functions.push_back(new VoidServerFunction(this, name, fptr)); + functions.push_back(new VoidServerFunction(this, name, flags, fptr)); } protected: @@ -175,19 +185,21 @@ namespace DFHack template void addMethod( const char *name, - command_result (Svc::*fptr)(color_ostream &out, const In *input, Out *output) + command_result (Svc::*fptr)(color_ostream &out, const In *input, Out *output), + int flags = 0 ) { assert(!owner); - functions.push_back(new ServerMethod(this, name, fptr)); + functions.push_back(new ServerMethod(this, name, flags, fptr)); } template void addMethod( const char *name, - command_result (Svc::*fptr)(color_ostream &out, const In *input) + command_result (Svc::*fptr)(color_ostream &out, const In *input), + int flags = 0 ) { assert(!owner); - functions.push_back(new VoidServerMethod(this, name, fptr)); + functions.push_back(new VoidServerMethod(this, name, flags, fptr)); } }; diff --git a/library/proto/Basic.proto b/library/proto/Basic.proto index 1a9a8f1b4..6e58345ff 100644 --- a/library/proto/Basic.proto +++ b/library/proto/Basic.proto @@ -52,10 +52,10 @@ message BasicMaterialInfoMask { Pressed = 5; }; repeated StateType states = 1; + optional int32 temperature = 4 [default = 10015]; optional bool flags = 2 [default = false]; optional bool reaction = 3 [default = false]; - optional int32 temperature = 4 [default = 10015]; }; message NameInfo { @@ -68,6 +68,12 @@ message NameInfo { optional string english_name = 5; }; +message SkillInfo { + required int32 id = 1; + required int32 level = 2; + required int32 experience = 3; +}; + message BasicUnitInfo { required int32 unit_id = 1; @@ -86,12 +92,7 @@ message BasicUnitInfo { repeated int32 labors = 11; - message Skill { - required int32 id = 1; - required int32 level = 2; - required int32 experience = 3; - }; - repeated Skill skills = 12; + repeated SkillInfo skills = 12; required int32 pos_x = 13; required int32 pos_y = 14; @@ -102,3 +103,12 @@ message BasicUnitInfoMask { optional bool labors = 1 [default = false]; optional bool skills = 2 [default = false]; }; + +message BasicSquadInfo { + required int32 squad_id = 1; + + optional NameInfo name = 2; + optional string alias = 3; + + repeated sint32 members = 4; +}; diff --git a/library/proto/BasicApi.proto b/library/proto/BasicApi.proto index ed1276fef..33f2a5764 100644 --- a/library/proto/BasicApi.proto +++ b/library/proto/BasicApi.proto @@ -4,7 +4,7 @@ option optimize_for = LITE_RUNTIME; import "Basic.proto"; -message ListEnumsRes { +message ListEnumsOut { repeated EnumItemName material_flags = 1; repeated EnumItemName inorganic_flags = 2; @@ -16,7 +16,7 @@ message ListEnumsRes { repeated EnumItemName job_skill = 7; }; -message ListMaterialsRq { +message ListMaterialsIn { optional BasicMaterialInfoMask mask = 1; repeated BasicMaterialId id_list = 2; optional bool builtin = 3; @@ -24,17 +24,22 @@ message ListMaterialsRq { optional bool creatures = 5; optional bool plants = 6; }; -message ListMaterialsRes { +message ListMaterialsOut { repeated BasicMaterialInfo value = 1; }; -message ListUnitsRq { +message ListUnitsIn { optional BasicUnitInfoMask mask = 1; repeated int32 id_list = 2; optional int32 race = 3; optional int32 civ_id = 4; }; -message ListUnitsRes { +message ListUnitsOut { repeated BasicUnitInfo value = 1; -}; \ No newline at end of file +}; + +message ListSquadsIn {} +message ListSquadsOut { + repeated BasicSquadInfo value = 1; +} diff --git a/plugins/proto/rename.proto b/plugins/proto/rename.proto index a139ceaa0..a6c3b055a 100644 --- a/plugins/proto/rename.proto +++ b/plugins/proto/rename.proto @@ -2,14 +2,14 @@ package dfproto; option optimize_for = LITE_RUNTIME; -message RenameSquadRq { +message RenameSquadIn { required int32 squad_id = 1; optional string nickname = 2; optional string alias = 3; } -message RenameUnitRq { +message RenameUnitIn { required int32 unit_id = 1; optional string nickname = 2; diff --git a/plugins/rename.cpp b/plugins/rename.cpp index ae6edc652..9544901fc 100644 --- a/plugins/rename.cpp +++ b/plugins/rename.cpp @@ -122,7 +122,7 @@ void setUnitNickname(df::unit *unit, const std::string &nick) } } -static command_result RenameSquad(color_ostream &stream, const RenameSquadRq *in) +static command_result RenameSquad(color_ostream &stream, const RenameSquadIn *in) { CoreSuspender suspend; @@ -138,7 +138,7 @@ static command_result RenameSquad(color_ostream &stream, const RenameSquadRq *in return CR_OK; } -static command_result RenameUnit(color_ostream &stream, const RenameUnitRq *in) +static command_result RenameUnit(color_ostream &stream, const RenameUnitIn *in) { CoreSuspender suspend;