diff --git a/api.async.lua b/api.async.lua new file mode 100644 index 0000000..2f0e77a --- /dev/null +++ b/api.async.lua @@ -0,0 +1,8 @@ + +mapsync = { + async_env = true +} + +function mapsync.select_backend(chunk_pos) + return minetest.ipc_get("mapsync:backend") +end diff --git a/api.lua b/api.lua index cf09253..f4df56a 100644 --- a/api.lua +++ b/api.lua @@ -17,6 +17,14 @@ function mapsync.register_backend(name, backend_def) -- register backends[name] = backend_def + if minetest.ipc_set then + -- register async + minetest.ipc_set("mapsync:backend", { + type = backend_def.type, + name = backend_def.name, + path = backend_def.path + }) + end end -- unregisters a backend @@ -64,4 +72,4 @@ end function mapsync.get_data_backend() return data_backend_def -end \ No newline at end of file +end diff --git a/data.spec.lua b/data.spec.lua index f45420a..1cdc8ee 100644 --- a/data.spec.lua +++ b/data.spec.lua @@ -21,7 +21,6 @@ mtt.register("data", function(callback) assert(mapsync.get_data_file("myfile2.txt", "w")) -- write to data file - assert(not mapsync.get_data_file("myfile3.txt")) local f = mapsync.get_data_file("myfile3.txt", "w") assert(f) f:write("stuff") diff --git a/deserialize_chunk.lua b/deserialize_chunk.lua index 263b162..b80c5d2 100644 --- a/deserialize_chunk.lua +++ b/deserialize_chunk.lua @@ -1,4 +1,4 @@ -local global_env = ... +local global_env = ... or _G local param1, param2, node_data @@ -70,4 +70,4 @@ function mapsync.get_key_manifest(filename) return false, m_err_msg end return minetest.parse_json(key_str) -end \ No newline at end of file +end diff --git a/init.lua b/init.lua index f6b3a11..b22a078 100644 --- a/init.lua +++ b/init.lua @@ -40,7 +40,24 @@ dofile(MP.."/auto_update.lua") dofile(MP.."/save.lua") loadfile(MP.."/data.lua")(global_env) dofile(MP.."/load.lua") -dofile(MP.."/mapgen.lua") + +if not minetest.register_mapgen_script then + -- sync mapgen + dofile(MP.."/mapgen.lua") +else + -- async mapgen + minetest.register_mapgen_script(MP.."/api.async.lua") + minetest.register_mapgen_script(MP.."/pos_iterator.lua") + minetest.register_mapgen_script(MP.."/encoding.lua") + minetest.register_mapgen_script(MP.."/functions.lua") + minetest.register_mapgen_script(MP.."/mapgen.async.lua") + + minetest.register_mapgen_script(MP.."/load.lua") + minetest.register_mapgen_script(MP.."/parse_chunk.lua") + minetest.register_mapgen_script(MP.."/deserialize_chunk.lua") + minetest.register_mapgen_script(MP.."/deserialize_mapblock.lua") + minetest.register_mapgen_script(MP.."/localize_nodeids.lua") +end -- hud stuff dofile(MP.."/hud.lua") @@ -69,9 +86,9 @@ end -- testing if minetest.get_modpath("mtt") and mtt.enabled then - dofile(MP.."/init.spec.lua") dofile(MP.."/functions.spec.lua") dofile(MP.."/data.spec.lua") dofile(MP.."/api.spec.lua") dofile(MP.."/serialize_chunk.spec.lua") -end \ No newline at end of file + dofile(MP.."/mapgen.spec.lua") +end diff --git a/init.spec.lua b/init.spec.lua deleted file mode 100644 index 6ea8cc6..0000000 --- a/init.spec.lua +++ /dev/null @@ -1,2 +0,0 @@ -local pos = { x=0, y=0, z=0 } -mtt.emerge_area(pos, pos) \ No newline at end of file diff --git a/mapgen.async.lua b/mapgen.async.lua new file mode 100644 index 0000000..bef4380 --- /dev/null +++ b/mapgen.async.lua @@ -0,0 +1,16 @@ +minetest.register_on_generated(function(vmanip, minp) + local chunk_pos = mapsync.get_chunkpos(minp) + + local t1 = minetest.get_us_time() + mapsync.load(chunk_pos, vmanip) + local t2 = minetest.get_us_time() + + local micros = t2 - t1 + if micros > 0 then + -- log slow chunks + minetest.log( + "action", + "[mapsync] async emerge of " .. minetest.pos_to_string(chunk_pos) .. " took " .. micros .. " us" + ) + end +end) diff --git a/mapgen.lua b/mapgen.lua index 04b2043..7bcfbf5 100644 --- a/mapgen.lua +++ b/mapgen.lua @@ -14,4 +14,4 @@ minetest.register_on_generated(function(minp) "[mapsync] emerge of " .. minetest.pos_to_string(chunk_pos) .. " took " .. micros .. " us" ) end -end) \ No newline at end of file +end) diff --git a/mapgen.spec.lua b/mapgen.spec.lua new file mode 100644 index 0000000..86b3bea --- /dev/null +++ b/mapgen.spec.lua @@ -0,0 +1,18 @@ +local BACKEND_NAME = "test-mapgen-backend" + +mtt.register("mapgen-test (register)", function(callback) + -- backend for mapgen test + mapsync.register_backend(BACKEND_NAME, { + type = "fs", + path = minetest.get_modpath("mapsync") .. "/test/map" + }) + callback() +end) + +local pos = { x=0, y=0, z=0 } +mtt.emerge_area(pos, pos) + +mtt.register("mapgen-test (unregister)", function(callback) + mapsync.unregister_backend(BACKEND_NAME) + callback() +end) \ No newline at end of file diff --git a/parse_chunk.lua b/parse_chunk.lua index 0824d20..cb6b0f2 100644 --- a/parse_chunk.lua +++ b/parse_chunk.lua @@ -1,4 +1,4 @@ -local global_env = ... +local global_env = ... or _G -- local vars for faster access local insert, byte, decode_uint16 = table.insert, string.byte, mapsync.decode_uint16 @@ -65,4 +65,4 @@ function mapsync.parse_chunk(filename) end return chunk -end \ No newline at end of file +end diff --git a/test/map/chunk_(0,0,0).zip b/test/map/chunk_(0,0,0).zip new file mode 100644 index 0000000..d518aca Binary files /dev/null and b/test/map/chunk_(0,0,0).zip differ