Data Library
Data persistence, serialization, and management system for the Lilia framework.
Overview
The data library provides comprehensive functionality for data persistence, serialization, and management within the Lilia framework. It handles encoding and decoding of complex data types including vectors, angles, colors, and nested tables for database storage. The library manages both general data storage with gamemode and map-specific scoping, as well as entity persistence for maintaining spawned entities across server restarts. It includes automatic serialization/deserialization, database integration, and caching mechanisms to ensure efficient data access and storage operations.
lia.data.encodetable(value)
Encode vectors/angles/colors/tables into JSON-safe structures.
Before persisting data to DB or file storage.
Parameters:
any valueReturns:
any Encoded representation.Example Usage:
local payload = lia.data.encodetable({
pos = Vector(0, 0, 64),
ang = Angle(0, 90, 0),
tint = Color(255, 0, 0)
})
lia.data.decode(value)
Decode nested structures into native types (Vector/Angle/Color).
After reading serialized data from DB/file.
Parameters:
any valueReturns:
any Decoded value with deep conversion.Example Usage:
local decoded = lia.data.decode(storedJsonTable)
local pos = decoded.spawnPos
lia.data.serialize(value)
Serialize a value into JSON, pre-encoding special types.
Before writing data blobs into DB columns.
Parameters:
any valueReturns:
string JSON string.Example Usage:
local json = lia.data.serialize({pos = Vector(1,2,3)})
lia.db.updateSomewhere(json)
lia.data.deserialize(raw)
Deserialize JSON/pon or raw tables back into native types.
After fetching data rows from DB.
Parameters:
string|table|any rawReturns:
anyExample Usage:
local row = lia.db.select(...):get()
local data = lia.data.deserialize(row.data)
lia.data.decodeVector(raw)
Decode a vector from various string/table encodings.
While rebuilding persistent entities or map data.
Parameters:
any rawReturns:
Vector|anyExample Usage:
local pos = lia.data.decodeVector(row.pos)
if isvector(pos) then ent:SetPos(pos) end
lia.data.decodeAngle(raw)
Decode an angle from string/table encodings.
During persistence loading or data deserialization.
Parameters:
any rawReturns:
Angle|anyExample Usage:
local ang = lia.data.decodeAngle(row.angles)
if isangle(ang) then ent:SetAngles(ang) end
lia.data.set(key, value, global, ignoreMap)
Persist a key/value pair scoped to gamemode/map (or global).
To save configuration/state data into the DB.
Parameters:
string keyany value
boolean global optional
boolean ignoreMap optional
Returns:
string Path prefix used for file save fallback.Example Usage:
lia.data.set("event.active", true, false, false)
lia.data.delete(key, global, ignoreMap)
Delete a stored key (and row if empty) from DB cache.
To remove saved state/config entries.
Parameters:
string keyboolean global optional
boolean ignoreMap optional
Returns:
booleanExample Usage:
lia.data.delete("event.active")
lia.data.loadTables()
Load stored data rows for global, gamemode, and map scopes.
On database ready to hydrate lia.data.stored cache.
Example Usage:
hook.Add("DatabaseConnected", "LoadLiliaData", lia.data.loadTables)
lia.data.loadPersistence()
Ensure persistence table has required columns; add if missing.
Before saving/loading persistent entities.
Returns:
promiseExample Usage:
lia.data.loadPersistence():next(function() print("Persistence columns ready") end)
lia.data.savePersistence(entities)
Save persistent entities to the database (with dynamic columns).
On PersistenceSave hook/timer with collected entities.
Parameters:
table entities Array of entity data tables.Returns:
promise|nilExample Usage:
hook.Run("PersistenceSave", collectedEntities)
lia.data.loadPersistenceData(callback)
Load persistent entities from DB, decode fields, and cache them.
On server start or when manually reloading persistence.
Parameters:
function callback optional Invoked with entities table once loaded.Returns:
promiseExample Usage:
lia.data.loadPersistenceData(function(entities)
for _, entData in ipairs(entities) do
-- spawn logic here
end
end)
lia.data.get(key, default)
Fetch a stored key from cache, deserializing strings on demand.
Anywhere stored data is read after loadTables.
Parameters:
string keyany default
Returns:
anyExample Usage:
local eventData = lia.data.get("event.settings", {})
lia.data.getPersistence()
Return the cached list of persistent entities (last loaded/saved).
For admin tools or debug displays.
Returns:
tableExample Usage:
PrintTable(lia.data.getPersistence())
lia.data.addEquivalencyMap(map1, map2)
Register an equivalency between two map names (bidirectional).
To share data/persistence across multiple map aliases.
Parameters:
string map1string map2
Example Usage:
lia.data.addEquivalencyMap("rp_downtown_v1", "rp_downtown_v2")
lia.data.getEquivalencyMap(map)
Resolve a map name to its equivalency (if registered).
Before saving/loading data keyed by map name.
Parameters:
string mapReturns:
stringExample Usage:
local canonical = lia.data.getEquivalencyMap(game.GetMap())