Module Coding

En-/Decodes to/from various formats.

You can require all sub-modules at once by requiring "!all".

Module Status: Experimental 2020-10-31.

Usage:

    local Coding = require('__eradicators-library__/erlib/lua/Coding/!init')()
    

Hydra

HydraOptions Hydra implements some new options that are not in serpent.
Hydra.lines(object, options) Multi-line serialization preset.
Hydra.line() Single-line serialization preset.
Hydra.block() Pretty printable serialization preset.
Hydra.dump() Raw serialize with recursive tables preset.
Hydra.load() Deserialization.
Hydra.serialize() Raw serpent.
Hydra.encode() Alias of Hydra.dump
Hydra.decode(data) Similar to Hydra.load, but only has one return value.

Bluestring

Bluestring.encode(data[, serializer='json'[, prefix='0']])
Bluestring.decode(data[, deserializer='json'[, prefix='0']])

Base64

Base64.encode(data)
Base64.decode(data)

Json

Json.encode(data)
Json.decode(data)

Zip

Zip.encode(data)
Zip.decode(data)

Crc32

Crc32.encode(data)

Sha256

Sha256.encode(data)


Hydra

See the official repository for detailed usage instructions.

This is a customized version of "serpent" - renamed to avoid confusion with Factorios built-in serpent installation. This page only documents changes compared to serpent.

  • Equal to Factorios built-in serpent, Hydra represents redundant sub-tables references by the number 0 zero (instead of nil like normal serpent does).

  • Hydra knows about factorio userdata like LuaEntity, LuaPlayer, etc..

Usage:

local Hydra = require('__eradicators-library__/erlib/lua/Coding/Hydra')()
HydraOptions
Hydra implements some new options that are not in serpent. These work for all presets but are off by default, except for Hydra.lines, where they are on.

Fields:

  • indentlevel NaturalNumber, limits indentation of very deep sub-tables.
  • showref boolean, adds a comment to the output of self-referencing sub-tables.
Hydra.lines(object, options)
Multi-line serialization preset. A more readable, more informative alternative to Hydra.block(). Defaults to indentlevel = 1, showref = true, nocode = true.

Parameters:

Usage:

  • local t = {'a','b','c'}; t = {t,{t,t},{t,t,t},[42]={{{{{table.insert}}}}}}
    local opts={indentlevel=1,showref=true,nocode=true} --"lines" mode default
    print(Hydra.lines(t,opts))
    
    > {
    >   {"a", "b", "c"},
    >   {0 --[[ self[1] ]], 0 --[[ self[1] ]]},                    --self-ref comment
    >   {0 --[[ self[1] ]], 0 --[[ self[1] ]], 0 --[[ self[1] ]]}, --one level indented
    >   [42] = {{{{{function()end}}}}}                            --short code skip
    > }
    
  • print(Hydra.lines{game, game.player, game.player.force})
    > {
    >   {__self = "LuaGameScript"},
    >   {__self = "LuaPlayer"},
    >   {__self = "LuaForce"}
    > }
    
Hydra.line()
Single-line serialization preset.
Hydra.block()
Pretty printable serialization preset.
Hydra.dump()
Raw serialize with recursive tables preset.
Hydra.load()
Deserialization.
Hydra.serialize()
Raw serpent.
Hydra.encode()
Alias of Hydra.dump
Hydra.decode(data)
Similar to Hydra.load, but only has one return value.

Parameters:

  • data string a serialized table

Returns:

    AnyValue or nil the value that was encoded or nil if decode failed. If nil is a valid return value for your data then this is ambiguous and you should consider handling Hydra.load directly.

Bluestring

En-/Decodes to/from factorio blueprint exchange string format. Warning: No verification of in or output is done. This is simply a shortcut for the required Json+Zip+Base64+VersionByte'0' function chain.

See blueprint string format on the wiki for further information.

Note: serpent serialization is only for json-incompatible mod data. The resulting string will not be blueprint comatible.

Note: uses LuaGameScript.encode_string and LuaGameScript.decode_string and LuaGameScript.table_to_json and LuaGameScript.json_to_table when available and native lua otherwise.

Usage:

local Bluestring = require('__eradicators-library__/erlib/lua/Coding/Bluestring')()
Bluestring.encode(data[, serializer='json'[, prefix='0']])

Parameters:

  • data table or string Tables will be serialized to strings before compression.
  • serializer string or nil , 'json' or 'serpent'. (default 'json')
  • prefix string , the version marker byte (default '0')

Returns:

    string or nil the encoded string, or nothing if encoding failed.

Usage:

    LuaItemStack.import_stack(Bluestring.encode(my_bp_table))
Bluestring.decode(data[, deserializer='json'[, prefix='0']])

Parameters:

  • data string an encoded string
  • deserializer string, nil or false , 'json' or 'serpent'. If set to false the data is decompressed but not deserialized. (default 'json')
  • prefix string , the version marker byte (default '0')

Returns:

    table or nil a fresh lua table, or nothing if decoding failed.

Usage:

    local my_bp_table = Bluestring.decode(LuaItemStack.export_stack())

Base64

En-/Decodes to/from base64 encoding. (Source)

Usage:

local Base64 = require('__eradicators-library__/erlib/lua/Coding/Base64')()
Base64.encode(data)

Parameters:

  • data string the original string

Returns:

    string the encoded string
Base64.decode(data)

Parameters:

Returns:

    string the original string

Json

En-/Decodes to/from json. (Source)

Usage:

local Json = require('__eradicators-library__/erlib/lua/Coding/Json')()
Json.encode(data)

Parameters:

  • data string the original string

Returns:

    string the encoded string
Json.decode(data)

Parameters:

  • data string the encoded string

Returns:

    string the original string

Zip

En-/Decompresses to/from zip deflate. (Source)

Usage:

local Zip = require('__eradicators-library__/erlib/lua/Coding/Zip')()
Zip.encode(data)

Parameters:

  • data string the original string

Returns:

    string the compressed string
Zip.decode(data)

Parameters:

  • data string the compressed string

Returns:

    string the original string

Crc32

Calculates the crc32 hash of the input string. (Source)

Usage:

local Crc32 = require('__eradicators-library__/erlib/lua/Coding/Crc32')()
Crc32.encode(data)

Parameters:

  • data string the original string

Returns:

    uint or nil 0 < n < 2^32 (4294967296), or nil if it failed.

Sha256

Calculates the sha256 hash of the input string. (Source)

Usage:

local Sha256 = require('__eradicators-library__/erlib/lua/Coding/Sha256')()

oding.Sha256 = (_ENV.bit32) and import 'erlib/lua/Coding/Sha256' or nil
Sha256.encode(data)

Parameters:

  • data string the original string

Returns:

    string or nil the hash, or nothing if it failed
generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19