add FFIBytearray

This commit is contained in:
MihailRis 2025-04-06 22:23:39 +03:00
parent 4260742d6e
commit 36c2971fcf
2 changed files with 8 additions and 9 deletions

View File

@ -263,6 +263,8 @@ entities.get_all = function(uids)
return stdcomp.get_all(uids) return stdcomp.get_all(uids)
end end
end end
FFIBytearray = require "core:internal/bytearray"
ffi = nil
math.randomseed(time.uptime() * 1536227939) math.randomseed(time.uptime() * 1536227939)

View File

@ -1,6 +1,3 @@
local _ffi = ffi
ffi = nil
-- Lua has no parallelizm, also _set_data does not call any lua functions so -- Lua has no parallelizm, also _set_data does not call any lua functions so
-- may be reused one global ffi buffer per lua_State -- may be reused one global ffi buffer per lua_State
local canvas_ffi_buffer local canvas_ffi_buffer
@ -8,14 +5,14 @@ local canvas_ffi_buffer_size = 0
function __vc_Canvas_set_data(self, data) function __vc_Canvas_set_data(self, data)
if type(data) == "cdata" then if type(data) == "cdata" then
self:_set_data(tostring(_ffi.cast("uintptr_t", data))) self:_set_data(tostring(ffi.cast("uintptr_t", data)))
end end
local width = self.width local width = self.width
local height = self.height local height = self.height
local size = width * height * 4 local size = width * height * 4
if size > canvas_ffi_buffer_size then if size > canvas_ffi_buffer_size then
canvas_ffi_buffer = _ffi.new( canvas_ffi_buffer = ffi.new(
string.format("unsigned char[%s]", size) string.format("unsigned char[%s]", size)
) )
canvas_ffi_buffer_size = size canvas_ffi_buffer_size = size
@ -23,7 +20,7 @@ function __vc_Canvas_set_data(self, data)
for i=0, size - 1 do for i=0, size - 1 do
canvas_ffi_buffer[i] = data[i + 1] canvas_ffi_buffer[i] = data[i + 1]
end end
self:_set_data(tostring(_ffi.cast("uintptr_t", canvas_ffi_buffer))) self:_set_data(tostring(ffi.cast("uintptr_t", canvas_ffi_buffer)))
end end
function crc32(bytes, chksum) function crc32(bytes, chksum)
@ -31,14 +28,14 @@ function crc32(bytes, chksum)
local length = #bytes local length = #bytes
if type(bytes) == "table" then if type(bytes) == "table" then
local buffer_len = _ffi.new('int[1]', length) local buffer_len = ffi.new('int[1]', length)
local buffer = _ffi.new( local buffer = ffi.new(
string.format("char[%s]", length) string.format("char[%s]", length)
) )
for i=1, length do for i=1, length do
buffer[i - 1] = bytes[i] buffer[i - 1] = bytes[i]
end end
bytes = _ffi.string(buffer, buffer_len[0]) bytes = ffi.string(buffer, buffer_len[0])
end end
return _crc32(bytes, chksum) return _crc32(bytes, chksum)
end end