{"id":13526196,"url":"https://github.com/excessive/cdata","last_synced_at":"2025-10-06T19:11:47.350Z","repository":{"id":26267188,"uuid":"29714504","full_name":"excessive/cdata","owner":"excessive","description":"Serialize Lua tables to C structs and strings.","archived":false,"fork":false,"pushed_at":"2015-08-14T20:13:09.000Z","size":132,"stargazers_count":30,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T07:11:16.461Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/excessive.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-23T03:22:44.000Z","updated_at":"2021-10-27T22:20:02.000Z","dependencies_parsed_at":"2022-08-26T17:30:32.331Z","dependency_job_id":null,"html_url":"https://github.com/excessive/cdata","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excessive%2Fcdata","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excessive%2Fcdata/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excessive%2Fcdata/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excessive%2Fcdata/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/excessive","download_url":"https://codeload.github.com/excessive/cdata/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254020902,"owners_count":22000805,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T06:01:26.290Z","updated_at":"2025-10-06T19:11:42.309Z","avatar_url":"https://github.com/excessive.png","language":"Lua","funding_links":[],"categories":["Serialization"],"sub_categories":[],"readme":"# C Data\n\nC Data is a simple wrapper for LuaJIT FFI's cast to C String. This is primarily used to serialize Lua tables into network-transferable data.\n\nIt is worth noting that if you encode an incomplete table (missing a key) then when you decode the packet, it will have the missing key with a value of 0.\n\n\n## Examples\n\n### Register Packets\n\n```lua\nlocal cdata   = require \"cdata\"\nlocal packets = {}\n\n-- all structs get a type field so we don't lose our minds.\nfunction add_struct(name, fields, map)\n    local struct = string.format(\"typedef struct { uint8_t type; %s } %s;\", fields, name)\n    cdata:new_struct(name, struct)\n\n    -- the packet_type struct isn't a real packet, so don't index it.\n    if map then\n        map.name = name\n        table.insert(packets, map)\n        packets[name] = #packets\n    end\nend\n\n-- Slightly special, I guess.\nadd_struct(\"packet_type\", \"\")\n\nadd_struct(\n    \"player_whois\", [[\n        uint16_t id;\n    ]], {\n        \"id\",\n    }\n)\n\nadd_struct(\n    \"player_create\", [[\n        uint16_t id;\n        uint8_t flags;\n        float position_x, position_y, position_z;\n        float orientation_x, orientation_y, orientation_z;\n        unsigned char name[64];\n    ]], {\n        \"id\",\n        \"flags\",\n        \"position_x\", \"position_y\", \"position_z\",\n        \"orientation_x\", \"orientation_y\", \"orientation_z\",\n        \"name\",\n    }\n)\n\nadd_struct(\n    \"player_update\", [[\n        uint16_t id;\n        float position_x, position_y, position_z;\n        float orientation_x, orientation_y, orientation_z;\n    ]], {\n        \"id\",\n        \"position_x\", \"position_y\", \"position_z\",\n        \"orientation_x\", \"orientation_y\", \"orientation_z\",\n    }\n)\n\nadd_struct(\n    \"player_action\", [[\n        uint16_t id;\n        uint16_t action;\n    ]], {\n        \"id\",\n        \"action\",\n    }\n)\n```\n\n\n### Encode Data\n\n```lua\nlocal player = self.players[1]\nlocal data   = {\n    type          = packets[\"player_update\"],\n    id            = player.id,\n    position_x    = player.position.x,\n    position_y    = player.position.y,\n    position_z    = player.position.z,\n    orientation_x = player.orientation.x,\n    orientation_y = player.orientation.y,\n    orientation_z = player.orientation.z,\n}\n\nlocal struct  = cdata:set_struct(\"player_update\", data)\nlocal encoded = cdata:encode(struct)\n```\n\n\n### Decode Data\n\n```lua\n-- We assume we have a variable named data that we received from the network\nlocal header = cdata:decode(\"packet_type\", data)\nlocal map    = packets[header.type]\n\nif not map then\n    error(string.format(\"Invalid packet type (%s) received!\", header.type))\n    return\nend\n\nlocal decoded = cdata:decode(map.name, data)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexcessive%2Fcdata","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexcessive%2Fcdata","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexcessive%2Fcdata/lists"}