• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

BuckarooBanzay / mapblock_lib / 8748698230

19 Apr 2024 05:08AM UTC coverage: 19.22% (-35.6%) from 54.827%
8748698230

Pull #3

github

BuckarooBanzay
mtt action
Pull Request #3: mtt action

202 of 1051 relevant lines covered (19.22%)

16.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.33
/get_catalog.lua
1
---------
2
-- Catalog functions
3

4
local has_placeholder_mod = minetest.get_modpath("placeholder")
1✔
5

6
------
7
-- Catalog object
8
-- @type Catalog
9
local Catalog = {}
1✔
10
local Catalog_mt = { __index = Catalog }
1✔
11

12
--- Get the overall size of the catalog
13
-- @return @{util.mapblock_pos} the size in mapblocks
14
function Catalog:get_size()
1✔
15
        return vector.add(self.manifest.range, 1)
×
16
end
17

18
local function get_meta_filename(mapblock_pos)
19
        local pos_str = minetest.pos_to_string(mapblock_pos)
×
20
        return "mapblock_" .. pos_str .. ".meta.json"
×
21
end
22

23
local function get_mapblock_bin_filename(mapblock_pos)
24
        local pos_str = minetest.pos_to_string(mapblock_pos)
×
25
        return "mapblock_" .. pos_str .. ".bin"
×
26
end
27

28
local function read_manifest_mapblock(filename, catalog_mapblock_pos)
29
        local f = io.open(filename, "rb")
×
30
        local z, err = mtzip.unzip(f)
×
31
        if err then
×
32
                f:close()
×
33
                return nil, nil, err
×
34
        end
35

36
        local meta_name = get_meta_filename(catalog_mapblock_pos)
×
37
        local bin_name = get_mapblock_bin_filename(catalog_mapblock_pos)
×
38
        local mapblock_data = z:get(bin_name)
×
39
        local manifest_data = z:get(meta_name)
×
40
        local mapblock = mapblock_lib.read_mapblock(mapblock_data)
×
41
        if not manifest_data then
×
42
                return nil, nil, "no manifest found in '" .. meta_name .. "'"
×
43
        end
44
        local manifest = minetest.parse_json(manifest_data)
×
45
        f:close()
×
46

47
        return manifest, mapblock
×
48
end
49

50
--- Check for the existence of a mapblock in the catalog
51
-- @param catalog_mapblock_pos @{util.mapblock_pos} the mapblock position in the catalog
52
-- @return the zip-manifest entry of the meta-file or nil if not found
53
function Catalog:has_mapblock(catalog_mapblock_pos)
1✔
54
        local meta_name = get_meta_filename(catalog_mapblock_pos)
×
55
        return self.zip:get_entry(meta_name)
×
56
end
57

58
--- Deserialize a single mapblock from the catalog
59
-- @see deserialize_options.lua
60
-- @param catalog_mapblock_pos @{util.mapblock_pos} the mapblock position in the catalog
61
-- @param world_mapblock_pos @{util.mapblock_pos} the mapblock position in the world
62
-- @param options @{deserialize_mapblock.deserialize_options} mapblock deserialization options
63
-- @return success true on success
64
-- @return error in case of an error
65
function Catalog:deserialize(catalog_mapblock_pos, world_mapblock_pos, options)
1✔
66
        local manifest, mapblock, err = read_manifest_mapblock(self.filename, catalog_mapblock_pos)
×
67
        if err then
×
68
                return nil, err
×
69
        end
70
        options = options or {}
×
71
        return mapblock_lib.deserialize_mapblock(world_mapblock_pos, mapblock, manifest, options)
×
72
end
73

74
--- Prepare a mapblock with options for faster access
75
-- @param catalog_mapblock_pos @{util.mapblock_pos} the mapblock position in the catalog
76
-- @param options @{deserialize_mapblock.deserialize_options} mapblock deserialization options
77
-- @return deserFn a function that accepts a mapblock position @{util.mapblock_pos} to write the mapblock to the map
78
-- @return error in case of an error
79
function Catalog:prepare(catalog_mapblock_pos, options)
1✔
80
        options = options or {}
×
81
        local manifest, mapblock, err = read_manifest_mapblock(self.filename, catalog_mapblock_pos)
×
82
        if err then
×
83
                return nil, err
×
84
        end
85

86
        if options.transform and options.transform.replace then
×
87
                -- replace node-ids before localizing them
88
                mapblock_lib.replace(options.transform.replace, manifest.node_mapping, mapblock)
×
89
        end
90

91
        -- localize node ids and ignore unknown nodes
92
        local all_nodes_known, unknown_nodes = mapblock_lib.localize_nodeids(manifest.node_mapping, mapblock.node_ids)
×
93
        if has_placeholder_mod and not all_nodes_known then
×
94
                -- set placeholders
95
                mapblock_lib.place_placeholders(mapblock, manifest, unknown_nodes)
×
96
        end
97

98
        -- transform, if needed
99
        if options.transform then
×
100
                local size = {x=15, y=15, z=15}
×
101
                mapblock_lib.transform(options.transform, size, mapblock, manifest.metadata)
