isaacscript-common
Version:
Helper functions and features for IsaacScript mods.
64 lines (63 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.copyBitSet128 = copyBitSet128;
exports.deserializeBitSet128 = deserializeBitSet128;
exports.isBitSet128 = isBitSet128;
exports.isSerializedBitSet128 = isSerializedBitSet128;
exports.serializeBitSet128 = serializeBitSet128;
const SerializationBrand_1 = require("../enums/private/SerializationBrand");
const isaacAPIClass_1 = require("./isaacAPIClass");
const table_1 = require("./table");
const types_1 = require("./types");
const utils_1 = require("./utils");
const OBJECT_NAME = "BitSet128";
const KEYS = ["l", "h"];
/** Helper function to copy a `BitSet128` Isaac API class. */
function copyBitSet128(bitSet128) {
if (!isBitSet128(bitSet128)) {
error(`Failed to copy a ${OBJECT_NAME} object since the provided object was not a userdata ${OBJECT_NAME} class.`);
}
const lowBits = bitSet128.l;
const highBits = bitSet128.h;
return BitSet128(lowBits, highBits);
}
/**
* Helper function to convert a `SerializedBitSet128` object to a normal `BitSet128` object. (This
* is used by the save data manager when reading data from the "save#.dat" file.)
*/
function deserializeBitSet128(bitSet128) {
if (!(0, types_1.isTable)(bitSet128)) {
error(`Failed to deserialize a ${OBJECT_NAME} object since the provided object was not a Lua table.`);
}
const [l, h] = (0, table_1.getNumbersFromTable)(bitSet128, OBJECT_NAME, ...KEYS);
(0, utils_1.assertDefined)(l, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: l`);
(0, utils_1.assertDefined)(h, `Failed to deserialize a ${OBJECT_NAME} object since the provided object did not have a value for: h`);
return BitSet128(l, h);
}
/** Helper function to check if something is an instantiated `BitSet128` object. */
function isBitSet128(object) {
return (0, isaacAPIClass_1.isIsaacAPIClassOfType)(object, OBJECT_NAME);
}
/**
* Used to determine is the given table is a serialized `BitSet128` object created by the `deepCopy`
* function.
*/
function isSerializedBitSet128(object) {
if (!(0, types_1.isTable)(object)) {
return false;
}
return ((0, table_1.tableHasKeys)(object, ...KEYS) && object.has(SerializationBrand_1.SerializationBrand.BIT_SET_128));
}
/**
* Helper function to convert a `BitSet128` object to a `SerializedBitSet128` object. (This is used
* by the save data manager when writing data from the "save#.dat" file.)
*/
function serializeBitSet128(bitSet128) {
if (!isBitSet128(bitSet128)) {
error(`Failed to serialize a ${OBJECT_NAME} object since the provided object was not a userdata ${OBJECT_NAME} class.`);
}
const bitSet128Table = new LuaMap();
(0, table_1.copyUserdataValuesToTable)(bitSet128, KEYS, bitSet128Table);
bitSet128Table.set(SerializationBrand_1.SerializationBrand.BIT_SET_128, "");
return bitSet128Table;
}