From 7704f5280d2131cbd77264097aef014d218fe59b Mon Sep 17 00:00:00 2001 From: arpruss Date: Fri, 30 Oct 2015 21:18:27 -0500 Subject: [PATCH] support builds without bit module --- raspberryjammod/base64.lua | 7 +- raspberryjammod/init.lua | 17 +++-- raspberryjammod/slowbit32.lua | 139 ++++++++++++++++++++++++++++++++++ raspberryjammod/socket.lua | 5 +- raspberryjammod/tools.lua | 7 +- 5 files changed, 162 insertions(+), 13 deletions(-) create mode 100644 raspberryjammod/slowbit32.lua diff --git a/raspberryjammod/base64.lua b/raspberryjammod/base64.lua index e473ce8..511e08d 100644 --- a/raspberryjammod/base64.lua +++ b/raspberryjammod/base64.lua @@ -58,8 +58,11 @@ local Base64 = {} -- Imports ---------------------------------------------------------------------------- -bit = ie.require("bit") - +local bit +local status,err = pcall(function() bit = ie.require 'bit' end) +if not status then + bit = ie.dofile(minetest.get_modpath(minetest.get_current_modname()) .. '/slowbit32.lua') +end local band, bor, lshift, rshift = bit.band, bit.bor, bit.lshift, bit.rshift ---------------------------------------------------------------------------- diff --git a/raspberryjammod/init.lua b/raspberryjammod/init.lua index 946ac2c..8bf8595 100644 --- a/raspberryjammod/init.lua +++ b/raspberryjammod/init.lua @@ -87,13 +87,16 @@ else remote_address = "*" end -local server = socket.bind(remote_address, 4711) +local server,err = socket.bind(remote_address, 4711) +assert(server, err) + server:setoption('tcp-nodelay',true) server:settimeout(0) local ws_server = nil if ws then + if not bit or not bit.bxor then bit = ie.require("slowbit32") end tools = ie.require("tools") base64 = ie.require("base64") ws_server = socket.bind(remote_address, 14711) @@ -285,6 +288,10 @@ function python(name, args, kill_script) return true end +local function sanitize_pipe(s) + return s:gsub("%&", "&"):gsub("%|", "|") +end + minetest.register_on_chat_message(function(name, message) local id = get_player_id_by_name(name) table.insert(chat_record, id .. "," .. sanitize_pipe(message)) @@ -318,10 +325,6 @@ function get_entity_id(entity) end end -local function sanitize_pipe(s) - return s:gsub("%&", "&"):gsub("%|", "|") -end - function handle_entity(cmd, id, args) local entity if id == nil then @@ -665,8 +668,8 @@ function handle_command(line) end local args = {} - for arg in argtext:gmatch("([^,]+)") do - table.insert(args, arg) + for argument in argtext:gmatch("([^,]+)") do + table.insert(args, argument) end if cmd:sub(1,6) == "world." then return handle_world(cmd:sub(7),args) diff --git a/raspberryjammod/slowbit32.lua b/raspberryjammod/slowbit32.lua new file mode 100644 index 0000000..145ab74 --- /dev/null +++ b/raspberryjammod/slowbit32.lua @@ -0,0 +1,139 @@ +-- These are super slow 32-bit operations -- + +local two31 = 2^31 +local two32 = 2^32 + +local to_binary = function(x) + local pos = two31 + local a = "" + if x < 0 then + x = x + two32 + end + x = x % two32 + for i=1,32 do + if x >= pos then + a = a .. "1" + x = x - pos + else + a = a .. "0" + end + pos = pos / 2 + end + return a +end + +local from_binary = function(x) + local z = tonumber(x) + if z >= two31 then + return z - two32 + else + return z + end +end + + +local band = function(...) + local a = {} + local arg = {...} + for i = 1,#arg do + a[i] = to_binary(arg[i]) + end + local c = "" + for i = 1,32 do + local value = true + for j = 1,#arg do + if a[j]:sub(i,i) == "0" then + value = false + break + end + end + + if value then + c = c .. "1" + else + c = c .. "0" + end + end + return from_binary(c) +end + +local bor = function(...) + local a = {} + local arg = {...} + for i = 1,#arg do + a[i] = to_binary(arg[i]) + end + local c = "" + for i = 1,32 do + local value = false + for j = 1,#arg do + if a[j]:sub(i,i) == "1" then + value = true + break + end + end + + if value then + c = c .. "1" + else + c = c .. "0" + end + end + return from_binary(c) +end + +local bxor = function(...) + local a = {} + local arg = {...} + for i = 1,#arg do + a[i] = to_binary(arg[i]) + end + local c = "" + for i = 1,32 do + local value = false + for j = 1,#arg do + if a[j]:sub(i,i) == "1" then + value = not value + end + end + + if value then + c = c .. "1" + else + c = c .. "0" + end + end + return from_binary(c) +end + + +local bnot = function(x) + local a = to_binary(x) + local c = "" + for i = 1,32 do + if a:sub(i,i) == "0" then + c = c .. "1" + else + c = c .. "0" + end + end + return from_binary(c) +end + +local lshift = function(x,n) + return (x * 2^n) % two32 +end + +local rshift = function(x,n) + return math.floor(x / 2^n) +end + +local rol = function(x,n) + local a = to_binary(x) + for i = 1,n do + a = a:sub(2,32) .. a:sub(1,1) + end + return from_binary(a) +end + +return {rol=rol, bxor=bxor, bor=bor, band=band, bnot=bnot, lshift=lshift, rshift=rshift} diff --git a/raspberryjammod/socket.lua b/raspberryjammod/socket.lua index dcdae92..0682aef 100644 --- a/raspberryjammod/socket.lua +++ b/raspberryjammod/socket.lua @@ -21,8 +21,6 @@ if not status then socket = ie.require("socket.cx64") end -ie.module("socket") - ----------------------------------------------------------------------------- -- Exported auxiliar functions ----------------------------------------------------------------------------- @@ -49,7 +47,7 @@ function bind(host, port, backlog) return sock end -try = newtry() +-- try = newtry() function choose(table) return function(name, opt1, opt2) @@ -141,3 +139,4 @@ sourcet["default"] = sourcet["until-closed"] source = choose(sourcet) +return { connect=connect, bind=bind, choose=choose, source=source } diff --git a/raspberryjammod/tools.lua b/raspberryjammod/tools.lua index 0631232..d7ff8b2 100644 --- a/raspberryjammod/tools.lua +++ b/raspberryjammod/tools.lua @@ -4,7 +4,12 @@ else ie = _G end -local bit = ie.require 'bit' +local bit +local status,err = pcall(function() bit = ie.require 'bit' end) +if not status then + bit = ie.dofile(minetest.get_modpath(minetest.get_current_modname()) .. '/slowbit32.lua') +end + local rol = bit.rol local bxor = bit.bxor local bor = bit.bor