Make SkinDB support optional

master
Lars Mueller 2022-08-02 20:43:42 +02:00
parent f705f446b5
commit b2af386587
4 changed files with 98 additions and 65 deletions

View File

@ -165,6 +165,9 @@ Left-click to undo, right-click to redo. Undo-redo log size is limited due to [M
## Configuration
`epidermis` must be added to `secure.http_mods` for SkinDB uploading & downloading (including syncing) to be enabled;
otherwise epidermis will be limited to the local (offline, cached) SkinDB copy
<!--modlib:conf:2-->
### `skindb`

View File

@ -9,8 +9,7 @@ include"theme.lua"
include"send_notification.lua"
include"colorpicker_rgb_formspec.lua"
include"colorpicker_hsv_ingame.lua"
local http = assert(minetest.request_http_api(), "add epidermis to secure.http_mods")
assert(loadfile(modlib.mod.get_resource("skindb.lua")))(http)
assert(loadfile(modlib.mod.get_resource("skindb.lua")))(minetest.request_http_api())
include"skin.lua"
include"paintable.lua"
include"tools.lua"

View File

@ -436,33 +436,41 @@ function def:_show_control_panel(player)
tooltip = "Open texture preview"
},
},
{
{
exit = false,
name = "upload",
icon = "upload",
tooltip = "Upload to SkinDB"
},
{
exit = false,
name = "download",
icon = "download",
tooltip = "Pick from SkinDB"
}
},
{{
exit = false,
name = "delete",
icon = "bin",
tooltip = "Delete"
}},
{{
exit = true,
name = "close",
icon = "cross",
tooltip = "Close"
}}
}
local skindb_group = {}
if def._show_upload_formspec then
table.insert(skindb_group, {
exit = false,
name = "upload",
icon = "upload",
tooltip = "Upload to SkinDB"
})
end
if def._show_picker_formspec then
table.insert(skindb_group, {
exit = false,
name = "download",
icon = "download",
tooltip = "Pick from SkinDB"
})
end
if #skindb_group > 0 then
table.insert(action_groups, skindb_group)
end
table.insert(action_groups, {{
exit = false,
name = "delete",
icon = "bin",
tooltip = "Delete"
}})
table.insert(action_groups, {{
exit = true,
name = "close",
icon = "cross",
tooltip = "Close"
}})
local fs = {
false, -- placeholder
{"real_coordinates", true},
@ -504,9 +512,13 @@ function def:_show_control_panel(player)
elseif fields.delete then
self:_show_delete_formspec(player)
elseif fields.upload then
self:_show_upload_formspec(player)
if self._show_upload_formspec then
self:_show_upload_formspec(player)
end
elseif fields.download then
self:_show_picker_formspec(player)
if self._show_picker_formspec then
self:_show_picker_formspec(player)
end
end
end)
end
@ -634,6 +646,9 @@ function def:_show_upload_formspec(player, message)
}
end)
end
if not epidermis.upload then -- no SkinDB support
def._show_upload_formspec = nil
end
function def:_show_picker_formspec(player)
if #epidermis.skins == 0 then
@ -749,5 +764,8 @@ function def:_show_picker_formspec(player)
end
show_formspec()
end
if not epidermis.skins then -- no SkinDB support
def._show_picker_formspec = nil
end
moblib.register_entity("epidermis:paintable", def)

View File

@ -7,7 +7,53 @@ Assumptions:
- `GROUP BY` works like `ORDER BY` (otherwise no ordering is guaranteed)
]]
local http = assert(...)
-- Load offline copy
local texture_path = epidermis.paths.dynamic_textures.skindb
epidermis.skins = {}
local function on_local_copy_loaded() end
local function load_local_copy()
local ids = {}
for _, filename in ipairs(minetest.get_dir_list(texture_path, false)) do
local id = filename:match"^epidermis_skindb_(%d+)%.png$"
if id then
table.insert(ids, tonumber(id))
end
end
table.sort(ids)
for index, id in ipairs(ids) do
local filename = ("epidermis_skindb_%d.png"):format(id)
local path = modlib.file.concat_path{texture_path, filename}
local metafile = assert(io.open(modlib.file.concat_path{texture_path, filename .. ".json"}))
local meta = modlib.json:read_file(metafile)
metafile:close()
meta.texture = "blank.png" -- dynamic media isn't available yet
epidermis.skins[index] = meta
epidermis.dynamic_add_media(path, function()
meta.texture = filename
end, false) -- Enable caching for SkinDB skins
end
on_local_copy_loaded()
end
-- HACK wait a globalstep before loading the local copy to prevent the dynamic media join race condition in singleplayer
minetest.after(0, function()
minetest.after(0, load_local_copy)
end)
-- HTTP-requiring code
local http = ...
if not http then
function on_local_copy_loaded()
if #epidermis.skins == 0 then -- empty local copy...
epidermis.skins = nil -- ... disable skin picking entirely
end
end
return -- disable entirely
end
local base_url = "http://minetest.fensta.bplaced.net"
@ -68,40 +114,7 @@ minetest.register_privilege("epidermis_upload", {
give_to_admin = false,
})
-- "Downloading"
local texture_path = epidermis.paths.dynamic_textures.skindb
epidermis.skins = {}
local function on_local_copy_loaded() end
local function load_local_copy()
local ids = {}
for _, filename in ipairs(minetest.get_dir_list(texture_path, false)) do
local id = filename:match"^epidermis_skindb_(%d+)%.png$"
if id then
table.insert(ids, tonumber(id))
end
end
table.sort(ids)
for index, id in ipairs(ids) do
local filename = ("epidermis_skindb_%d.png"):format(id)
local path = modlib.file.concat_path{texture_path, filename}
local metafile = assert(io.open(modlib.file.concat_path{texture_path, filename .. ".json"}))
local meta = modlib.json:read_file(metafile)
metafile:close()
meta.texture = "blank.png" -- dynamic media isn't available yet
epidermis.skins[index] = meta
epidermis.dynamic_add_media(path, function()
meta.texture = filename
end, false) -- Enable caching for SkinDB skins
end
on_local_copy_loaded()
end
-- HACK wait a globalstep before loading the local copy to prevent the dynamic media join race condition in singleplayer
minetest.after(0, function()
minetest.after(0, load_local_copy)
end)
-- "Downloading" / Updating
local timeout = 10
local html_unescape = modlib.web.html.unescape