×
102
        end
103

104
        return function(mapblock_pos)
105
                -- write to map
106
                local min, max = mapblock_lib.get_mapblock_bounds_from_mapblock(mapblock_pos)
×
107
                mapblock_lib.deserialize_part(min, max, mapblock, manifest.metadata, options)
×
108
        end
109
end
110

111
------
112
-- Deserialize options
113
-- @number delay for async mode: delay between deserialization-calls
114
-- @number rotate_y the y rotation, can be 0,90,180 or 270 degrees
115
-- @field callback function to call when the blocks are deserialized
116
-- @field progress_callback function to call when the progress is update
117
-- @field error_callback function to call on errors
118
-- @field mapblock_options function that returns the deserialization options when called with a mapblock_pos as param
119
-- @table deserialize_all_options
120

121
--- Deserialize all mapblocks in the catalog to the world
122
-- @see deserialize_options.lua
123
-- @param target_mapblock_pos @{util.mapblock_pos} the first mapblock position
124
-- @param options[opt] @{deserialize_all_options} deserialization options
125
function Catalog:deserialize_all(target_mapblock_pos, options)
1✔
126
        local f = io.open(self.filename, "rb")
×
127
        local z, err = mtzip.unzip(f)
×
128
        if err then
×
129
                return false, err
×
130
        end
131

132
        local pos1 = target_mapblock_pos
×
133
        local pos2 = vector.add(pos1, self.manifest.range)
×
134
        local iterator, total_count = mapblock_lib.pos_iterator(pos1, pos2)
×
135
        local mapblock_pos
136
        local count = 0
×
137

138
        options = options or {}
×
139
        options.delay = options.delay or 0.2
×
140
        options.rotate_y = options.rotate_y or 0
×
141
        options.callback = options.callback or function() end
×
142
        options.progress_callback = options.progress_callback or function() end
×
143
        options.error_callback = options.error_callback or function() end
×
144
        options.mapblock_options = options.mapblock_options or function() end
×
145

146
        local start = minetest.get_us_time()
×
147

148
        local worker
149
        worker = function()
150
                mapblock_pos = iterator()
×
151
                if mapblock_pos then
×
152
                        local rel_pos = vector.subtract(mapblock_pos, pos1)
×
153
                        rel_pos = mapblock_lib.rotate_pos(rel_pos, self.manifest.range, options.rotate_y)
×
154
                        local mapblock_entry_name = "mapblock_" .. minetest.pos_to_string(rel_pos) .. ".bin"
×
155
                        local manifest_entry_name = "mapblock_" .. minetest.pos_to_string(rel_pos) .. ".meta.json"
×
156

157
                        local mb_manifest = z:get(manifest_entry_name)
×
158
                        if mb_manifest then
×
159
                                local manifest = minetest.parse_json(mb_manifest)
×
160
                                local mapblock = mapblock_lib.read_mapblock(z:get(mapblock_entry_name))
×
161
                                local mapblock_options = options.mapblock_options(mapblock_pos)
×
162
                                if options.rotate_y then
×
163
                                        -- apply mapblock rotation to mapblock-nodes
164
                                        mapblock_options = mapblock_options or {}
×
165
                                        mapblock_options.transform = mapblock_options.transform or {}
×
166
                                        mapblock_options.transform.rotate = mapblock_options.transform.rotate or {}
×
167
                                        mapblock_options.transform.rotate.axis = "y"
×
168
                                        mapblock_options.transform.rotate.angle = options.rotate_y
×
169
                                end
170
                                local _, deser_err = mapblock_lib.deserialize_mapblock(mapblock_pos, mapblock, manifest, mapblock_options)
×
171
                                if deser_err then
×
172
                                        options.error_callback(deser_err)
×
173
                                        return
×
174
                                end
175
                        end
176

177
                        count = count + 1
×
178
                        options.progress_callback(count / total_count)
×
179
                        minetest.after(options.delay, worker)
×
180
                else
181
                        -- done
182
                        f:close()
×
183
                        local micros = minetest.get_us_time() - start
×
184
                        options.callback(count, micros)
×
185
                end
186
        end
187

188
        -- initial call
189
        worker()
×
190
end
191

192
--- create a new catalog wrapper for the given filename
193
-- @param filename the file to read from
194
-- @return @{Catalog} the catalog object
195
function mapblock_lib.get_catalog(filename)
2✔
196
        local f = io.open(filename, "rb")
×
197
        if not f then
×
198
                return nil, "file is nil: '" .. filename .. "'"
×
199
        end
200
        local z, err = mtzip.unzip(f)
×
201
        if err then
×
202
                f:close()
×
203
                return nil, err
×
204
        end
205

206
        local manifest = minetest.parse_json(z:get("manifest.json"))
×
207
        f:close()
×
208
        if not manifest then
×
209
                return false, "no manifest found!"
×
210
        end
211

212
        local self = {
×
213
                filename = filename,
214
                manifest = manifest,
215
                -- for lookups only
216
                zip = z
×
217
        }
218
        return setmetatable(self, Catalog_mt)
×
219
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc