MCPcopy Create free account
hub / github.com/DFHack/dfhack / decode_matinfo

Function decode_matinfo

library/LuaApi.cpp:466–526  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

464}
465
466static bool decode_matinfo(lua_State *state, MaterialInfo *info, bool numpair = false)
467{
468 int curtop = lua_gettop(state);
469
470 luaL_checkany(state, 1);
471
472 if (!lua_isnumber(state, 1))
473 {
474 if (lua_isnil(state, 1))
475 return false;
476
477 if (lua_getmetatable(state, 1))
478 {
479 if (lua_rawequal(state, -1, lua_upvalueindex(1)))
480 {
481 lua_getfield(state, 1, "type");
482 lua_getfield(state, 1, "index");
483 goto int_pair;
484 }
485
486 lua_pop(state, 1);
487 }
488
489 if (lua_isuserdata(state, 1))
490 {
491 if (auto item = Lua::GetDFObject<df::item>(state, 1))
492 return info->decode(item);
493 if (auto plant = Lua::GetDFObject<df::plant>(state, 1))
494 return info->decode(MaterialInfo::PLANT_BASE, plant->material);
495 if (auto mvec = Lua::GetDFObject<df::material_vec_ref>(state, 1))
496 return info->decode(*mvec, luaL_checkint(state, 2));
497 }
498
499 lua_getfield(state, 1, "mat_type");
500 lua_getfield(state, 1, "mat_index");
501 goto int_pair;
502 }
503 else
504 {
505 if (!numpair)
506 luaL_argerror(state, 1, "material info object expected");
507
508 if (curtop < 2)
509 lua_settop(state, 2);
510 }
511
512int_pair:
513 {
514 int ok;
515 int type = lua_tointegerx(state, -2, &ok);
516 if (!ok)
517 luaL_argerror(state, 1, "material id is not a number");
518 int index = lua_tointegerx(state, -1, &ok);
519 if (!ok)
520 index = -1;
521
522 lua_settop(state, curtop);
523

Callers 5

dfhack_matinfo_decodeFunction · 0.85
dfhack_matinfo_getTokenFunction · 0.85
dfhack_matinfo_toStringFunction · 0.85
dfhack_matinfo_matchesFunction · 0.85

Calls 11

lua_gettopFunction · 0.85
luaL_checkanyFunction · 0.85
lua_isnumberFunction · 0.85
lua_getmetatableFunction · 0.85
lua_rawequalFunction · 0.85
lua_getfieldFunction · 0.85
lua_isuserdataFunction · 0.85
luaL_argerrorFunction · 0.85
lua_settopFunction · 0.85
lua_tointegerxFunction · 0.85
decodeMethod · 0.45

Tested by

no test coverage detected