Skip to content

Commit

Permalink
Add HTTP API to main menu (#9998)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed Jun 6, 2020
1 parent 7ec0e3d commit 60bab8b
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 113 deletions.
19 changes: 19 additions & 0 deletions builtin/fstk/dialog.lua
Expand Up @@ -67,3 +67,22 @@ function dialog_create(name,get_formspec,buttonhandler,eventhandler)
ui.add(self)
return self
end

function messagebox(name, message)
return dialog_create(name,
function()
return ([[
formspec_version[3]
size[8,3]
textarea[0.375,0.375;7.25,1.2;;;%s]
button[3,1.825;2,0.8;ok;%s]
]]):format(message, fgettext("OK"))
end,
function(this, fields)
if fields.ok then
this:delete()
return true
end
end,
nil)
end
2 changes: 1 addition & 1 deletion builtin/fstk/ui.lua
Expand Up @@ -85,7 +85,7 @@ function ui.update()
"box[0.5,1.2;13,5;#000]",
("textarea[0.5,1.2;13,5;;%s;%s]"):format(
error_title, error_message),
"button[5,6.6;4,1;btn_error_confirm;" .. fgettext("Ok") .. "]"
"button[5,6.6;4,1;btn_error_confirm;" .. fgettext("OK") .. "]"
}
else
local active_toplevel_ui_elements = 0
Expand Down
1 change: 1 addition & 0 deletions builtin/init.lua
Expand Up @@ -36,6 +36,7 @@ dofile(commonpath .. "misc_helpers.lua")

if INIT == "game" then
dofile(gamepath .. "init.lua")
assert(not core.get_http_api)
elseif INIT == "mainmenu" then
local mm_script = core.settings:get("main_menu_script")
if mm_script and mm_script ~= "" then
Expand Down
49 changes: 26 additions & 23 deletions builtin/mainmenu/dlg_contentstore.lua
@@ -1,5 +1,5 @@
--Minetest
--Copyright (C) 2018 rubenwardy
--Copyright (C) 2018-20 rubenwardy
--
--This program is free software; you can redistribute it and/or modify
--it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -15,8 +15,18 @@
--with this program; if not, write to the Free Software Foundation, Inc.,
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

if not minetest.get_http_api then
function create_store_dlg()
return messagebox("store",
fgettext("ContentDB is not available when Minetest was compiled without cURL"))
end
return
end

local store = { packages = {}, packages_full = {} }

local http = minetest.get_http_api()

-- Screenshot
local screenshot_dir = core.get_cache_path() .. DIR_DELIM .. "cdb"
assert(core.create_dir(screenshot_dir))
Expand Down Expand Up @@ -171,11 +181,6 @@ local function get_screenshot(package)
end

function store.load()
local tmpdir = os.tempfolder()
local target = tmpdir .. DIR_DELIM .. "packages.json"

assert(core.create_dir(tmpdir))

local version = core.get_version()
local base_url = core.settings:get("contentdb_url")
local url = base_url ..
Expand All @@ -189,31 +194,29 @@ function store.load()
end
end

core.download_file(url, target)
local timeout = tonumber(minetest.settings:get("curl_file_download_timeout"))
local response = http.fetch_sync({ url = url, timeout = timeout })
if not response.succeeded then
return
end

local file = io.open(target, "r")
if file then
store.packages_full = core.parse_json(file:read("*all")) or {}
file:close()
store.packages_full = core.parse_json(response.data) or {}

for _, package in pairs(store.packages_full) do
package.url = base_url .. "/packages/" ..
for _, package in pairs(store.packages_full) do
package.url = base_url .. "/packages/" ..
package.author .. "/" .. package.name ..
"/releases/" .. package.release .. "/download/"

local name_len = #package.name
if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
package.id = package.author:lower() .. "/" .. package.name:sub(1, name_len - 5)
else
package.id = package.author:lower() .. "/" .. package.name
end
local name_len = #package.name
if package.type == "game" and name_len > 5 and package.name:sub(name_len - 4) == "_game" then
package.id = package.author:lower() .. "/" .. package.name:sub(1, name_len - 5)
else
package.id = package.author:lower() .. "/" .. package.name
end

store.packages = store.packages_full
store.loaded = true
end

core.delete_dir(tmpdir)
store.packages = store.packages_full
store.loaded = true
end

function store.update_paths()
Expand Down
7 changes: 5 additions & 2 deletions doc/fst_api.txt
Expand Up @@ -101,6 +101,9 @@ dialog_create(name, cbf_formspec, cbf_button_handler, cbf_events)
^ cbf_events: function to handle events
function(dialog, event)

messagebox(name, message)
^ creates a message dialog

Class reference dialog:

methods:
Expand All @@ -113,13 +116,13 @@ methods:
^ hide dialog
- delete()
^ delete dialog from ui

members:
- data
^ variable data attached to this dialog
- parent
^ parent component to return to on exit

File: fst/buttonbar.lua
-----------------------

Expand Down

0 comments on commit 60bab8b

Please sign in to comment.