From 71c5a67650934b6ddd9485c5c5e3040a8abd72f0 Mon Sep 17 00:00:00 2001 From: prndev Date: Mon, 20 May 2024 14:03:28 +0200 Subject: [PATCH 1/4] `table.getn` is unavailable in Lua 5.3. Example: https://stackoverflow.com/questions/31452871/. --- buttplug.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/buttplug.lua b/buttplug.lua index b9354c6..1322c54 100644 --- a/buttplug.lua +++ b/buttplug.lua @@ -227,11 +227,11 @@ function buttplug.send_stop_all_devices_cmd() end function buttplug.count_devices() - return table.getn(buttplug.devices) + return #buttplug.devices end function buttplug.add_device(dev) - local dev_count = table.getn(buttplug.devices) + local dev_count = #buttplug.devices buttplug.devices[dev_count + 1] = { index = dev["DeviceIndex"], From ba92ce31deba105141896b1ad5e16cc254b01481 Mon Sep 17 00:00:00 2001 From: prndev Date: Mon, 20 May 2024 14:04:58 +0200 Subject: [PATCH 2/4] Add cdef for using poll on non-Windows systems. --- demo.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/demo.lua b/demo.lua index d372e55..4e595c5 100644 --- a/demo.lua +++ b/demo.lua @@ -1,5 +1,3 @@ --- local pollnet = require("pollnet") --- local json = require("json") local buttplug = require("buttplug") -- get system Sleep function @@ -7,6 +5,7 @@ local ffi = require("ffi") ffi.cdef[[ void Sleep(int ms); +int poll(struct pollfd *fds,unsigned long nfds,int timeout); ]] local sleep From e418a8abe6c1cbe4435bb334ec4873dc759286c9 Mon Sep 17 00:00:00 2001 From: prndev Date: Mon, 20 May 2024 14:06:20 +0200 Subject: [PATCH 3/4] Explicitly use pollnet v0.5.1. --- README.md | 11 +++++++++-- pollnet.lua | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 6ea183c..86620a7 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,15 @@ This is a [buttplug.io](buttplug.io) client written in Lua. ## Example code -TODO +See `demo.lua`. + +## Dependencies + +Known to work with these versions: + +- Lua 5.3 +- [pollnet v0.5.1](https://github.com/probable-basilisk/pollnet/releases/tag/v0.5.1) ## Known issues -- Does not handle multiple devices \ No newline at end of file +- Multiple devices have not been tested. diff --git a/pollnet.lua b/pollnet.lua index 7c62478..5e05aa7 100644 --- a/pollnet.lua +++ b/pollnet.lua @@ -1,25 +1,3 @@ --- MIT License - --- Copyright (c) 2020 probable-basilisk - --- Permission is hereby granted, free of charge, to any person obtaining a copy --- of this software and associated documentation files (the "Software"), to deal --- in the Software without restriction, including without limitation the rights --- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell --- copies of the Software, and to permit persons to whom the Software is --- furnished to do so, subject to the following conditions: - --- The above copyright notice and this permission notice shall be included in all --- copies or substantial portions of the Software. - --- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR --- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, --- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE --- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER --- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, --- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE --- SOFTWARE. - --[[ pollnet bindings for luajit @@ -76,14 +54,18 @@ ffi.cdef[[ struct pnctx* pollnet_init(); struct pnctx* pollnet_get_or_init_static(); void pollnet_shutdown(struct pnctx* ctx); +unsigned int pollnet_open_tcp(struct pnctx* ctx, const char* addr); +unsigned int pollnet_listen_tcp(struct pnctx* ctx, const char* addr); unsigned int pollnet_open_ws(struct pnctx* ctx, const char* url); unsigned int pollnet_simple_http_get(struct pnctx* ctx, const char* url); unsigned int pollnet_simple_http_post(struct pnctx* ctx, const char* url, const char* content_type, const char* data, unsigned int datasize); void pollnet_close(struct pnctx* ctx, unsigned int handle); void pollnet_close_all(struct pnctx* ctx); void pollnet_send(struct pnctx* ctx, unsigned int handle, const char* msg); +void pollnet_send_binary(struct pnctx* ctx, unsigned int handle, const char* msg, unsigned int msgsize); unsigned int pollnet_update(struct pnctx* ctx, unsigned int handle); -int pollnet_get(struct pnctx* ctx, unsigned int handle, char* dest, unsigned int dest_size); +unsigned int pollnet_update_blocking(struct pnctx* ctx, unsigned int handle); +int pollnet_get(struct pnctx* ctx, unsigned int handle, signed char* dest, unsigned int dest_size); int pollnet_get_error(struct pnctx* ctx, unsigned int handle, char* dest, unsigned int dest_size); unsigned int pollnet_get_connected_client_handle(struct pnctx* ctx, unsigned int handle); unsigned int pollnet_listen_ws(struct pnctx* ctx, const char* addr); @@ -160,6 +142,10 @@ function socket_mt:open_ws(url, scratch_size) return self:_open(scratch_size, pollnet.pollnet_open_ws, url) end +function socket_mt:open_tcp(addr, scratch_size) + return self:_open(scratch_size, pollnet.pollnet_open_tcp, addr) +end + function socket_mt:serve_http(addr, dir, scratch_size) self.is_http_server = true if dir and dir ~= "" then @@ -183,6 +169,10 @@ function socket_mt:listen_ws(addr, scratch_size) return self:_open(scratch_size, pollnet.pollnet_listen_ws, addr) end +function socket_mt:listen_tcp(addr, scratch_size) + return self:_open(scratch_size, pollnet.pollnet_listen_tcp, addr) +end + function socket_mt:on_connection(f) self._on_connection = f return self @@ -274,6 +264,14 @@ local function listen_ws(addr, scratch_size) return Socket():listen_ws(addr, scratch_size) end +local function open_tcp(addr, scratch_size) + return Socket():open_tcp(addr, scratch_size) +end + +local function listen_tcp(addr, scratch_size) + return Socket():listen_tcp(addr, scratch_size) +end + local function serve_http(addr, dir, scratch_size) return Socket():serve_http(addr, dir, scratch_size) end @@ -298,10 +296,12 @@ return { shutdown = shutdown_ctx, open_ws = open_ws, listen_ws = listen_ws, + open_tcp = open_tcp, + listen_tcp = listen_tcp, serve_http = serve_http, http_get = http_get, http_post = http_post, Socket = Socket, pollnet = pollnet, nanoid = get_nanoid, -} \ No newline at end of file +} From 51188cdf5163b1dc2792b0839d359c363a5188c0 Mon Sep 17 00:00:00 2001 From: prndev Date: Sun, 30 Jun 2024 21:19:28 +0200 Subject: [PATCH 4/4] I ran a test. It indeed does work with multiple toys. --- README.md | 4 ---- demo.lua | 13 +++++++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 86620a7..05ec00b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,3 @@ Known to work with these versions: - Lua 5.3 - [pollnet v0.5.1](https://github.com/probable-basilisk/pollnet/releases/tag/v0.5.1) - -## Known issues - -- Multiple devices have not been tested. diff --git a/demo.lua b/demo.lua index 4e595c5..fc56468 100644 --- a/demo.lua +++ b/demo.lua @@ -54,14 +54,15 @@ function main_loop() end -- Game doing other things, including running the thing - if buttplug.count_devices() > 0 then - buttplug.send_vibrate_cmd(0, { 0.2 }) - sleep(500) - buttplug.send_vibrate_cmd(0, { 0 }) - os.exit() + for device_index = 0, buttplug.count_devices()-1 do + buttplug.send_vibrate_cmd(device_index, { 0.2 }) + end + sleep(500) + for device_index = 0, buttplug.count_devices()-1 do + buttplug.send_vibrate_cmd(device_index, { 0 }) end - sleep(500) + end end