function registerCustomTypeLua(typename, bytecount, bytestovaluefunction, valuetobytesfunction, isFloat) : CustomType
Registers a custom type based on Lua functions.
The bytes-to-value function is used to convert raw bytes into a value. It should be defined as a function that receives the bytes as separate parameters and returns an integer value.
The value-to-bytes function is used to convert a value back into bytes. It should be defined as a function that receives the value and returns the bytes that should be written.
Function Parameters[edit]
| Parameter
|
Type
|
Description
|
| typename
|
String
|
The name of the custom type to register.
|
| bytecount
|
Integer
|
The number of bytes used by this custom type.
|
| bytestovaluefunction
|
Function
|
The function that converts the raw bytes into a value.
|
| valuetobytesfunction
|
Function
|
The function that converts a value into the bytes that should be written.
|
| isFloat
|
Boolean
|
If true, the custom type is treated as a floating-point type.
|
Returns[edit]
CustomType — The registered Custom Type object.
Bytes To Value Function[edit]
| Function
|
Return Type
|
Description
|
| function bytestovalue(b1, b2, b3, b4)
|
Integer
|
Receives the bytes of the custom type and returns the interpreted value.
|
Value To Bytes Function[edit]
| Function
|
Return Type
|
Description
|
| function valuetobytes(value)
|
Integer values
|
Receives a value and returns the bytes that should be written.
|
Examples[edit]
1 local function bytesToValue(b1, b2, b3, b4)
2 return b1 + (b2 * 0x100) + (b3 * 0x10000) + (b4 * 0x1000000)
3 end
4
5 local function valueToBytes(value)
6 local b1 = value % 0x100
7 local b2 = math.floor(value / 0x100) % 0x100
8 local b3 = math.floor(value / 0x10000) % 0x100
9 local b4 = math.floor(value / 0x1000000) % 0x100
10
11 return b1, b2, b3, b4
12 end
13
14 local customType = registerCustomTypeLua(
15 "Lua Integer Example",
16 4,
17 bytesToValue,
18 valueToBytes,
19 false
20 )
21
22 print("Registered custom type: " .. tostring(customType.name))
1 local function encryptedBytesToValue(b1, b2, b3, b4)
2 local value = b1 + (b2 * 0x100) + (b3 * 0x10000) + (b4 * 0x1000000)
3
4 return value ~ 0x12345678
5 end
6
7 local function valueToEncryptedBytes(value)
8 local encrypted = value ~ 0x12345678
9
10 local b1 = encrypted % 0x100
11 local b2 = math.floor(encrypted / 0x100) % 0x100
12 local b3 = math.floor(encrypted / 0x10000) % 0x100
13 local b4 = math.floor(encrypted / 0x1000000) % 0x100
14
15 return b1, b2, b3, b4
16 end
17
18 registerCustomTypeLua(
19 "Lua XOR Integer",
20 4,
21 encryptedBytesToValue,
22 valueToEncryptedBytes,
23 false
24 )
Core Lua documentation entry points