1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
local base64 = require("vendor.base64")
local json = require("vendor.JSON")
local table_unpack = table.unpack or unpack
local function base64_dec(x)
local succ, dec = pcall(base64.decode, x)
if succ then return dec end
end
local function json_dec(x)
local succ, dec = pcall(json.decode, json, x)
if succ then return dec end
end
local function json_enc(x)
return json:encode(x)
end
local function send(peer, x)
peer:send(json_enc(x))
end
local rand_string
if love then
rand_string = function(n)
local b = {}
for i = 1, n do
table.insert(b, love.math.random(0, 255))
end
return string.char(table_unpack(b))
end
mkdir = love.filesystem.mkdir
else
local rand_file
rand_string = function(n)
local rand_file = rand_file or io.open("/dev/random")
return rand_file:read(n)
end
end
return {
rand_string = rand_string,
mkdir = mkdir,
base64_dec = base64_dec,
base64_enc = base64.encode,
json_dec = json_dec,
json_enc = json_enc,
send = send,
}
|