From 30beda4d0f59aa37ee9d33c67d52aba686d53978 Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Tue, 20 Oct 2009 21:31:08 +0200 Subject: Move library "options" from clua.cc to l_option.cc. --- crawl-ref/source/clua.cc | 137 +---------------------------------------- crawl-ref/source/l_libs.h | 1 + crawl-ref/source/l_option.cc | 139 ++++++++++++++++++++++++++++++++++++++++++ crawl-ref/source/makefile.obj | 1 + 4 files changed, 142 insertions(+), 136 deletions(-) create mode 100644 crawl-ref/source/l_option.cc diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index 1c2ebcfeb7..92212e37e6 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -594,7 +594,6 @@ extern void luaopen_kills(lua_State *ls); void luaopen_item(lua_State *ls); void luaopen_food(lua_State *ls); void luaopen_file(lua_State *ls); -void luaopen_options(lua_State *ls); void luaopen_globals(lua_State *ls); void CLua::init_lua() @@ -622,7 +621,7 @@ void CLua::init_lua() luaopen_food(_state); cluaopen_crawl(_state); luaopen_file(_state); - luaopen_options(_state); + cluaopen_options(_state); cluaopen_monsters(_state); luaopen_globals(_state); @@ -1572,140 +1571,6 @@ void luaopen_file(lua_State *ls) } -//////////////////////////////////////////////////////////////// -// Option handling - -typedef int (*ohandler)(lua_State *ls, const char *name, void *data, bool get); -struct option_handler -{ - const char *option; - void *data; - ohandler handler; -}; - -static int option_hboolean(lua_State *ls, const char *name, void *data, - bool get) -{ - if (get) - { - lua_pushboolean(ls, *static_cast( data )); - return (1); - } - else - { - if (lua_isboolean(ls, 3)) - *static_cast( data ) = lua_toboolean(ls, 3); - return (0); - } -} - -static option_handler handlers[] = -{ - // Boolean options come first - { "easy_open", &Options.easy_open, option_hboolean }, - { "colour_map", &Options.colour_map, option_hboolean }, - { "clean_map", &Options.clean_map, option_hboolean }, - { "show_uncursed", &Options.show_uncursed, option_hboolean }, - { "easy_open", &Options.easy_open, option_hboolean }, - { "easy_armour", &Options.easy_unequip, option_hboolean }, - { "easy_unequip", &Options.easy_unequip, option_hboolean }, - { "easy_butcher", &Options.easy_butcher, option_hboolean }, - { "always_confirm_butcher", &Options.always_confirm_butcher, option_hboolean }, - { "default_target", &Options.default_target, option_hboolean }, - { "autopickup_no_burden", &Options.autopickup_no_burden, option_hboolean }, - { "note_skill_max", &Options.note_skill_max, option_hboolean }, - { "delay_message_clear", &Options.delay_message_clear, option_hboolean }, - { "no_dark_brand", &Options.no_dark_brand, option_hboolean }, - { "auto_list", &Options.auto_list, option_hboolean }, - { "pickup_thrown", &Options.pickup_thrown, option_hboolean }, - { "pickup_dropped", &Options.pickup_dropped, option_hboolean }, - { "show_waypoints", &Options.show_waypoints, option_hboolean }, - { "item_colour", &Options.item_colour, option_hboolean }, - { "target_zero_exp", &Options.target_zero_exp, option_hboolean }, - { "target_wrap", &Options.target_wrap, option_hboolean }, - { "easy_exit_menu", &Options.easy_exit_menu, option_hboolean }, - { "dos_use_background_intensity", &Options.dos_use_background_intensity, - option_hboolean }, - { "menu_colour_prefix_class", &Options.menu_colour_prefix_class, - option_hboolean } -}; - -static const option_handler *get_handler(const char *optname) -{ - if (optname) - { - for (int i = 0, count = sizeof(handlers) / sizeof(*handlers); - i < count; ++i) - { - if (!strcmp(handlers[i].option, optname)) - return &handlers[i]; - } - } - return (NULL); -} - -static int option_get(lua_State *ls) -{ - const char *opt = luaL_checkstring(ls, 2); - if (!opt) - return (0); - - // Is this a Lua named option? - game_options::opt_map::iterator i = Options.named_options.find(opt); - if (i != Options.named_options.end()) - { - const std::string &ov = i->second; - lua_pushstring(ls, ov.c_str()); - return (1); - } - - const option_handler *oh = get_handler(opt); - if (oh) - return (oh->handler(ls, opt, oh->data, true)); - - return (0); -} - -static int option_set(lua_State *ls) -{ - const char *opt = luaL_checkstring(ls, 2); - if (!opt) - return (0); - - const option_handler *oh = get_handler(opt); - if (oh) - oh->handler(ls, opt, oh->data, false); - - return (0); -} - -#define OPT_METATABLE "clua_metatable_optaccess" -void luaopen_options(lua_State *ls) -{ - int top = lua_gettop(ls); - - luaL_newmetatable(ls, OPT_METATABLE); - lua_pushstring(ls, "__index"); - lua_pushcfunction(ls, option_get); - lua_settable(ls, -3); - - luaL_getmetatable(ls, OPT_METATABLE); - lua_pushstring(ls, "__newindex"); - lua_pushcfunction(ls, option_set); - lua_settable(ls, -3); - - lua_settop(ls, top); - - // Create dummy userdata to front for our metatable - int *dummy = static_cast( lua_newuserdata(ls, sizeof(int)) ); - // Mystic number - *dummy = 42; - - luaL_getmetatable(ls, OPT_METATABLE); - lua_setmetatable(ls, -2); - lua_setglobal(ls, "options"); -} - // Pushing various objects. static int push_activity_interrupt(lua_State *ls, activity_interrupt_data *t) diff --git a/crawl-ref/source/l_libs.h b/crawl-ref/source/l_libs.h index 4b8476c235..178f1f5201 100644 --- a/crawl-ref/source/l_libs.h +++ b/crawl-ref/source/l_libs.h @@ -14,6 +14,7 @@ void cluaopen_crawl(lua_State *ls); void cluaopen_monsters(lua_State *ls); +void cluaopen_options(lua_State *ls); void cluaopen_you(lua_State *ls); /* diff --git a/crawl-ref/source/l_option.cc b/crawl-ref/source/l_option.cc new file mode 100644 index 0000000000..610f5ebe4c --- /dev/null +++ b/crawl-ref/source/l_option.cc @@ -0,0 +1,139 @@ +#include "AppHdr.h" + +#include "clua.h" +#include "l_libs.h" + +//////////////////////////////////////////////////////////////// +// Option handling + +typedef int (*ohandler)(lua_State *ls, const char *name, void *data, bool get); +struct option_handler +{ + const char *option; + void *data; + ohandler handler; +}; + +static int option_hboolean(lua_State *ls, const char *name, void *data, + bool get) +{ + if (get) + { + lua_pushboolean(ls, *static_cast( data )); + return (1); + } + else + { + if (lua_isboolean(ls, 3)) + *static_cast( data ) = lua_toboolean(ls, 3); + return (0); + } +} + +static option_handler handlers[] = +{ + // Boolean options come first + { "easy_open", &Options.easy_open, option_hboolean }, + { "colour_map", &Options.colour_map, option_hboolean }, + { "clean_map", &Options.clean_map, option_hboolean }, + { "show_uncursed", &Options.show_uncursed, option_hboolean }, + { "easy_open", &Options.easy_open, option_hboolean }, + { "easy_armour", &Options.easy_unequip, option_hboolean }, + { "easy_unequip", &Options.easy_unequip, option_hboolean }, + { "easy_butcher", &Options.easy_butcher, option_hboolean }, + { "always_confirm_butcher", &Options.always_confirm_butcher, option_hboolean }, + { "default_target", &Options.default_target, option_hboolean }, + { "autopickup_no_burden", &Options.autopickup_no_burden, option_hboolean }, + { "note_skill_max", &Options.note_skill_max, option_hboolean }, + { "delay_message_clear", &Options.delay_message_clear, option_hboolean }, + { "no_dark_brand", &Options.no_dark_brand, option_hboolean }, + { "auto_list", &Options.auto_list, option_hboolean }, + { "pickup_thrown", &Options.pickup_thrown, option_hboolean }, + { "pickup_dropped", &Options.pickup_dropped, option_hboolean }, + { "show_waypoints", &Options.show_waypoints, option_hboolean }, + { "item_colour", &Options.item_colour, option_hboolean }, + { "target_zero_exp", &Options.target_zero_exp, option_hboolean }, + { "target_wrap", &Options.target_wrap, option_hboolean }, + { "easy_exit_menu", &Options.easy_exit_menu, option_hboolean }, + { "dos_use_background_intensity", &Options.dos_use_background_intensity, + option_hboolean }, + { "menu_colour_prefix_class", &Options.menu_colour_prefix_class, + option_hboolean } +}; + +static const option_handler *get_handler(const char *optname) +{ + if (optname) + { + for (int i = 0, count = sizeof(handlers) / sizeof(*handlers); + i < count; ++i) + { + if (!strcmp(handlers[i].option, optname)) + return &handlers[i]; + } + } + return (NULL); +} + +static int option_get(lua_State *ls) +{ + const char *opt = luaL_checkstring(ls, 2); + if (!opt) + return (0); + + // Is this a Lua named option? + game_options::opt_map::iterator i = Options.named_options.find(opt); + if (i != Options.named_options.end()) + { + const std::string &ov = i->second; + lua_pushstring(ls, ov.c_str()); + return (1); + } + + const option_handler *oh = get_handler(opt); + if (oh) + return (oh->handler(ls, opt, oh->data, true)); + + return (0); +} + +static int option_set(lua_State *ls) +{ + const char *opt = luaL_checkstring(ls, 2); + if (!opt) + return (0); + + const option_handler *oh = get_handler(opt); + if (oh) + oh->handler(ls, opt, oh->data, false); + + return (0); +} + +#define OPT_METATABLE "clua_metatable_optaccess" + +void cluaopen_options(lua_State *ls) +{ + int top = lua_gettop(ls); + + luaL_newmetatable(ls, OPT_METATABLE); + lua_pushstring(ls, "__index"); + lua_pushcfunction(ls, option_get); + lua_settable(ls, -3); + + luaL_getmetatable(ls, OPT_METATABLE); + lua_pushstring(ls, "__newindex"); + lua_pushcfunction(ls, option_set); + lua_settable(ls, -3); + + lua_settop(ls, top); + + // Create dummy userdata to front for our metatable + int *dummy = static_cast( lua_newuserdata(ls, sizeof(int)) ); + // Mystic number + *dummy = 42; + + luaL_getmetatable(ls, OPT_METATABLE); + lua_setmetatable(ls, -2); + lua_setglobal(ls, "options"); +} diff --git a/crawl-ref/source/makefile.obj b/crawl-ref/source/makefile.obj index 8b0c7412ab..ff593408c8 100644 --- a/crawl-ref/source/makefile.obj +++ b/crawl-ref/source/makefile.obj @@ -54,6 +54,7 @@ l_los.o \ l_mapgrd.o \ l_mapmrk.o \ l_mons.o \ +l_option.o \ l_you.o \ los.o \ losparam.o \ -- cgit v1.2.3-54-g00ecf