server/src/bind_message.c

336 lines
8.5 KiB
C
Raw Normal View History

2010-08-08 10:06:34 +02:00
#include <platform.h>
#include <kernel/config.h>
2011-03-07 17:26:50 +01:00
/* kernel includes */
2010-08-08 10:06:34 +02:00
#include <kernel/faction.h>
#include <kernel/item.h>
#include <kernel/message.h>
#include <kernel/reports.h>
#include <kernel/region.h>
#include <kernel/unit.h>
2011-03-07 17:26:50 +01:00
/* util includes */
2010-08-08 10:06:34 +02:00
#include <util/message.h>
2011-03-07 17:26:50 +01:00
/* lua includes */
2010-08-08 10:06:34 +02:00
#include <tolua.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
2010-08-08 10:06:34 +02:00
#define E_OK 0
#define E_INVALID_MESSAGE 1
#define E_INVALID_PARAMETER_NAME 2
#define E_INVALID_PARAMETER_TYPE 3
#define E_INVALID_PARAMETER_VALUE 4
typedef struct lua_message {
2011-03-07 08:02:35 +01:00
const message_type *mtype;
message *msg;
variant *args;
2010-08-08 10:06:34 +02:00
} lua_message;
2011-03-07 08:02:35 +01:00
int mtype_get_param(const message_type * mtype, const char *param)
2010-08-08 10:06:34 +02:00
{
int i;
2011-03-07 08:02:35 +01:00
for (i = 0; i != mtype->nparameters; ++i) {
if (strcmp(mtype->pnames[i], param) == 0) {
2010-08-08 10:06:34 +02:00
return i;
}
}
return mtype->nparameters;
}
2011-03-07 08:02:35 +01:00
static lua_message *msg_create_message(const char *type)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = malloc(sizeof(lua_message));
2010-08-08 10:06:34 +02:00
lmsg->msg = 0;
lmsg->args = 0;
lmsg->mtype = mt_find(type);
if (lmsg->mtype) {
2011-03-07 08:02:35 +01:00
lmsg->args = (variant *) calloc(lmsg->mtype->nparameters, sizeof(variant));
2010-08-08 10:06:34 +02:00
}
return lmsg;
}
/*
static void
msg_destroy_message(lua_message * msg)
{
if (msg->msg) msg_release(msg->msg);
if (msg->mtype) {
int i;
for (i=0;i!=msg->mtype->nparameters;++i) {
if (msg->mtype->types[i]->release) {
msg->mtype->types[i]->release(msg->args[i]);
}
}
}
}
*/
2011-03-07 08:02:35 +01:00
int msg_set_resource(lua_message * msg, const char *param, const char *resname)
2010-08-08 10:06:34 +02:00
{
if (msg->mtype) {
int i = mtype_get_param(msg->mtype, param);
const resource_type * rtype;
2011-03-07 08:02:35 +01:00
if (i == msg->mtype->nparameters) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_NAME;
}
2011-03-07 08:02:35 +01:00
if (strcmp(msg->mtype->types[i]->name, "resource") != 0) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_TYPE;
}
rtype = rt_find(resname);
if (rtype) {
msg->args[i].v = (void *)rtype;
} else {
return E_INVALID_PARAMETER_VALUE;
}
2010-08-08 10:06:34 +02:00
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
int msg_set_unit(lua_message * msg, const char *param, const unit * u)
2010-08-08 10:06:34 +02:00
{
if (msg->mtype) {
int i = mtype_get_param(msg->mtype, param);
2011-03-07 08:02:35 +01:00
if (i == msg->mtype->nparameters) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_NAME;
}
2011-03-07 08:02:35 +01:00
if (strcmp(msg->mtype->types[i]->name, "unit") != 0) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_TYPE;
}
2011-03-07 08:02:35 +01:00
msg->args[i].v = (void *)u;
2010-08-08 10:06:34 +02:00
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
int msg_set_region(lua_message * msg, const char *param, const region * r)
2010-08-08 10:06:34 +02:00
{
if (msg->mtype) {
int i = mtype_get_param(msg->mtype, param);
2011-03-07 08:02:35 +01:00
if (i == msg->mtype->nparameters) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_NAME;
}
2011-03-07 08:02:35 +01:00
if (strcmp(msg->mtype->types[i]->name, "region") != 0) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_TYPE;
}
2011-03-07 08:02:35 +01:00
msg->args[i].v = (void *)r;
2010-08-08 10:06:34 +02:00
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
int msg_set_string(lua_message * msg, const char *param, const char *value)
2010-08-08 10:06:34 +02:00
{
if (msg->mtype) {
int i = mtype_get_param(msg->mtype, param);
variant var;
2011-03-07 08:02:35 +01:00
if (i == msg->mtype->nparameters) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_NAME;
}
2011-03-07 08:02:35 +01:00
if (strcmp(msg->mtype->types[i]->name, "string") != 0) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_TYPE;
}
2011-03-07 08:02:35 +01:00
var.v = (void *)value;
2010-08-08 10:06:34 +02:00
msg->args[i] = msg->mtype->types[i]->copy(var);
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
int msg_set_int(lua_message * msg, const char *param, int value)
2010-08-08 10:06:34 +02:00
{
if (msg->mtype) {
int i = mtype_get_param(msg->mtype, param);
2011-03-07 08:02:35 +01:00
if (i == msg->mtype->nparameters) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_NAME;
}
2011-03-07 08:02:35 +01:00
if (strcmp(msg->mtype->types[i]->name, "int") != 0) {
2010-08-08 10:06:34 +02:00
return E_INVALID_PARAMETER_TYPE;
}
msg->args[i].i = value;
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
int msg_send_faction(lua_message * msg, faction * f)
2010-08-08 10:06:34 +02:00
{
assert(f);
assert(msg);
if (msg->mtype) {
2011-03-07 08:02:35 +01:00
if (msg->msg == NULL) {
2010-08-08 10:06:34 +02:00
msg->msg = msg_create(msg->mtype, msg->args);
}
add_message(&f->msgs, msg->msg);
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
int msg_send_region(lua_message * lmsg, region * r)
2010-08-08 10:06:34 +02:00
{
if (lmsg->mtype) {
2011-03-07 08:02:35 +01:00
if (lmsg->msg == NULL) {
2010-08-08 10:06:34 +02:00
lmsg->msg = msg_create(lmsg->mtype, lmsg->args);
}
add_message(&r->msgs, lmsg->msg);
return E_OK;
}
return E_INVALID_MESSAGE;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_create(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
const char *type = tolua_tostring(L, 1, 0);
lua_message *lmsg = msg_create_message(type);
tolua_pushusertype(L, (void *)lmsg, TOLUA_CAST "message");
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_set_string(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
const char *param = tolua_tostring(L, 2, 0);
const char *value = tolua_tostring(L, 3, 0);
2010-08-08 10:06:34 +02:00
int result = msg_set_string(lmsg, param, value);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_set_int(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
const char *param = tolua_tostring(L, 2, 0);
2010-08-08 10:06:34 +02:00
int value = (int)tolua_tonumber(L, 3, 0);
int result = msg_set_int(lmsg, param, value);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_set_resource(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
const char *param = tolua_tostring(L, 2, 0);
const char *value = tolua_tostring(L, 3, 0);
2010-08-08 10:06:34 +02:00
int result = msg_set_resource(lmsg, param, value);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_set_unit(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
const char *param = tolua_tostring(L, 2, 0);
unit *value = (unit *) tolua_tousertype(L, 3, 0);
2010-08-08 10:06:34 +02:00
int result = msg_set_unit(lmsg, param, value);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_set_region(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
const char *param = tolua_tostring(L, 2, 0);
region *value = (region *) tolua_tousertype(L, 3, 0);
2010-08-08 10:06:34 +02:00
int result = msg_set_region(lmsg, param, value);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_set(lua_State * L)
2010-08-08 10:06:34 +02:00
{
tolua_Error err;
if (tolua_isnumber(L, 3, 0, &err)) {
return tolua_msg_set_int(L);
} else if (tolua_isusertype(L, 3, TOLUA_CAST "region", 0, &err)) {
return tolua_msg_set_region(L);
} else if (tolua_isusertype(L, 3, TOLUA_CAST "unit", 0, &err)) {
return tolua_msg_set_unit(L);
}
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) - 1);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_send_region(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
region *r = (region *) tolua_tousertype(L, 2, 0);
2010-08-08 10:06:34 +02:00
int result = msg_send_region(lmsg, r);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_report_action(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
region *r = (region *) tolua_tousertype(L, 2, 0);
unit *u = (unit *) tolua_tousertype(L, 3, 0);
2010-08-08 10:06:34 +02:00
int result, flags = (int)tolua_tonumber(L, 4, 0);
2011-03-07 08:02:35 +01:00
if (lmsg->msg == NULL) {
2010-08-08 10:06:34 +02:00
lmsg->msg = msg_create(lmsg->mtype, lmsg->args);
}
result = report_action(r, u, lmsg->msg, flags);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
2011-03-07 08:02:35 +01:00
static int tolua_msg_send_faction(lua_State * L)
2010-08-08 10:06:34 +02:00
{
2011-03-07 08:02:35 +01:00
lua_message *lmsg = (lua_message *) tolua_tousertype(L, 1, 0);
faction *f = (faction *) tolua_tousertype(L, 2, 0);
2010-08-08 10:06:34 +02:00
if (f && lmsg) {
int result = msg_send_faction(lmsg, f);
2011-03-07 08:02:35 +01:00
tolua_pushnumber(L, (lua_Number) result);
2010-08-08 10:06:34 +02:00
return 1;
}
return 0;
}
2011-03-07 08:02:35 +01:00
void tolua_message_open(lua_State * L)
2010-08-08 10:06:34 +02:00
{
/* register user types */
tolua_usertype(L, TOLUA_CAST "message");
tolua_module(L, NULL, 0);
tolua_beginmodule(L, NULL);
{
tolua_function(L, TOLUA_CAST "message", tolua_msg_create);
2011-03-07 08:02:35 +01:00
tolua_cclass(L, TOLUA_CAST "message", TOLUA_CAST "message", TOLUA_CAST "",
NULL);
2010-08-08 10:06:34 +02:00
tolua_beginmodule(L, TOLUA_CAST "message");
{
tolua_function(L, TOLUA_CAST "set", tolua_msg_set);
tolua_function(L, TOLUA_CAST "set_unit", tolua_msg_set_unit);
tolua_function(L, TOLUA_CAST "set_region", tolua_msg_set_region);
tolua_function(L, TOLUA_CAST "set_resource", tolua_msg_set_resource);
tolua_function(L, TOLUA_CAST "set_int", tolua_msg_set_int);
tolua_function(L, TOLUA_CAST "set_string", tolua_msg_set_string);
tolua_function(L, TOLUA_CAST "send_faction", tolua_msg_send_faction);
tolua_function(L, TOLUA_CAST "send_region", tolua_msg_send_region);
tolua_function(L, TOLUA_CAST "report_action", tolua_msg_report_action);
tolua_function(L, TOLUA_CAST "create", tolua_msg_create);
}
tolua_endmodule(L);
}
tolua_endmodule(L);
}