/* TODO - optional ptag argument? */ static int lnet_get_ipv4 (lua_State *L) { libnet_t* ud = checkudata(L); libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_IPV4_H); struct libnet_ipv4_hdr* ip = (struct libnet_ipv4_hdr*) pblock->buf; libnet_pblock_t* oblock = pblock->prev; if(oblock && oblock->type != LIBNET_PBLOCK_IPO_H) { oblock = NULL; } lua_newtable(L); setintfield(L, 2, "ptag", pblock->ptag); setintfield(L, 2, "_iphl", ip->ip_hl); setintfield(L, 2, "_ipv", ip->ip_v); setintfield(L, 2, "tos", ip->ip_tos); setintfield(L, 2, "_len", ntohs(ip->ip_len)); setintfield(L, 2, "id", ntohs(ip->ip_id)); setintfield(L, 2, "frag", ntohs(ip->ip_off)); /* FIXME Only if non-zero, or flags are set: setintfield(L, 2, "_fragoffset", ntohs(ip->ip_off) & 0xd0); Only if non-zero: setintfield(L, 2, "_fragflags", ntohs(ip->ip_off) & 0x1f); , 2, "_fragmore", ; , 2, "_fragdont", ; , 2, "_fragrsv", ; */ setintfield(L, 2, "ttl", ip->ip_ttl); setintfield(L, 2, "protocol", ip->ip_p); setintfield(L, 2, "_sum", ntohs(ip->ip_sum)); setipfield(L, 2, "src", ip->ip_src); setipfield(L, 2, "dst", ip->ip_dst); if(oblock) { lua_pushlstring(L, (const char*)oblock->buf, (size_t)oblock->b_len); } else { lua_pushstring(L, ""); } lua_setfield(L, 2, "options"); /* TODO If we need to manipulate IPV4 payload, then we'll need to encode the blocks following the options, and then set and _payload field. Unless the payload was PBLOCK_IPDATA instead of some specific pblock, in which case it's easy to get it. Also, we'll need to change either libnet_build_ipv4() or net:ipv4{} so that when payload is specified, it destroys any of the preceeding pblocks that would normally be used to form the payload. */ /* FIXME - at least deal with IPDATA, as get_tcp does. */ return 1; }
// get_craft_recipe(result item) int ModApiCraft::l_get_craft_recipe(lua_State *L) { NO_MAP_LOCK_REQUIRED; int k = 1; int input_i = 1; std::string o_item = luaL_checkstring(L,input_i); IGameDef *gdef = getServer(L); ICraftDefManager *cdef = gdef->cdef(); CraftInput input; CraftOutput output(o_item,0); bool got = cdef->getCraftRecipe(input, output, gdef); lua_newtable(L); // output table if(got){ lua_newtable(L); for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++, k++) { if (i->empty()) { continue; } lua_pushinteger(L,k); lua_pushstring(L,i->name.c_str()); lua_settable(L, -3); } lua_setfield(L, -2, "items"); setintfield(L, -1, "width", input.width); switch (input.method) { case CRAFT_METHOD_NORMAL: lua_pushstring(L,"normal"); break; case CRAFT_METHOD_COOKING: lua_pushstring(L,"cooking"); break; case CRAFT_METHOD_FUEL: lua_pushstring(L,"fuel"); break; default: lua_pushstring(L,"unknown"); } lua_setfield(L, -2, "type"); } else { lua_pushnil(L); lua_setfield(L, -2, "items"); setintfield(L, -1, "width", 0); } return 1; }
/* local stats = jit.util.stats(func) */ static int ju_stats(lua_State *L) { if (!(L->top > L->base)) luaL_argerror(L, 1, "Lua function expected"); if (isLfunction(L->base)) { Proto *pt = clvalue(L->base)->l.p; lua_createtable(L, 0, 11); setintfield("status", pt->jit_status); setintfield("stackslots", pt->maxstacksize); setintfield("params", pt->numparams); setintfield("bytecodes", pt->sizecode); setintfield("consts", pt->sizek); setintfield("upvalues", pt->nups); setintfield("subs", pt->sizep); lua_pushboolean(L, pt->is_vararg); lua_setfield(L, -2, "isvararg"); lua_getfenv(L, 1); lua_setfield(L, -2, "env"); if (pt->jit_szmcode != 0) { setintfield("mcodesize", (int)mcodesize(pt)); lua_pushnumber(L, (lua_Number)(size_t)pt->jit_mcode); lua_setfield(L, -2, "mcodeaddr"); } return 1; } else { return 0; /* Don't throw an error like the other util functions. */ } }
// get_craft_result(input) int ModApiCraft::l_get_craft_result(lua_State *L) { NO_MAP_LOCK_REQUIRED; int input_i = 1; std::string method_s = getstringfield_default(L, input_i, "method", "normal"); enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method", es_CraftMethod, CRAFT_METHOD_NORMAL); int width = 1; lua_getfield(L, input_i, "width"); if(lua_isnumber(L, -1)) width = luaL_checkinteger(L, -1); lua_pop(L, 1); lua_getfield(L, input_i, "items"); std::vector<ItemStack> items = read_items(L, -1,getServer(L)); lua_pop(L, 1); // items IGameDef *gdef = getServer(L); ICraftDefManager *cdef = gdef->cdef(); CraftInput input(method, width, items); CraftOutput output; std::vector<ItemStack> output_replacements; bool got = cdef->getCraftResult(input, output, output_replacements, true, gdef); lua_newtable(L); // output table if (got) { ItemStack item; item.deSerialize(output.item, gdef->idef()); LuaItemStack::create(L, item); lua_setfield(L, -2, "item"); setintfield(L, -1, "time", output.time); push_items(L, output_replacements); lua_setfield(L, -2, "replacements"); } else { LuaItemStack::create(L, ItemStack()); lua_setfield(L, -2, "item"); setintfield(L, -1, "time", 0); lua_newtable(L); lua_setfield(L, -2, "replacements"); } lua_newtable(L); // decremented input table lua_pushstring(L, method_s.c_str()); lua_setfield(L, -2, "method"); lua_pushinteger(L, width); lua_setfield(L, -2, "width"); push_items(L, input.items); lua_setfield(L, -2, "items"); return 2; }
static void set_dig_params(lua_State *L, int table, const DigParams ¶ms) { setboolfield(L, table, "diggable", params.diggable); setfloatfield(L, table, "time", params.time); setintfield(L, table, "wear", params.wear); }
void push_dig_params(lua_State *L,const DigParams ¶ms) { lua_newtable(L); setboolfield(L, -1, "diggable", params.diggable); setfloatfield(L, -1, "time", params.time); setintfield(L, -1, "wear", params.wear); }
static void push_craft_recipe(lua_State *L, IGameDef *gdef, const CraftDefinition *recipe, const CraftOutput &tmpout) { CraftInput input = recipe->getInput(tmpout, gdef); CraftOutput output = recipe->getOutput(input, gdef); lua_newtable(L); // items std::vector<ItemStack>::const_iterator iter = input.items.begin(); for (u16 j = 1; iter != input.items.end(); iter++, j++) { if (iter->empty()) continue; lua_pushstring(L, iter->name.c_str()); lua_rawseti(L, -2, j); } lua_setfield(L, -2, "items"); setintfield(L, -1, "width", input.width); switch (input.method) { case CRAFT_METHOD_NORMAL: lua_pushstring(L, "normal"); break; case CRAFT_METHOD_COOKING: lua_pushstring(L, "cooking"); break; case CRAFT_METHOD_FUEL: lua_pushstring(L, "fuel"); break; default: lua_pushstring(L, "unknown"); } lua_setfield(L, -2, "type"); lua_pushstring(L, output.item.c_str()); lua_setfield(L, -2, "output"); }
/* ** Open JIT library */ LUALIB_API int luaopen_jit(lua_State *L) { /* Add the core JIT library. */ luaL_register(L, LUA_JITLIBNAME, jitlib); lua_pushliteral(L, LUAJIT_VERSION); lua_setfield(L, -2, "version"); setintfield("version_num", LUAJIT_VERSION_NUM); lua_pushstring(L, luaJIT_arch); lua_setfield(L, -2, "arch"); makepipeline(L); /* Add the utility JIT library. */ luaL_register(L, LUA_JITLIBNAME ".util", jitutillib); makestatus(L, "status"); makehints(L, hints_H, JIT_H_MAX, "hints"); makehints(L, hints_FH, JIT_FH_MAX, "fhints"); lua_pop(L, 1); /* Everything ok, so turn the JIT engine on. Vroooom! */ if (luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|LUAJIT_MODE_ON) <= 0) { /* Ouch. Someone screwed up DynASM or the JSUBs. Probably me. */ /* But if you get 999999999, look at jit_consistency_check(). */ return luaL_error(L, "JIT engine init failed (%d)", G(L)->jit_state->dasmstatus); } return 1; }
/*- -- argt = net:get_eth() Get eth pblock argument table. */ static int lnet_get_eth (lua_State *L) { libnet_t* ud = checkudata(L); libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_ETH_H); struct libnet_ethernet_hdr* hdr = (struct libnet_ethernet_hdr*) pblock->buf; lua_newtable(L); setintfield(L, 2, "ptag", pblock->ptag); setethfield(L, 2, "src", hdr->ether_shost); setethfield(L, 2, "dst", hdr->ether_dhost); setnsintfield(L, 2, "type", hdr->ether_type); if(!pblock->prev) { /* direct payload */ uint8_t* data = pblock->buf + LIBNET_ETH_H; uint32_t datasz = 0; if(pblock->b_len > LIBNET_ETH_H) { datasz = pblock->b_len - LIBNET_ETH_H; } setlstringfield(L, 2, "payload", data, (size_t)datasz); } return 1; }
// get_all_craft_recipes(result item) int ModApiCraft::l_get_all_craft_recipes(lua_State *L) { NO_MAP_LOCK_REQUIRED; std::string o_item = luaL_checkstring(L,1); IGameDef *gdef = getServer(L); ICraftDefManager *cdef = gdef->cdef(); CraftInput input; CraftOutput output(o_item,0); std::vector<CraftDefinition*> recipes_list; recipes_list = cdef->getCraftRecipes(output, gdef); if (recipes_list.empty()) { lua_pushnil(L); return 1; } lua_createtable(L, recipes_list.size(), 0); std::vector<CraftDefinition*>::const_iterator iter = recipes_list.begin(); for (u16 i = 0; iter != recipes_list.end(); iter++) { CraftOutput tmpout; tmpout.item = ""; tmpout.time = 0; tmpout = (*iter)->getOutput(input, gdef); std::string query = tmpout.item; char *fmtpos, *fmt = &query[0]; if (strtok_r(fmt, " ", &fmtpos) == output.item) { input = (*iter)->getInput(output, gdef); lua_newtable(L); lua_newtable(L); // items std::vector<ItemStack>::const_iterator iter = input.items.begin(); for (u16 j = 0; iter != input.items.end(); iter++) { if (iter->empty()) continue; lua_pushstring(L, iter->name.c_str()); lua_rawseti(L, -2, ++j); } lua_setfield(L, -2, "items"); setintfield(L, -1, "width", input.width); switch (input.method) { case CRAFT_METHOD_NORMAL: lua_pushstring(L, "normal"); break; case CRAFT_METHOD_COOKING: lua_pushstring(L, "cooking"); break; case CRAFT_METHOD_FUEL: lua_pushstring(L, "fuel"); break; default: lua_pushstring(L, "unknown"); } lua_setfield(L, -2, "type"); lua_pushstring(L, &tmpout.item[0]); lua_setfield(L, -2, "output"); lua_rawseti(L, -2, ++i); } } return 1; }
void push_tool_capabilities(lua_State *L, const ToolCapabilities &toolcap) { lua_newtable(L); setfloatfield(L, -1, "full_punch_interval", toolcap.full_punch_interval); setintfield(L, -1, "max_drop_level", toolcap.max_drop_level); // Create groupcaps table lua_newtable(L); // For each groupcap for(std::map<std::string, ToolGroupCap>::const_iterator i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){ // Create groupcap table lua_newtable(L); const std::string &name = i->first; const ToolGroupCap &groupcap = i->second; // Create subtable "times" lua_newtable(L); for(std::map<int, float>::const_iterator i = groupcap.times.begin(); i != groupcap.times.end(); i++){ int rating = i->first; float time = i->second; lua_pushinteger(L, rating); lua_pushnumber(L, time); lua_settable(L, -3); } // Set subtable "times" lua_setfield(L, -2, "times"); // Set simple parameters setintfield(L, -1, "maxlevel", groupcap.maxlevel); setintfield(L, -1, "uses", groupcap.uses); // Insert groupcap table into groupcaps table lua_setfield(L, -2, name.c_str()); } // Set groupcaps table lua_setfield(L, -2, "groupcaps"); //Create damage_groups table lua_newtable(L); // For each damage group for(std::map<std::string, s16>::const_iterator i = toolcap.damageGroups.begin(); i != toolcap.damageGroups.end(); i++){ // Create damage group table lua_pushinteger(L, i->second); lua_setfield(L, -2, i->first.c_str()); } lua_setfield(L, -2, "damage_groups"); }
/* TODO - optional ptag argument? */ static int lnet_get_tcp (lua_State *L) { libnet_t* ud = checkudata(L); libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_TCP_H); struct libnet_tcp_hdr* hdr = (struct libnet_tcp_hdr*) pblock->buf; libnet_pblock_t* oblock = pblock->prev; libnet_pblock_t* dblock = pblock->prev; /* We might have TCP options and/or data blocks, look for them. */ if(oblock && oblock->type != LIBNET_PBLOCK_TCPO_H) { oblock = NULL; } if(dblock && dblock->type == LIBNET_PBLOCK_TCPO_H) { dblock = dblock->prev; } if(dblock && dblock->type != LIBNET_PBLOCK_TCPDATA) { dblock = NULL; } lua_newtable(L); setintfield(L, 2, "ptag", pblock->ptag); setnsintfield(L, 2, "src", hdr->th_sport); setnsintfield(L, 2, "dst", hdr->th_dport); setnlintfield(L, 2, "seq", hdr->th_seq); setnlintfield(L, 2, "ack", hdr->th_ack); setintfield(L, 2, "_rsv", hdr->th_x2); setintfield(L, 2, "_off", hdr->th_off); setintfield(L, 2, "flags", hdr->th_flags); setnsintfield(L, 2, "win", hdr->th_win); setnsintfield(L, 2, "_sum", hdr->th_sum); setnsintfield(L, 2, "urg", hdr->th_urp); if(oblock) { setlstringfield(L, 2, "options", (const char*)oblock->buf, (size_t)oblock->b_len); } else { setlstringfield(L, 2, "options", "", 0); } if(dblock) { setlstringfield(L, 2, "payload", (const char*)dblock->buf, (size_t)dblock->b_len); } return 1; }
static void pushpblock(lua_State* L, libnet_pblock_t* pblock) { int tindex = lua_gettop(L) + 1; lua_newtable(L); setlstringfield(L, tindex, "buf", pblock->buf, pblock->b_len); setintfield(L, tindex, "b_len", pblock->b_len); setintfield(L, tindex, "h_len", pblock->h_len); setintfield(L, tindex, "copied", pblock->copied); setstringfield(L, tindex, "type", libnet_diag_dump_pblock_type(pblock->type)); setintfield(L, tindex, "flags", pblock->flags); setintfield(L, tindex, "ptag", pblock->ptag); if(pblock->next) setintfield(L, tindex, "next", pblock->next->ptag); if(pblock->prev) setintfield(L, tindex, "prev", pblock->prev->ptag); }
// get_craft_recipe(result item) int ModApiCraft::l_get_craft_recipe(lua_State *L) { NO_MAP_LOCK_REQUIRED; std::string item = luaL_checkstring(L, 1); Server *server = getServer(L); CraftOutput output(item, 0); std::vector<CraftDefinition*> recipes = server->cdef() ->getCraftRecipes(output, server, 1); lua_createtable(L, 1, 0); if (recipes.empty()) { lua_pushnil(L); lua_setfield(L, -2, "items"); setintfield(L, -1, "width", 0); return 1; } push_craft_recipe(L, server, recipes[0], output); return 1; }
/* TODO - optional ptag argument? */ static int lnet_get_udp(lua_State *L) { libnet_t* ud = checkudata(L); libnet_pblock_t* pblock = checkptype(L, ud, LIBNET_PBLOCK_UDP_H); struct libnet_udp_hdr* udp = (struct libnet_udp_hdr*) pblock->buf; uint8_t* data = pblock->buf + LIBNET_UDP_H; uint32_t datasz = 0; lua_newtable(L); setintfield(L, 2, "ptag", pblock->ptag); setnsintfield(L, 2, "src", udp->uh_sport); setnsintfield(L, 2, "dst", udp->uh_dport); setnsintfield(L, 2, "_len", udp->uh_ulen); setnsintfield(L, 2, "_sum", udp->uh_sum); if(pblock->b_len > LIBNET_UDP_H) { datasz = pblock->b_len - LIBNET_UDP_H; } setlstringfield(L, 2, "payload", data, (size_t)datasz); return 1; }
// get_all_craft_recipes(result item) int ModApiCraft::l_get_all_craft_recipes(lua_State *L) { NO_MAP_LOCK_REQUIRED; std::string o_item = luaL_checkstring(L,1); IGameDef *gdef = getServer(L); ICraftDefManager *cdef = gdef->cdef(); CraftInput input; CraftOutput output(o_item,0); std::vector<CraftDefinition*> recipes_list = cdef->getCraftRecipes(output, gdef); if (recipes_list.empty()) { lua_pushnil(L); return 1; } // Get the table insert function lua_getglobal(L, "table"); lua_getfield(L, -1, "insert"); int table_insert = lua_gettop(L); lua_newtable(L); int table = lua_gettop(L); for (std::vector<CraftDefinition*>::const_iterator i = recipes_list.begin(); i != recipes_list.end(); i++) { CraftOutput tmpout; tmpout.item = ""; tmpout.time = 0; CraftDefinition *def = *i; tmpout = def->getOutput(input, gdef); std::string query = tmpout.item; char *fmtpos, *fmt = &query[0]; if (strtok_r(fmt, " ", &fmtpos) == output.item) { input = def->getInput(output, gdef); lua_pushvalue(L, table_insert); lua_pushvalue(L, table); lua_newtable(L); int k = 1; lua_newtable(L); for(std::vector<ItemStack>::const_iterator i = input.items.begin(); i != input.items.end(); i++, k++) { if (i->empty()) continue; lua_pushinteger(L,k); lua_pushstring(L,i->name.c_str()); lua_settable(L, -3); } lua_setfield(L, -2, "items"); setintfield(L, -1, "width", input.width); switch (input.method) { case CRAFT_METHOD_NORMAL: lua_pushstring(L,"normal"); break; case CRAFT_METHOD_COOKING: lua_pushstring(L,"cooking"); break; case CRAFT_METHOD_FUEL: lua_pushstring(L,"fuel"); break; default: lua_pushstring(L,"unknown"); } lua_setfield(L, -2, "type"); lua_pushstring(L, &tmpout.item[0]); lua_setfield(L, -2, "output"); if (lua_pcall(L, 2, 0, 0)) script_error(L); } } return 1; }
static void setnsintfield(lua_State* L, int tindex, const char* field, uint16_t i) { setintfield(L, tindex, field, ntohs(i)); }
void push_hit_params(lua_State *L,const HitParams ¶ms) { lua_newtable(L); setintfield(L, -1, "hp", params.hp); setintfield(L, -1, "wear", params.wear); }
static void set_hit_params(lua_State *L, int table, const HitParams ¶ms) { setintfield(L, table, "hp", params.hp); setintfield(L, table, "wear", params.wear); }