Add a ListSquads request.

develop
Alexander Gavrilov 2012-03-17 20:04:15 +04:00
parent 3d80a1ee35
commit aa63493bb8
7 changed files with 103 additions and 42 deletions

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

@ -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<df::name>(out->mutable_##name());
#define BITFIELD(name) describe_bitfield<df::name>(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()

@ -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<In*>(RPCFunctionBase::in()); }
Out *out() { return static_cast<Out*>(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<In*>(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<In*>(RPCFunctionBase::in()); }
Out *out() { return static_cast<Out*>(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<In*>(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<typename In, typename Out>
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<In,Out>(this, name, fptr));
functions.push_back(new ServerFunction<In,Out>(this, name, flags, fptr));
}
template<typename In>
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<In>(this, name, fptr));
functions.push_back(new VoidServerFunction<In>(this, name, flags, fptr));
}
protected:
@ -175,19 +185,21 @@ namespace DFHack
template<typename Svc, typename In, typename Out>
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<Svc,In,Out>(this, name, fptr));
functions.push_back(new ServerMethod<Svc,In,Out>(this, name, flags, fptr));
}
template<typename Svc, typename In>
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<Svc,In>(this, name, fptr));
functions.push_back(new VoidServerMethod<Svc,In>(this, name, flags, fptr));
}
};

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

@ -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;
};
};
message ListSquadsIn {}
message ListSquadsOut {
repeated BasicSquadInfo value = 1;
}

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

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