This repository has been archived by the owner on Jul 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmasscache.lua
234 lines (187 loc) · 5.08 KB
/
masscache.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
-- INVENTORY CACHE
holostorage.server_inventory = {
cache = {},
new = function (size)
local system = {
size = size,
stacks = {}
}
function system:get_size(abs)
return system.size
end
function system:get_stack(abs, index)
if not index then index = abs end
if system.stacks[index] then
return system.stacks[index]
end
return ItemStack(nil)
end
function system:set_stack(abs, index, stack)
if not stack then
stack = index
index = abs
end
if type(stack) == "table" or type(stack) == "string" then
stack = ItemStack(stack)
end
system.stacks[index] = stack
return stack
end
function system:get_list(abs)
return system.stacks
end
function system:set_list(abs, list)
if not list then list = abs end
system.stacks = list
end
function system:get_width(abs)
local size = 0
for _,v in pairs(system.stacks) do
if v and not v:is_empty() then
size = size + 1
end
end
return size
end
function system:first_empty_index()
local index = 0
local last = 0
for inx, stack in pairs(system.stacks) do
if stack:is_empty() then
index = inx
break
end
if last ~= inx - 1 then
index = inx - 1
break
end
last = inx
end
if index == 0 then
return #system.stacks + 1
end
return index
end
function system:room_for_item(abs, stack)
if not stack then stack = abs end
local matching = false
if system:get_width() < system.size then return true end
for _,stc in pairs(system.stacks) do
if not stc or stc:is_empty() then
matching = true
break
end
if stc:get_name() == stack:get_name() and
stc:get_meta() == stack:get_meta() then
if stc:item_fits(stack) then
matching = true
break
end
end
end
return matching
end
function system:add_item(abs, stack)
if not stack then stack = abs end
local leftover = nil
local first_empty_index = system:first_empty_index()
local added = false
for i, stc in pairs(system.stacks) do
if stc:get_name() == stack:get_name() and stc:get_meta() == stack:get_meta() then
if stc:get_count() == stack:get_stack_max() then
break
end
leftover = system.stacks[i]:add_item(stack)
if leftover and not leftover:is_empty() and system:room_for_item(leftover) then
leftover = system:add_item(leftover)
end
added = true
break
end
end
if added then return leftover end
if not leftover then
system.stacks[system:first_empty_index()] = stack
leftover = ItemStack(nil)
end
return leftover
end
return system
end,
from_table = function (inv, table)
if table["main"] then
table = table["main"]
end
for i, stack in pairs(table) do
inv:set_stack(i, stack)
end
return inv
end,
to_table = function (inv)
local t = {}
local size = inv:get_size()
if size then
for i = 1, size, 1 do
t[i] = inv:get_stack(i):to_table()
end
end
return t
end
}
local function create_invref(capacity)
return holostorage.server_inventory.new(capacity)
end
function holostorage.server_inventory.ensure_disk_inventory(stack, pstr)
local meta = stack:get_meta()
local tag = meta:get_string("storage_tag")
local cap = minetest.registered_items[stack:get_name()].holostorage_capacity
if not tag or tag == "" then
local rnd = PseudoRandom(os.clock())
local rndint = rnd.next(rnd)
local diskid = "d"..pstr.."-"..rndint
meta:set_string("storage_tag", diskid)
holostorage.server_inventory.cache[diskid] = create_invref(cap)
end
return stack
end
function holostorage.server_inventory.load_disk_from_file(stack, diskptr)
local world = minetest.get_worldpath()
local directory = world.."/holostorage"
local cap = minetest.registered_items[stack:get_name()].holostorage_capacity
local inv = create_invref(cap)
minetest.mkdir(directory)
local filetag = minetest.sha1(diskptr)..".invref"
local file = io.open(directory.."/"..filetag)
if not file then
holostorage.server_inventory.cache[diskptr] = inv
return diskptr
end
local str = ""
for line in file:lines() do
str = str..line
end
file:close()
holostorage.server_inventory.from_table(inv, minetest.deserialize(str))
holostorage.server_inventory.cache[diskptr] = inv
return diskptr
end
function holostorage.server_inventory.save_disk_to_file(diskptr)
if not holostorage.server_inventory.cache[diskptr] then return nil end
local world = minetest.get_worldpath()
local directory = world.."/holostorage"
local filetag = minetest.sha1(diskptr)..".invref"
minetest.mkdir(directory)
local inv = holostorage.server_inventory.cache[diskptr]
local data = minetest.serialize(holostorage.server_inventory.to_table(inv))
minetest.safe_file_write(directory.."/"..filetag, data)
return diskptr
end
function holostorage.server_inventory.save_disks_to_file()
for diskptr in pairs(holostorage.server_inventory.cache) do
holostorage.server_inventory.save_disk_to_file(diskptr)
end
end
-- Save disks on shutdown
minetest.register_on_shutdown(function ()
holostorage.server_inventory.save_disks_to_file()
end)