Skip to content

Commit 214da7b

Browse files
committedSep 10, 2013
Use the Settings Lua interface to read world.mt
1 parent dd5c451 commit 214da7b

File tree

1 file changed

+46
-72
lines changed

1 file changed

+46
-72
lines changed
 

‎builtin/modmgr.lua

+46-72
Original file line numberDiff line numberDiff line change
@@ -498,36 +498,18 @@ function modmgr.get_worldconfig(worldpath)
498498
local filename = worldpath ..
499499
DIR_DELIM .. "world.mt"
500500

501-
local worldfile = io.open(filename,"r")
501+
local worldfile = Settings(filename)
502502

503503
local worldconfig = {}
504504
worldconfig.global_mods = {}
505505
worldconfig.game_mods = {}
506506

507-
if worldfile then
508-
local dependency = worldfile:read("*l")
509-
while dependency do
510-
local parts = dependency:split("=")
511-
512-
local key = parts[1]:trim()
513-
514-
if key == "gameid" then
515-
worldconfig.id = parts[2]:trim()
516-
elseif key == "backend" then
517-
worldconfig.backend = parts[2]:trim()
518-
else
519-
local key = parts[1]:trim():sub(10)
520-
if parts[2]:trim() == "true" then
521-
worldconfig.global_mods[key] = true
522-
else
523-
worldconfig.global_mods[key] = false
524-
end
525-
end
526-
dependency = worldfile:read("*l")
507+
for key,value in pairs(worldfile:to_table()) do
508+
if key == "gameid" then
509+
worldconfig.id = value
510+
else
511+
worldconfig.global_mods[key] = engine.is_yes(value)
527512
end
528-
worldfile:close()
529-
else
530-
print("Modmgr: " .. filename .. " not found")
531513
end
532514

533515
--read gamemods
@@ -727,29 +709,34 @@ function modmgr.handle_configure_world_buttons(fields)
727709

728710
local filename = worldspec.path ..
729711
DIR_DELIM .. "world.mt"
730-
731-
local worldfile = io.open(filename,"w")
732712

733-
if worldfile then
734-
worldfile:write("gameid = " .. modmgr.worldconfig.id .. "\nbackend = " .. modmgr.worldconfig.backend .. "\n")
735-
736-
local rawlist = filterlist.get_raw_list(modmgr.modlist)
737-
738-
for i=1,#rawlist,1 do
739-
740-
if not rawlist[i].is_modpack and
741-
rawlist[i].typ ~= "game_mod" then
742-
if rawlist[i].enabled then
743-
worldfile:write("load_mod_" .. rawlist[i].name .. " = true" .. "\n")
744-
else
745-
worldfile:write("load_mod_" .. rawlist[i].name .. " = false" .. "\n")
746-
end
713+
local worldfile = Settings(filename)
714+
local mods = worldfile:to_table()
715+
716+
local rawlist = filterlist.get_raw_list(modmgr.modlist)
717+
718+
local i,mod
719+
for i,mod in ipairs(rawlist) do
720+
if not mod.is_modpack and
721+
mod.typ ~= "game_mod" then
722+
if mod.enabled then
723+
worldfile:set("load_mod_"..mod.name, "true")
724+
else
725+
worldfile:set("load_mod_"..mod.name, "false")
747726
end
727+
mods["load_mod_"..mod.name] = nil
748728
end
749-
750-
worldfile:close()
751-
else
752-
print("failed to open world config file")
729+
end
730+
731+
-- Remove mods that are not present anymore
732+
for key,value in pairs(mods) do
733+
if key:sub(1,9) == "load_mod_" then
734+
worldfile:remove(key)
735+
end
736+
end
737+
738+
if not worldfile:write() then
739+
print("failed to write world config file")
753740
end
754741

755742
modmgr.modlist = nil
@@ -888,37 +875,24 @@ function modmgr.preparemodlist(data)
888875
local filename = data.worldpath ..
889876
DIR_DELIM .. "world.mt"
890877

891-
local worldfile = io.open(filename,"r")
892-
if worldfile then
893-
local dependency = worldfile:read("*l")
894-
while dependency do
895-
local parts = dependency:split("=")
896-
897-
local key = parts[1]:trim()
898-
899-
if key ~= "gameid" then
900-
local key = parts[1]:trim():sub(10)
901-
local element = nil
902-
for i=1,#retval,1 do
903-
if retval[i].name == key then
904-
element = retval[i]
905-
break
906-
end
907-
end
908-
if element ~= nil then
909-
if parts[2]:trim() == "true" then
910-
element.enabled = true
911-
else
912-
element.enabled = false
913-
end
914-
else
915-
print("Mod: " .. key .. " " .. dump(parts[2]) .. " but not found")
878+
local worldfile = Settings(filename)
879+
880+
for key,value in pairs(worldfile:to_table()) do
881+
if key:sub(1, 9) == "load_mod_" then
882+
key = key:sub(10)
883+
local element = nil
884+
for i=1,#retval,1 do
885+
if retval[i].name == key then
886+
element = retval[i]
887+
break
916888
end
917889
end
918-
dependency = worldfile:read("*l")
890+
if element ~= nil then
891+
element.enabled = engine.is_yes(value)
892+
else
893+
print("Mod: " .. key .. " " .. dump(value) .. " but not found")
894+
end
919895
end
920-
worldfile:close()
921-
922896
end
923897

924898
return retval

0 commit comments

Comments
 (0)
Please sign in to comment.