| 464 | } |
| 465 | |
| 466 | static 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 | |
| 512 | int_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 |
no test coverage detected