Update installation instructions - adopted from Wuzzy's notes.
[insidethebox/insidethebox_wuzzy.git] / mods / boxes / io.lua
blobc2d2d417f66fa0e8935f3b55d1678b93e5a977e8
2 --[[
4 ITB (insidethebox) minetest game - Copyright (C) 2017-2018 sofar & nore
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public License
8 as published by the Free Software Foundation; either version 2.1
9 of the License, or (at your option) any later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19 MA 02111-1307 USA
21 ]]--
23 local function bytes_to_string(bytes)
24 local s = {}
25 for i = 1, #bytes do
26 s[i] = string.char(bytes[i])
27 end
28 return table.concat(s)
29 end
31 function boxes.import_box(id, box_data)
32 local data_index = 1
33 local function read_u8()
34 local c = string.byte(box_data, data_index)
35 data_index = data_index + 1
36 return c
37 end
38 local function read_u16()
39 local a = read_u8()
40 local b = read_u8()
41 return 256 * a + b
42 end
43 local function read_pos()
44 local x = read_u16()
45 local y = read_u16()
46 local z = read_u16()
47 return {x = x, y = y, z = z}
48 end
49 local version = read_u8()
50 assert (version == 1)
51 local type = read_u8()
52 local size = read_pos()
53 local entry = read_pos()
54 local exit = read_pos()
55 db.box_set_data(id, string.sub(box_data, data_index))
56 db.box_set_meta(id, {
57 type = type,
58 meta = {
59 size = size,
60 entry = entry,
61 exit = exit,
64 end
66 function boxes.export_box(id)
67 local data_index = 1
68 local data = {}
69 local function write_u8(x)
70 data[data_index] = x
71 data_index = data_index + 1
72 end
73 local function write_u16(x)
74 write_u8(math.floor(x / 256))
75 write_u8(x % 256)
76 end
77 local function write_pos(p)
78 write_u16(p.x)
79 write_u16(p.y)
80 write_u16(p.z)
81 end
82 local meta = db.box_get_meta(id)
83 write_u8(1) -- version
84 write_u8(meta.type)
85 write_pos(meta.meta.size)
86 write_pos(meta.meta.entry)
87 write_pos(meta.meta.exit)
88 return bytes_to_string(data) .. db.box_get_data(id)
89 end
91 local function read_file(filename)
92 local file = io.open(filename, "r")
93 if file ~= nil then
94 local file_content = file:read("*all")
95 io.close(file)
96 return file_content
97 end
98 return ""
99 end
101 --[[ -- kept for historical purposes.
102 local function write_file(filename, data)
103 local file, err = io.open(filename, "w")
104 if file then
105 file:write(data)
106 io.close(file)
107 else
108 error(err)
111 ]]--
113 local modpath = minetest.get_modpath(minetest.get_current_modname())
115 if not db.box_exists(0) then
116 local box_data = read_file(modpath .. "/entry.box")
117 boxes.import_box(0, box_data)
120 if not db.box_exists(1) then
121 local box_data = read_file(modpath .. "/exit.box")
122 boxes.import_box(1, box_data)
125 if not db.box_exists(2) then
126 local box_data = read_file(modpath .. "/box.box")
127 boxes.import_box(2, box_data)
130 -- local worldpath = minetest.get_worldpath()
131 -- write_file(worldpath .. "/entry.box", boxes.export_box(0))
132 -- write_file(worldpath .. "/exit.box", boxes.export_box(1))
133 -- write_file(worldpath .. "/box.box", boxes.export_box(2))