Difference between revisions of "Lua:Class:CustomType"

From Cheat Engine
Jump to navigation Jump to search
(Initial page creation.)
 
m (Class Template Implementation.)
 
Line 1: Line 1:
 
[[Category:Lua]]
 
[[Category:Lua]]
{{CodeBox|'''class''' CustomType ''':''' Object}}
+
{{Class|'''class''' CustomType ''':''' Object}}
  
 
The CustomType class represents a converter that translates raw data into a human-readable interpretation.
 
The CustomType class represents a converter that translates raw data into a human-readable interpretation.

Latest revision as of 18:22, 23 June 2026

{} Class

class CustomType : Object

The CustomType class represents a converter that translates raw data into a human-readable interpretation.

Custom types can be registered through Lua functions or through an Auto Assembler script. Once registered, the CustomType object can convert byte tables to values and values back to byte tables.

Inheritance[edit]

Class Inherits From Description
CustomType Object A converter for interpreting raw data as human-readable values.

Global Functions[edit]

Function Return Type Description
registerCustomTypeLua(typename, bytecount, bytestovaluefunction, valuetobytesfunction, isFloat) CustomType Registers a custom type based on Lua functions.
registerCustomTypeAutoAssembler(script) CustomType Registers a custom type based on an Auto Assembler script. The script must allocate ConvertRoutine and ConvertBackRoutine.
getCustomType(typename) CustomType or nil Returns the custom type object with the specified name, or nil if it could not be found.

Properties[edit]

Property Type Description
name String The name of the custom type.
functiontypename String The function type name of the custom type.
CustomTypeType TCustomTypeType The type of script used by the custom type.
script String The custom type script.
scriptUsesFloat Boolean True if the script interprets user-side values as floating-point values.

Methods[edit]

Method Return Type Description
byteTableToValue({bytetable}, Address) Integer or Number Converts a table of bytes into the interpreted value. Address is optional.
valueToByteTable(value, Address) Table Converts a value into a table of bytes. Address is optional.

registerCustomTypeLua[edit]

<> Reference

function registerCustomTypeLua(typename, bytecount, bytestovaluefunction, valuetobytesfunction, isFloat) : CustomType

Registers a custom type based on Lua functions.

The bytes-to-value function should be defined as a function that receives the bytes as separate parameters and returns an integer value.

The value-to-bytes function should be defined as a function that receives an integer value and returns the bytes that should be written.

registerCustomTypeLua 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 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 interprets user-side values as floating-point values.

registerCustomTypeAutoAssembler[edit]

<> Reference

function registerCustomTypeAutoAssembler(script) : CustomType

Registers a custom type based on an Auto Assembler script.

The script must allocate and define both ConvertRoutine and ConvertBackRoutine.

registerCustomTypeAutoAssembler Parameters[edit]

Parameter Type Description
script String The Auto Assembler script used to define the custom type. The script must allocate ConvertRoutine and ConvertBackRoutine.

getCustomType[edit]

<> Reference

function getCustomType(typename) : CustomType

Returns the custom type object with the specified name, or nil if it could not be found.

getCustomType Parameters[edit]

Parameter Type Description
typename String The name of the custom type to retrieve.

Examples[edit]

local function bytesToValue(b1, b2, b3, b4)
  return b1 + (b2 * 0x100) + (b3 * 0x10000) + (b4 * 0x1000000)
end

local function valueToBytes(value)
  local b1 = value % 0x100
  local b2 = math.floor(value / 0x100) % 0x100
  local b3 = math.floor(value / 0x10000) % 0x100
  local b4 = math.floor(value / 0x1000000) % 0x100

  return b1, b2, b3, b4
end

local customType = registerCustomTypeLua(
  "Lua Integer Example",
  4,
  bytesToValue,
  valueToBytes,
  false
)

print("Registered custom type: " .. tostring(customType))
local customType = getCustomType("Lua Integer Example")

if customType ~= nil then
  local value = customType.byteTableToValue({0x78, 0x56, 0x34, 0x12})
  print("Value: " .. tostring(value))

  local bytes = customType.valueToByteTable(value)

  for i = 1, #bytes do
    print("Byte " .. i .. ": " .. string.format("%02X", bytes[i]))
  end
else
  print("Custom type not found")
end
local script = [[
alloc(ConvertRoutine, 1024)
alloc(ConvertBackRoutine, 1024)

ConvertRoutine:
  // Add conversion code here
  ret

ConvertBackRoutine:
  // Add conversion-back code here
  ret
]]

local customType = registerCustomTypeAutoAssembler(script)

print("Registered custom type: " .. tostring(customType))

See Also[edit]

Main Pages