add or remove a lua function callback */ callback("level_enter", "function_name"); */ callback("level_enter", "function_name", true); */ level_enter, level_leave, cmd_before */
| 1661 | /* callback("level_enter", "function_name", true); */ |
| 1662 | /* level_enter, level_leave, cmd_before */ |
| 1663 | staticfn int |
| 1664 | nhl_callback(lua_State *L) |
| 1665 | { |
| 1666 | int argc = lua_gettop(L); |
| 1667 | int i; |
| 1668 | boolean rm; |
| 1669 | const char *fn, *cb; |
| 1670 | |
| 1671 | if (!gl.luacore) { |
| 1672 | panic("nh luacore not inited"); |
| 1673 | /*NOTREACHED*/ |
| 1674 | return 0; |
| 1675 | } |
| 1676 | if (argc == 2 || argc == 3) { |
| 1677 | if (argc == 2) { |
| 1678 | rm = FALSE; |
| 1679 | fn = luaL_checkstring(L, -1); |
| 1680 | cb = luaL_checkstring(L, -2); |
| 1681 | } else { |
| 1682 | rm = lua_toboolean(L, -1); |
| 1683 | fn = luaL_checkstring(L, -2); |
| 1684 | cb = luaL_checkstring(L, -3); |
| 1685 | } |
| 1686 | for (i = 0; i < NUM_NHCB; i++) |
| 1687 | if (!strcmp(cb, nhcb_name[i])) |
| 1688 | break; |
| 1689 | if (i >= NUM_NHCB) |
| 1690 | return 0; |
| 1691 | |
| 1692 | if (rm) { |
| 1693 | nhcb_counts[i]--; |
| 1694 | if (nhcb_counts[i] < 0) |
| 1695 | impossible("nh.callback counts are wrong"); |
| 1696 | } else { |
| 1697 | nhcb_counts[i]++; |
| 1698 | } |
| 1699 | |
| 1700 | lua_getglobal(gl.luacore, rm ? "nh_callback_rm" : "nh_callback_set"); |
| 1701 | lua_pushstring(gl.luacore, cb); |
| 1702 | lua_pushstring(gl.luacore, fn); |
| 1703 | nhl_pcall_handle(gl.luacore, 2, 0, "nhl_callback", NHLpa_panic); |
| 1704 | } |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
| 1708 | /* store or restore game state */ |
| 1709 | /* |
nothing calls this directly
no test coverage detected