Skip to content

Commit 3eb363f

Browse files
authored
Add updating to online content browser
1 parent 45e4829 commit 3eb363f

File tree

11 files changed

+192
-103
lines changed

11 files changed

+192
-103
lines changed

builtin/mainmenu/dlg_contentstore.lua

+71-35
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@
1515
--with this program; if not, write to the Free Software Foundation, Inc.,
1616
--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717

18+
local store = {}
19+
local package_dialog = {}
20+
21+
local search_string = ""
22+
local cur_page = 1
23+
local num_per_page = 5
24+
local filter_type = 1
25+
local filter_types_titles = {
26+
fgettext("All packages"),
27+
fgettext("Games"),
28+
fgettext("Mods"),
29+
fgettext("Texture packs"),
30+
}
31+
32+
local filter_types_type = {
33+
nil,
34+
"game",
35+
"mod",
36+
"txp",
37+
}
38+
39+
40+
41+
1842
local function download_package(param)
1943
if core.download_file(param.package.url, param.filename) then
2044
return {
@@ -39,7 +63,9 @@ local function start_install(calling_dialog, package)
3963

4064
local function callback(result)
4165
if result.successful then
42-
local path, msg = pkgmgr.install(result.package.type, result.filename, result.package.name)
66+
local path, msg = pkgmgr.install(result.package.type,
67+
result.filename, result.package.name,
68+
result.package.path)
4369
if not path then
4470
gamedata.errormessage = msg
4571
else
@@ -69,6 +95,7 @@ local function start_install(calling_dialog, package)
6995
end
7096
set_def("description", result.package.short_description)
7197
set_def("author", result.package.author)
98+
conf:set("release", result.package.release)
7299
conf:write()
73100
end
74101
end
@@ -85,7 +112,7 @@ local function start_install(calling_dialog, package)
85112
end
86113

87114
if not core.handle_async(download_package, params, callback) then
88-
minetest.log("error", "ERROR: async event failed")
115+
core.log("error", "ERROR: async event failed")
89116
gamedata.errormessage = fgettext("Failed to download $1", package.name)
90117
end
91118

@@ -111,19 +138,35 @@ local function start_install(calling_dialog, package)
111138
end
112139

113140

114-
local package_dialog = {}
115-
116141
function package_dialog.get_formspec()
117142
local package = package_dialog.package
118143

144+
store.update_paths()
145+
119146
local formspec = {
120-
"size[8,4;true]",
147+
"size[9,4;true]",
121148
"label[2.5,0.2;", core.formspec_escape(package.title), "]",
122-
"textarea[0.2,1;8,3;;;", core.formspec_escape(package.short_description), "]",
149+
"textarea[0.2,1;9,3;;;", core.formspec_escape(package.short_description), "]",
123150
"button[0,0;2,1;back;", fgettext("Back"), "]",
124-
"button[6,0;2,1;install;", fgettext("Install"), "]",
125151
}
126152

153+
if not package.path then
154+
formspec[#formspec + 1] = "button[7,0;2,1;install;"
155+
formspec[#formspec + 1] = fgettext("Install")
156+
formspec[#formspec + 1] = "]"
157+
elseif package.installed_release < package.release then
158+
formspec[#formspec + 1] = "button[7,0;2,1;install;"
159+
formspec[#formspec + 1] = fgettext("Update")
160+
formspec[#formspec + 1] = "]"
161+
formspec[#formspec + 1] = "button[7,1;2,1;uninstall;"
162+
formspec[#formspec + 1] = fgettext("Uninstall")
163+
formspec[#formspec + 1] = "]"
164+
else
165+
formspec[#formspec + 1] = "button[7,0;2,1;uninstall;"
166+
formspec[#formspec + 1] = fgettext("Uninstall")
167+
formspec[#formspec + 1] = "]"
168+
end
169+
127170
-- TODO: screenshots
128171

129172
return table.concat(formspec, "")
@@ -136,7 +179,15 @@ function package_dialog.handle_submit(this, fields, tabname, tabdata)
136179
end
137180

138181
if fields.install then
139-
start_install(package_dialog.package)
182+
start_install(this, package_dialog.package)
183+
return true
184+
end
185+
186+
if fields.uninstall then
187+
local dlg_delmod = create_delete_content_dlg(package_dialog.package)
188+
dlg_delmod:set_parent(this)
189+
this:hide()
190+
dlg_delmod:show()
140191
return true
141192
end
142193

@@ -151,28 +202,6 @@ function package_dialog.create(package)
151202
nil)
152203
end
153204

154-
155-
156-
157-
local store = {}
158-
local search_string = ""
159-
local cur_page = 1
160-
local num_per_page = 5
161-
local filter_type = 1
162-
local filter_types_titles = {
163-
fgettext("All packages"),
164-
fgettext("Games"),
165-
fgettext("Mods"),
166-
fgettext("Texture packs"),
167-
}
168-
169-
local filter_types_type = {
170-
nil,
171-
"game",
172-
"mod",
173-
"txp",
174-
}
175-
176205
function store.load()
177206
store.packages_full = core.get_package_list()
178207
store.packages = store.packages_full
@@ -209,6 +238,7 @@ function store.update_paths()
209238

210239
if content and content.author == package.author then
211240
package.path = content.path
241+
package.installed_release = content.release
212242
else
213243
package.path = nil
214244
end
@@ -301,17 +331,23 @@ function store.get_formspec()
301331
formspec[#formspec + 1] = "]"
302332

303333
-- buttons
304-
if package.path then
305-
formspec[#formspec + 1] = "button[6,0;1.5,1;uninstall_"
334+
if not package.path then
335+
formspec[#formspec + 1] = "button[6,0;1.5,1;install_"
306336
formspec[#formspec + 1] = tostring(i)
307337
formspec[#formspec + 1] = ";"
308-
formspec[#formspec + 1] = fgettext("Uninstall")
338+
formspec[#formspec + 1] = fgettext("Install")
309339
formspec[#formspec + 1] = "]"
310-
else
340+
elseif package.installed_release < package.release then
311341
formspec[#formspec + 1] = "button[6,0;1.5,1;install_"
312342
formspec[#formspec + 1] = tostring(i)
313343
formspec[#formspec + 1] = ";"
314-
formspec[#formspec + 1] = fgettext("Install")
344+
formspec[#formspec + 1] = fgettext("Update")
345+
formspec[#formspec + 1] = "]"
346+
else
347+
formspec[#formspec + 1] = "button[6,0;1.5,1;uninstall_"
348+
formspec[#formspec + 1] = tostring(i)
349+
formspec[#formspec + 1] = ";"
350+
formspec[#formspec + 1] = fgettext("Uninstall")
315351
formspec[#formspec + 1] = "]"
316352
end
317353
formspec[#formspec + 1] = "button[7.5,0;1.5,1;view_"

builtin/mainmenu/pkgmgr.lua

+68-43
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function get_mods(path,retval,modpack)
3232

3333
toadd.name = name
3434
toadd.author = mod_conf.author
35+
toadd.release = tonumber(mod_conf.release or "0")
3536
toadd.path = prefix
3637
toadd.type = "mod"
3738

@@ -74,6 +75,7 @@ function pkgmgr.get_texture_packs()
7475
retval[#retval + 1] = {
7576
name = item,
7677
author = conf:get("author"),
78+
release = tonumber(conf:get("release") or "0"),
7779
list_name = name,
7880
type = "txp",
7981
path = path,
@@ -336,90 +338,113 @@ function pkgmgr.get_worldconfig(worldpath)
336338
end
337339

338340
--------------------------------------------------------------------------------
339-
function pkgmgr.install_dir(type, path, basename)
341+
function pkgmgr.install_dir(type, path, basename, targetpath)
340342
local basefolder = pkgmgr.get_base_folder(path)
341343

342-
local targetpath
344+
-- There's no good way to detect a texture pack, so let's just assume
345+
-- it's correct for now.
343346
if type == "txp" then
344347
if basefolder and basefolder.type ~= "invalid" and basefolder.type ~= "txp" then
345348
return nil, fgettext("Unable to install a $1 as a texture pack", basefolder.type)
346349
end
347350

348351
local from = basefolder and basefolder.path or path
349-
targetpath = core.get_texturepath() .. DIR_DELIM .. basename
350-
core.copy_dir(from, targetpath)
352+
if targetpath then
353+
core.delete_dir(targetpath)
354+
core.create_dir(targetpath)
355+
else
356+
targetpath = core.get_texturepath() .. DIR_DELIM .. basename
357+
end
358+
if not core.copy_dir(from, targetpath) then
359+
return nil,
360+
fgettext("Failed to install $1 to $2", basename, targetpath)
361+
end
351362
return targetpath, nil
352363

353364
elseif not basefolder then
354365
return nil, fgettext("Unable to find a valid mod or modpack")
355366
end
356367

368+
--
369+
-- Get destination
370+
--
357371
if basefolder.type == "modpack" then
358372
if type ~= "mod" then
359373
return nil, fgettext("Unable to install a modpack as a $1", type)
360374
end
361-
local clean_path = nil
362375

363-
if basename ~= nil then
364-
clean_path = "mp_" .. basename
365-
end
366-
367-
if clean_path == nil then
368-
clean_path = get_last_folder(cleanup_path(basefolder.path))
369-
end
370-
371-
if clean_path ~= nil then
372-
targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
373-
if not core.copy_dir(basefolder.path,targetpath) then
376+
-- Get destination name for modpack
377+
if targetpath then
378+
core.delete_dir(targetpath)
379+
core.create_dir(targetpath)
380+
else
381+
local clean_path = nil
382+
if basename ~= nil then
383+
clean_path = "mp_" .. basename
384+
end
385+
if not clean_path then
386+
clean_path = get_last_folder(cleanup_path(basefolder.path))
387+
end
388+
if clean_path then
389+
targetpath = core.get_modpath() .. DIR_DELIM .. clean_path
390+
else
374391
return nil,
375-
fgettext("Failed to install $1 to $2", basename, targetpath)
392+
fgettext("Install Mod: unable to find suitable foldername for modpack $1",
393+
modfilename)
376394
end
377-
else
378-
return nil,
379-
fgettext("Install Mod: unable to find suitable foldername for modpack $1",
380-
modfilename)
381395
end
382-
383-
pkgmgr.refresh_globals()
384-
385396
elseif basefolder.type == "mod" then
386397
if type ~= "mod" then
387398
return nil, fgettext("Unable to install a mod as a $1", type)
388399
end
389-
local targetfolder = basename
390400

391-
if targetfolder == nil then
392-
targetfolder = pkgmgr.identify_modname(basefolder.path,"init.lua")
393-
end
401+
if targetpath then
402+
core.delete_dir(targetpath)
403+
core.create_dir(targetpath)
404+
else
405+
local targetfolder = basename
406+
if targetfolder == nil then
407+
targetfolder = pkgmgr.identify_modname(basefolder.path, "init.lua")
408+
end
394409

395-
--if heuristic failed try to use current foldername
396-
if targetfolder == nil then
397-
targetfolder = get_last_folder(basefolder.path)
398-
end
410+
-- If heuristic failed try to use current foldername
411+
if targetfolder == nil then
412+
targetfolder = get_last_folder(basefolder.path)
413+
end
399414

400-
if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
401-
targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
402-
core.copy_dir(basefolder.path, targetpath)
403-
else
404-
return nil, fgettext("Install Mod: unable to find real modname for: $1", modfilename)
415+
if targetfolder ~= nil and pkgmgr.isValidModname(targetfolder) then
416+
targetpath = core.get_modpath() .. DIR_DELIM .. targetfolder
417+
else
418+
return nil, fgettext("Install Mod: unable to find real modname for: $1", modfilename)
419+
end
405420
end
406421

407-
pkgmgr.refresh_globals()
408-
409422
elseif basefolder.type == "game" then
410423
if type ~= "game" then
411424
return nil, fgettext("Unable to install a game as a $1", type)
412425
end
413426

414-
targetpath = core.get_gamepath() .. DIR_DELIM .. basename
415-
core.copy_dir(basefolder.path, targetpath)
427+
if targetpath then
428+
core.delete_dir(targetpath)
429+
core.create_dir(targetpath)
430+
else
431+
targetpath = core.get_gamepath() .. DIR_DELIM .. basename
432+
end
433+
end
434+
435+
-- Copy it
436+
if not core.copy_dir(basefolder.path, targetpath) then
437+
return nil,
438+
fgettext("Failed to install $1 to $2", basename, targetpath)
416439
end
417440

441+
pkgmgr.refresh_globals()
442+
418443
return targetpath, nil
419444
end
420445

421446
--------------------------------------------------------------------------------
422-
function pkgmgr.install(type, modfilename, basename)
447+
function pkgmgr.install(type, modfilename, basename, dest)
423448
local archive_info = pkgmgr.identify_filetype(modfilename)
424449
local path = pkgmgr.extract(archive_info)
425450

@@ -430,7 +455,7 @@ function pkgmgr.install(type, modfilename, basename)
430455
archive_info.type)
431456
end
432457

433-
local targetpath, msg = pkgmgr.install_dir(type, path, basename)
458+
local targetpath, msg = pkgmgr.install_dir(type, path, basename, dest)
434459
core.delete_dir(path)
435460
return targetpath, msg
436461
end

src/content/content.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ void parseContentInfo(ContentSpec &spec)
9898

9999
if (conf.exists("author"))
100100
spec.author = conf.get("author");
101+
102+
if (conf.exists("release"))
103+
spec.release = conf.getS32("release");
101104
}
102105

103106
if (spec.desc.empty()) {

src/content/content.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2020
#pragma once
2121
#include "config.h"
2222
#include "convert_json.h"
23+
#include "irrlichttypes.h"
2324

2425
struct ContentSpec
2526
{
2627
std::string type;
2728
std::string author;
29+
u32 release = 0;
2830
std::string name;
2931
std::string desc;
3032
std::string path;

src/content/mods.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ void parseModContents(ModSpec &spec)
5656
if (info.exists("author"))
5757
spec.author = info.get("author");
5858

59+
if (info.exists("release"))
60+
spec.release = info.getS32("release");
61+
5962
spec.depends.clear();
6063
spec.optdepends.clear();
6164
spec.is_modpack = false;

0 commit comments

Comments
 (0)