-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueprint_custom_data.lua
97 lines (88 loc) · 3.22 KB
/
blueprint_custom_data.lua
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--------------------------------------------------------------------------------------------------------------------------------
-- Usage example:
-- local sample_data = { sample_key = sample_value, {a=1, b = 'c'}, d = true}
-- write_to_combinator(combinator, sample_data)
-- local loaded_data = read_from_combinator(combinator)
-- now loaded_data contains the same data as is same as sample_data
--------------------------------------------------------------------------------------------------------------------------------
-- Author: thelordodin
-- Special thanks to Mooncat - who guided me how to do this.
-- License: free to copy, change, and use in any projects. No warranty.
--------------------------------------------------------------------------------------------------------------------------------
local mp = require 'MessagePack'
local combinator_item_slot_count = 500
--------------------------------------------------------------------------------------------------------------------------------
function data_to_numbers(data)
local mpac = mp.pack(data)
local mpac_m = #mpac % 4
local mpac_s = #mpac - mpac_m
local r = {#mpac}
local v
for i = 1, mpac_s, 4 do -- 4 bytes per value is max for lua number
v = string.byte(mpac, i+3)*0x1000000
+ string.byte(mpac, i+2)*0x10000
+ string.byte(mpac, i+1)*0x100
+ string.byte(mpac, i+0)
if v >= 0x80000000 then
v = v-0x100000000
end
table.insert(r,v)
end
v = ((mpac_s+3 <= #mpac) and string.byte(mpac, mpac_s+3)*0x10000 or 0)
+ ((mpac_s+2 <= #mpac) and string.byte(mpac, mpac_s+2)*0x100 or 0)
+ ((mpac_s+1 <= #mpac) and string.byte(mpac, mpac_s+1) or 0)
table.insert(r,v)
return r
end
--------------------------------------------------------------------------------------------------------------------------------
function numbers_to_data(numbers)
local r = ""
for i = 2, #numbers do
local n = numbers[i]
if n < 0 then
n = n+0x100000000
end
local v4 = n % 0x100
n = (n - v4) / 0x100
local v3 = n % 0x100
n = (n - v3) / 0x100
local v2 = n % 0x100
n = (n - v2) / 0x100
local v1 = n % 0x100
r = r..string.char(v4, v3, v2, v1)
end
r = string.sub(r,1,numbers[1])
return mp.unpack(r)
end
--------------------------------------------------------------------------------------------------------------------------------
function write_to_combinator(combinator, data)
local numbers = data_to_numbers(data)
local params = {}
for i, v in pairs(numbers) do
table.insert(params,
{
signal =
{
type = "virtual",
name = "signal-0"
},
count = v,
index = i
})
end
if #params > combinator_item_slot_count then
return false
end
combinator.get_or_create_control_behavior().parameters = {parameters = params};
return true
end
--------------------------------------------------------------------------------------------------------------------------------
function read_from_combinator(combinator)
local params = combinator.get_or_create_control_behavior().parameters.parameters
local numbers = {}
for _, p in pairs(params) do
table.insert(numbers, p.count)
end
return numbers_to_data(numbers)
end
--------------------------------------------------------------------------------------------------------------------------------