rojo-rbx / rbx-dom

Roblox DOM and (de)serialization implementation in Rust
MIT License
105 stars 42 forks source link

Documenting the CollisionGroupData property format #424

Open phoriah opened 2 weeks ago

phoriah commented 2 weeks ago

If you wish to document the format behind the CollisionGroupData property - here's luau code that generates CollisionGroupData-like output using buffers.

CollisionGroupData = function()
    local collision_groups = game:GetService("PhysicsService"):GetRegisteredCollisionGroups()

    local col_groups_n = #collision_groups

    if col_groups_n == 0 then
        return "\1\0"
    end

    local buffer_size = 3 -- Initial size

    for _, group in collision_groups do
        buffer_size += 7 + #group.name
    end

    buffer_size -= 1 -- Except Default group

    local b = buffer.create(buffer_size)

    local offset = 0

    buffer.writeu8(b, offset, 1) -- ? [CONSTANT] Version byte (likely)
    offset += 1
    buffer.writeu16(b, offset, col_groups_n * 10) -- Group count (not sure about u16, it could be u8, because groups are limited to 32 afaik, and there could be another u8 as a 0 constant after it instead)
    offset += 2

    for i, group in collision_groups do
        local name, id, mask = group.name, i - 1, group.mask
        local nameLength = #name

        if id ~= 0 then
            buffer.writeu8(b, offset, id) -- ID
            offset += 1
        end

        buffer.writeu8(b, offset, 4) -- ? [CONSTANT] Not sure what this is (also not sure about u8, could be i8)
        offset += 1

        buffer.writei32(b, offset, mask) -- Mask value as signed 32-bit integer
        offset += 4

        -- It's weird that string length is u8 and not u32 like it usually is
        buffer.writeu8(b, offset, nameLength) -- Name length
        offset += 1
        buffer.writestring(b, offset, name) -- Name
        offset += nameLength
    end

    return buffer.tostring(b)
end

Hopefully it's enough. Feel free to correct the code if it's wrong!