Skip to content

Commit

Permalink
remove backend-handler construct, simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jan 11, 2024
1 parent 204c613 commit 906c416
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 286 deletions.
37 changes: 0 additions & 37 deletions api.lua
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@

-- type => handler_def
local backend_handlers = {}

function mapsync.register_backend_handler(name, handler)
-- default to no-op functions
handler.validate_config = handler.validate_config or function() end
handler.init = handler.init or function() end

backend_handlers[name] = handler
end

function mapsync.select_handler(backend_def)
return backend_handlers[backend_def.type]
end

-- type => handler_def
local data_backend_handlers = {}

function mapsync.register_data_backend_handler(name, handler)
data_backend_handlers[name] = handler
end

function mapsync.select_data_handler(data_backend_def)
return data_backend_handlers[data_backend_def.type]
end

-- name => backend_def
local backends = {}

-- register a map backend
function mapsync.register_backend(name, backend_def)
local handler = mapsync.select_handler(backend_def)
if not handler then
error("unknown backend type: '" .. backend_def.type .. "' for backend '" .. name .. "'")
end

backend_def.name = name
-- default to always-on backend if no selector specified
backend_def.select = backend_def.select or function() return true end

-- validate config
handler.validate_config(backend_def)

-- init backend def
handler.init(backend_def)

-- register
backends[name] = backend_def
end
Expand Down
6 changes: 2 additions & 4 deletions api.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ mtt.register("backend selection", function(callback)
})

local backend_def = mapsync.select_backend({x=0, y=0, z=0})
local handler = mapsync.select_handler(backend_def)

assert(handler)
assert(backend_def.name == "my-backend")
assert(handler.save_chunk(backend_def, {x=0, y=0, z=0}))

assert(mapsync.save({x=0, y=0, z=0}))

backend_def = mapsync.select_backend({x=0, y=10, z=0})
assert(not backend_def)
Expand Down
44 changes: 0 additions & 44 deletions backend_fs.lua

This file was deleted.

160 changes: 0 additions & 160 deletions backend_patch.lua

This file was deleted.

17 changes: 10 additions & 7 deletions data.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local global_env = ...

function mapsync.save_data(key, value)
local data_backend_def = mapsync.get_data_backend()
Expand All @@ -6,10 +7,9 @@ function mapsync.save_data(key, value)
return
end

local data_handler = mapsync.select_data_handler(data_backend_def)
if data_handler then
data_handler.save_data(data_backend_def, key, value)
end
local f = assert(global_env.io.open(data_backend_def.path .. "/" .. key .. ".lua", "w"))
f:write(minetest.serialize(value))
f:close()
end

function mapsync.load_data(key)
Expand All @@ -19,8 +19,11 @@ function mapsync.load_data(key)
return
end

local data_handler = mapsync.select_data_handler(data_backend_def)
if data_handler then
return data_handler.load_data(data_backend_def, key)
local f = global_env.io.open(data_backend_def.path .. "/" .. key .. ".lua", "r")
if not f then
return
end
local value = minetest.deserialize(f:read("*all"))
f:close()
return value
end
12 changes: 9 additions & 3 deletions functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ function mapsync.get_backend_chunk_mtime(chunk_pos)
return
end

local handler = mapsync.select_handler(backend_def)

-- get manifest
local manifest = handler.get_manifest(backend_def, chunk_pos)
local manifest = mapsync.get_manifest(mapsync.get_chunk_zip_path(backend_def.path, chunk_pos))
if not manifest then
return
end
Expand Down Expand Up @@ -151,6 +149,14 @@ function mapsync.deep_compare(tbl1, tbl2)
return false
end

function mapsync.get_chunk_json_path(prefix, chunk_pos)
return prefix .. "/chunk_" .. minetest.pos_to_string(chunk_pos) .. ".json"
end

function mapsync.get_chunk_zip_path(prefix, chunk_pos)
return prefix .. "/chunk_" .. minetest.pos_to_string(chunk_pos) .. ".zip"
end

-- https://gist.github.com/SafeteeWoW/080e784e5ebfda42cad486c58e6d26e4
-- license: zlib

Expand Down
14 changes: 6 additions & 8 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,31 @@ end
dofile(MP.."/api.lua")
dofile(MP.."/privs.lua")

-- backends
loadfile(MP.."/backend_fs.lua")(global_env)
loadfile(MP.."/backend_patch.lua")(global_env)

-- utilities / helpers
dofile(MP.."/pos_iterator.lua")
dofile(MP.."/encoding.lua")
dofile(MP.."/serialize_mapblock.lua")
dofile(MP.."/deserialize_mapblock.lua")
dofile(MP.."/localize_nodeids.lua")
dofile(MP.."/functions.lua")

-- diff / patch
dofile(MP.."/create_diff.lua")
dofile(MP.."/apply_diff.lua")
loadfile(MP.."/patch.lua")(global_env)

-- save/load
dofile(MP.."/auto_save.lua")
dofile(MP.."/auto_update.lua")
dofile(MP.."/save.lua")
loadfile(MP.."/save.lua")(global_env)
loadfile(MP.."/data.lua")(global_env)
dofile(MP.."/load.lua")
dofile(MP.."/mapgen.lua")
dofile(MP.."/data.lua")

-- hud stuff
dofile(MP.."/hud.lua")

-- pass on global env (secure/insecure)
loadfile(MP.."/functions.lua")(global_env)
loadfile(MP.."/serialize_chunk.lua")(global_env)
loadfile(MP.."/parse_chunk.lua")(global_env)
loadfile(MP.."/deserialize_chunk.lua")(global_env)
Expand All @@ -72,7 +70,7 @@ if minetest.get_modpath("mtt") and mtt.enabled then
dofile(MP.."/crypto.spec.lua")
dofile(MP.."/data.spec.lua")
dofile(MP.."/diff.spec.lua")
dofile(MP.."/backend_patch.spec.lua")
dofile(MP.."/patch.spec.lua")
dofile(MP.."/api.spec.lua")
dofile(MP.."/serialize_chunk.spec.lua")
end
Loading

0 comments on commit 906c416

Please sign in to comment.