| 1 |
/* |
| 2 |
* Copyright (C) 2004 Ivo Danihelka ([email protected]) |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
*/ |
| 9 |
#include "options-script.h" |
| 10 |
|
| 11 |
#include "OptionAgent.h" |
| 12 |
#include "MessagerAgent.h" |
| 13 |
#include "StringMsg.h" |
| 14 |
#include "IntMsg.h" |
| 15 |
#include "SimpleMsg.h" |
| 16 |
#include "def-script.h" |
| 17 |
|
| 18 |
|
| 19 |
//----------------------------------------------------------------- |
| 20 |
/** |
| 21 |
* void sendMsg(listener, msg, [value]) |
| 22 |
*/ |
| 23 |
int |
| 24 |
script_options_sendMsg(lua_State *L) throw() |
| 25 |
{ |
| 26 |
BEGIN_NOEXCEPTION; |
| 27 |
BaseMsg *message = NULL; |
| 28 |
const char *listener = luaL_checkstring(L, 1); |
| 29 |
const char *msg = luaL_checkstring(L, 2); |
| 30 |
if (lua_isstring(L, 3)) { |
| 31 |
const char *string_value = luaL_checkstring(L, 3); |
| 32 |
message = new StringMsg(listener, msg, string_value); |
| 33 |
} |
| 34 |
else if (lua_isnumber(L, 3)) { |
| 35 |
int int_value = luaL_checkint(L, 3); |
| 36 |
message = new IntMsg(listener, msg, int_value); |
| 37 |
} |
| 38 |
else { |
| 39 |
message = new SimpleMsg(listener, msg); |
| 40 |
} |
| 41 |
|
| 42 |
MessagerAgent::agent()->forwardNewMsg(message); |
| 43 |
|
| 44 |
END_NOEXCEPTION; |
| 45 |
return 0; |
| 46 |
} |
| 47 |
//----------------------------------------------------------------- |
| 48 |
/** |
| 49 |
* void setParam(name, value) |
| 50 |
*/ |
| 51 |
int |
| 52 |
script_options_setParam(lua_State *L) throw() |
| 53 |
{ |
| 54 |
BEGIN_NOEXCEPTION; |
| 55 |
const char *name = luaL_checkstring(L, 1); |
| 56 |
const char *value = lua_tostring(L, 2); |
| 57 |
if (!value) { |
| 58 |
value = luaL_checkstring(L, 2); |
| 59 |
} |
| 60 |
OptionAgent::agent()->setParam(name, value); |
| 61 |
END_NOEXCEPTION; |
| 62 |
return 0; |
| 63 |
} |
| 64 |
//----------------------------------------------------------------- |
| 65 |
/** |
| 66 |
* string getParam(name) |
| 67 |
* Returns string or nil. |
| 68 |
*/ |
| 69 |
int |
| 70 |
script_options_getParam(lua_State *L) throw() |
| 71 |
{ |
| 72 |
BEGIN_NOEXCEPTION; |
| 73 |
const char *name = luaL_checkstring(L, 1); |
| 74 |
std::string value = OptionAgent::agent()->getParam(name); |
| 75 |
if (value.empty()) { |
| 76 |
lua_pushnil(L); |
| 77 |
} |
| 78 |
else { |
| 79 |
lua_pushlstring(L, value.c_str(), value.size()); |
| 80 |
} |
| 81 |
END_NOEXCEPTION; |
| 82 |
//NOTE: return value |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|