#include "classes.h" #include "lua/api.h" #include using LuaBitSet = std::bitset<128>; constexpr int BIT_SET_HALF = 64; constexpr LuaBitSet BIT_SET_LOW_MASK = LuaBitSet(~0ull); // The low or high 64 bits. static uint64_t bit_set_low(const LuaBitSet& bits) { return (bits & BIT_SET_LOW_MASK).to_ullong(); } static uint64_t bit_set_high(const LuaBitSet& bits) { return (bits >> BIT_SET_HALF).to_ullong(); } // A bit set from its low and high 64 bits. static LuaBitSet bit_set_make(uint64_t low, uint64_t high) { return LuaBitSet(high) << BIT_SET_HALF | LuaBitSet(low); } // (008bc330, 0085e490, 0085e4d0, 0085e4f0, 0085e510, 0085dae0, 00592200, 0085e530, 0085e560, 0085e5c0) // Registers Lua's BitSet128, the 128 bit flags tears and bombs carry, with l and h as its halves. // Get and Set ignore bits past 127, which the original wraps into the 128. void lua_api_bit_set128_register(lua_State* state) { lua_class_begin(state, "BitSet128", LUA_CLASS_BIT_SET128); lua_static(state, "__call", [](LuaIgnored, int64_t low, int64_t high) { return bit_set_make(low, high); }); lua_method(state, "__band", [](const LuaBitSet& a, const LuaBitSet& b) { return a & b; }); lua_method(state, "__bor", [](const LuaBitSet& a, const LuaBitSet& b) { return a | b; }); lua_method(state, "__bxor", [](const LuaBitSet& a, const LuaBitSet& b) { return a ^ b; }); lua_method(state, "__bnot", [](const LuaBitSet& bits) { return ~bits; }); lua_method(state, "__eq", [](const LuaBitSet& a, const LuaBitSet& b) { return a == b; }); lua_method(state, "__tostring", [](const LuaBitSet& bits) { char text[64]; std::snprintf(text, sizeof(text), "BitSet128: %016llx%016llx", (unsigned long long)bit_set_high(bits), (unsigned long long)bit_set_low(bits)); return std::string(text); }); lua_method(state, "Get", [](const LuaBitSet& bits, uint32_t bit) { return bit < bits.size() && bits[bit]; }); lua_method(state, "Set", [](LuaBitSet& bits, uint32_t bit, bool isSet) { if (bit < bits.size()) bits[bit] = isSet; }); lua_property( state, "l", [](const LuaBitSet& bits) { return (int64_t)bit_set_low(bits); }, [](LuaBitSet& bits, int64_t low) { bits = bit_set_make(low, bit_set_high(bits)); }); lua_property( state, "h", [](const LuaBitSet& bits) { return (int64_t)bit_set_high(bits); }, [](LuaBitSet& bits, int64_t high) { bits = bit_set_make(bit_set_low(bits), high); }); lua_class_end(state); }