forked from github/server
solve helpers.c without bsdstring
This commit is contained in:
parent
5576ef37b6
commit
686bbdbf69
1 changed files with 72 additions and 76 deletions
148
src/helpers.c
148
src/helpers.c
|
@ -17,7 +17,6 @@ without prior permission by the authors of Eressea.
|
||||||
|
|
||||||
#include <util/attrib.h>
|
#include <util/attrib.h>
|
||||||
#include <util/base36.h>
|
#include <util/base36.h>
|
||||||
#include <util/bsdstring.h>
|
|
||||||
#include <util/event.h>
|
#include <util/event.h>
|
||||||
#include <util/functions.h>
|
#include <util/functions.h>
|
||||||
#include <util/gamedata.h>
|
#include <util/gamedata.h>
|
||||||
|
@ -52,66 +51,64 @@ lua_giveitem(unit * s, unit * d, const item_type * itype, int n, struct order *o
|
||||||
{
|
{
|
||||||
lua_State *L = (lua_State *)global.vm_state;
|
lua_State *L = (lua_State *)global.vm_state;
|
||||||
char fname[64];
|
char fname[64];
|
||||||
int result = -1;
|
int result = -1, len;
|
||||||
const char *iname = itype->rtype->_name;
|
const char *iname = itype->rtype->_name;
|
||||||
|
|
||||||
assert(s != NULL);
|
assert(s != NULL);
|
||||||
strlcpy(fname, iname, sizeof(fname));
|
len = snprintf(fname, sizeof(fname), "%s_give", iname);
|
||||||
strlcat(fname, "_give", sizeof(fname));
|
if (len > 0 && (size_t)len < sizeof(fname)) {
|
||||||
|
lua_getglobal(L, fname);
|
||||||
|
if (lua_isfunction(L, -1)) {
|
||||||
|
tolua_pushusertype(L, s, TOLUA_CAST "unit");
|
||||||
|
tolua_pushusertype(L, d, TOLUA_CAST "unit");
|
||||||
|
tolua_pushstring(L, iname);
|
||||||
|
lua_pushinteger(L, n);
|
||||||
|
|
||||||
lua_getglobal(L, fname);
|
if (lua_pcall(L, 4, 1, 0) != 0) {
|
||||||
if (lua_isfunction(L, -1)) {
|
const char *error = lua_tostring(L, -1);
|
||||||
tolua_pushusertype(L, s, TOLUA_CAST "unit");
|
log_error("unit %s calling '%s': %s.\n", unitname(s), fname, error);
|
||||||
tolua_pushusertype(L, d, TOLUA_CAST "unit");
|
lua_pop(L, 1);
|
||||||
tolua_pushstring(L, iname);
|
}
|
||||||
lua_pushinteger(L, n);
|
else {
|
||||||
|
result = (int)lua_tonumber(L, -1);
|
||||||
if (lua_pcall(L, 4, 1, 0) != 0) {
|
lua_pop(L, 1);
|
||||||
const char *error = lua_tostring(L, -1);
|
}
|
||||||
log_error("unit %s calling '%s': %s.\n", unitname(s), fname, error);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = (int)lua_tonumber(L, -1);
|
log_error("unit %s trying to call '%s' : not a function.\n", unitname(s), fname);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
log_error("unit %s trying to call '%s' : not a function.\n", unitname(s), fname);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int limit_resource_lua(const region * r, const resource_type * rtype)
|
static int limit_resource_lua(const region * r, const resource_type * rtype)
|
||||||
{
|
{
|
||||||
char fname[64];
|
char fname[64];
|
||||||
int result = -1;
|
int result = -1, len;
|
||||||
lua_State *L = (lua_State *)global.vm_state;
|
lua_State *L = (lua_State *)global.vm_state;
|
||||||
|
|
||||||
strlcpy(fname, rtype->_name, sizeof(fname));
|
len = snprintf(fname, sizeof(fname), "%s_limit", rtype->_name);
|
||||||
strlcat(fname, "_limit", sizeof(fname));
|
if (len > 0 && (size_t)len < sizeof(fname)) {
|
||||||
|
lua_getglobal(L, fname);
|
||||||
|
if (lua_isfunction(L, -1)) {
|
||||||
|
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region");
|
||||||
|
|
||||||
lua_getglobal(L, fname);
|
if (lua_pcall(L, 1, 1, 0) != 0) {
|
||||||
if (lua_isfunction(L, -1)) {
|
const char *error = lua_tostring(L, -1);
|
||||||
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region");
|
log_error("limit(%s) calling '%s': %s.\n", regionname(r, NULL), fname, error);
|
||||||
|
lua_pop(L, 1);
|
||||||
if (lua_pcall(L, 1, 1, 0) != 0) {
|
}
|
||||||
const char *error = lua_tostring(L, -1);
|
else {
|
||||||
log_error("limit(%s) calling '%s': %s.\n", regionname(r, NULL), fname, error);
|
result = (int)lua_tonumber(L, -1);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = (int)lua_tonumber(L, -1);
|
log_error("limit(%s) calling '%s': not a function.\n", regionname(r, NULL), fname);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
log_error("limit(%s) calling '%s': not a function.\n", regionname(r, NULL), fname);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,25 +117,26 @@ produce_resource_lua(region * r, const resource_type * rtype, int norders)
|
||||||
{
|
{
|
||||||
lua_State *L = (lua_State *)global.vm_state;
|
lua_State *L = (lua_State *)global.vm_state;
|
||||||
char fname[64];
|
char fname[64];
|
||||||
|
int len;
|
||||||
|
|
||||||
strlcpy(fname, rtype->_name, sizeof(fname));
|
len = snprintf(fname, sizeof(fname), "%s_produce", rtype->_name);
|
||||||
strlcat(fname, "_produce", sizeof(fname));
|
if (len > 0 && (size_t)len < sizeof(fname)) {
|
||||||
|
lua_getglobal(L, fname);
|
||||||
|
if (lua_isfunction(L, -1)) {
|
||||||
|
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region");
|
||||||
|
lua_pushinteger(L, norders);
|
||||||
|
|
||||||
lua_getglobal(L, fname);
|
if (lua_pcall(L, 2, 0, 0) != 0) {
|
||||||
if (lua_isfunction(L, -1)) {
|
const char *error = lua_tostring(L, -1);
|
||||||
tolua_pushusertype(L, (void *)r, TOLUA_CAST "region");
|
log_error("produce(%s) calling '%s': %s.\n", regionname(r, NULL), fname, error);
|
||||||
lua_pushinteger(L, norders);
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
if (lua_pcall(L, 2, 0, 0) != 0) {
|
}
|
||||||
const char *error = lua_tostring(L, -1);
|
else {
|
||||||
log_error("produce(%s) calling '%s': %s.\n", regionname(r, NULL), fname, error);
|
log_error("produce(%s) calling '%s': not a function.\n", regionname(r, NULL), fname);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
log_error("produce(%s) calling '%s': not a function.\n", regionname(r, NULL), fname);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void push_param(lua_State * L, char c, spllprm * param)
|
static void push_param(lua_State * L, char c, spllprm * param)
|
||||||
|
@ -216,32 +214,31 @@ static int
|
||||||
lua_changeresource(unit * u, const struct resource_type *rtype, int delta)
|
lua_changeresource(unit * u, const struct resource_type *rtype, int delta)
|
||||||
{
|
{
|
||||||
lua_State *L = (lua_State *)global.vm_state;
|
lua_State *L = (lua_State *)global.vm_state;
|
||||||
int result = -1;
|
int len, result = -1;
|
||||||
char fname[64];
|
char fname[64];
|
||||||
|
|
||||||
strlcpy(fname, rtype->_name, sizeof(fname));
|
len = snprintf(fname, sizeof(fname), "%s_changeresource", rtype->_name);
|
||||||
strlcat(fname, "_changeresource", sizeof(fname));
|
if (len > 0 && (size_t)len < sizeof(fname)) {
|
||||||
|
lua_getglobal(L, fname);
|
||||||
lua_getglobal(L, fname);
|
if (lua_isfunction(L, -1)) {
|
||||||
if (lua_isfunction(L, -1)) {
|
tolua_pushusertype(L, u, TOLUA_CAST "unit");
|
||||||
tolua_pushusertype(L, u, TOLUA_CAST "unit");
|
lua_pushinteger(L, delta);
|
||||||
lua_pushinteger(L, delta);
|
|
||||||
|
if (lua_pcall(L, 2, 1, 0) != 0) {
|
||||||
if (lua_pcall(L, 2, 1, 0) != 0) {
|
const char *error = lua_tostring(L, -1);
|
||||||
const char *error = lua_tostring(L, -1);
|
log_error("change(%s) calling '%s': %s.\n", unitname(u), fname, error);
|
||||||
log_error("change(%s) calling '%s': %s.\n", unitname(u), fname, error);
|
lua_pop(L, 1);
|
||||||
lua_pop(L, 1);
|
}
|
||||||
|
else {
|
||||||
|
result = (int)lua_tonumber(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result = (int)lua_tonumber(L, -1);
|
log_error("change(%s) calling '%s': not a function.\n", unitname(u), fname);
|
||||||
lua_pop(L, 1);
|
lua_pop(L, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
log_error("change(%s) calling '%s': not a function.\n", unitname(u), fname);
|
|
||||||
lua_pop(L, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,13 +247,12 @@ static int
|
||||||
use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord)
|
use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord)
|
||||||
{
|
{
|
||||||
lua_State *L = (lua_State *)global.vm_state;
|
lua_State *L = (lua_State *)global.vm_state;
|
||||||
int result = 0;
|
int len, result = 0;
|
||||||
char fname[64];
|
char fname[64];
|
||||||
int (*callout)(unit *, const item_type *, int, struct order *);
|
int (*callout)(unit *, const item_type *, int, struct order *);
|
||||||
|
|
||||||
strlcpy(fname, "use_", sizeof(fname));
|
len = snprintf(fname, sizeof(fname), "use_%s", itype->rtype->_name);
|
||||||
strlcat(fname, itype->rtype->_name, sizeof(fname));
|
if (len > 0 && (size_t)len < sizeof(fname)) {
|
||||||
|
|
||||||
callout = (int(*)(unit *, const item_type *, int, struct order *))get_function(fname);
|
callout = (int(*)(unit *, const item_type *, int, struct order *))get_function(fname);
|
||||||
if (callout) {
|
if (callout) {
|
||||||
return callout(u, itype, amount, ord);
|
return callout(u, itype, amount, ord);
|
||||||
|
@ -286,7 +282,7 @@ use_item_lua(unit *u, const item_type *itype, int amount, struct order *ord)
|
||||||
log_error("no such callout: %s", fname);
|
log_error("no such callout: %s", fname);
|
||||||
}
|
}
|
||||||
log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname);
|
log_error("use(%s) calling '%s': not a function.\n", unitname(u), fname);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue