Skip to content

Commit 52beaff

Browse files
sapierPilzAdam
sapier
authored andcommittedJul 11, 2013
Worldlist fixes
Add alphabeticaly sorted worldlists Select world after creation Move worldlist handling to separate file Merge world selection of singleplayer/server tab Remove some useless code
1 parent e8f201c commit 52beaff

File tree

2 files changed

+309
-199
lines changed

2 files changed

+309
-199
lines changed
 

Diff for: ‎builtin/mainmenu.lua

+98-199
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ os.setlocale("C", "numeric")
22

33
local scriptpath = engine.get_scriptdir()
44

5+
dofile(scriptpath .. DIR_DELIM .. "mainmenu_worldlist.lua")
56
dofile(scriptpath .. DIR_DELIM .. "modmgr.lua")
67
dofile(scriptpath .. DIR_DELIM .. "modstore.lua")
78
dofile(scriptpath .. DIR_DELIM .. "gamemgr.lua")
@@ -151,12 +152,16 @@ end
151152

152153
--------------------------------------------------------------------------------
153154
function menu.update_gametype()
155+
156+
157+
154158
if (menu.game_last_check == nil or
155159
menu.game_last_check ~= menu.last_game) and
156160
tabbuilder.current_tab == "singleplayer" then
157161

158162
local gamedetails = menu.lastgame()
159163
engine.set_topleft_text(gamedetails.name)
164+
worldlist.set_gamefilter(gamedetails.id)
160165

161166
--background
162167
local background_set = false
@@ -198,6 +203,7 @@ end
198203

199204
--------------------------------------------------------------------------------
200205
function menu.reset_gametype()
206+
worldlist.set_gamefilter(nil)
201207
menu.game_last_check = nil
202208

203209
local path_background_texture = menu.basetexturedir .. "menu_background.png"
@@ -295,77 +301,30 @@ function update_menu()
295301
end
296302

297303
--------------------------------------------------------------------------------
298-
function menu.filtered_game_list()
304+
function menu.render_world_list()
299305
local retval = ""
300-
301-
local current_game = menu.lastgame()
302306

303-
for i=1,#menu.worldlist,1 do
304-
if menu.worldlist[i].gameid == current_game.id then
305-
if retval ~= "" then
306-
retval = retval ..","
307-
end
308-
309-
retval = retval .. menu.worldlist[i].name ..
310-
" \\[" .. menu.worldlist[i].gameid .. "\\]"
311-
end
312-
end
307+
local current_worldlist = worldlist.get_list()
313308

314-
return retval
315-
end
316-
317-
--------------------------------------------------------------------------------
318-
function menu.filtered_game_list_raw()
319-
local retval = {}
320-
321-
local current_game = menu.lastgame()
322-
323-
for i=1,#menu.worldlist,1 do
324-
if menu.worldlist[i].gameid == current_game.id then
325-
table.insert(retval,menu.worldlist[i])
326-
end
327-
end
328-
329-
return retval
330-
end
331-
332-
--------------------------------------------------------------------------------
333-
function menu.filtered_index_to_plain(filtered_index)
334-
335-
local current_game = menu.lastgame()
336-
337-
local temp_idx = 0
338-
339-
if menu.worldlist == nil then
340-
return -1
341-
end
342-
343-
for i=1,#menu.worldlist,1 do
344-
if menu.worldlist[i].gameid == current_game.id then
345-
temp_idx = temp_idx +1
309+
for i,v in ipairs(current_worldlist) do
310+
if retval ~= "" then
311+
retval = retval ..","
346312
end
347313

348-
if temp_idx == filtered_index then
349-
return i
350-
end
314+
retval = retval .. v.name ..
315+
" \\[" .. v.gameid .. "\\]"
351316
end
352-
return -1
317+
318+
return retval
353319
end
354320

355321
--------------------------------------------------------------------------------
356322
function menu.init()
357323
--init menu data
358324
gamemgr.update_gamelist()
359-
360-
menu.worldlist = engine.get_worlds()
361325

362-
menu.last_world = tonumber(engine.setting_get("main_menu_last_world_idx"))
363326
menu.last_game = tonumber(engine.setting_get("main_menu_last_game_idx"))
364327

365-
if type(menu.last_world) ~= "number" then
366-
menu.last_world = 1
367-
end
368-
369328
if type(menu.last_game) ~= "number" then
370329
menu.last_game = 1
371330
end
@@ -405,41 +364,50 @@ function menu.lastgame()
405364
end
406365

407366
--------------------------------------------------------------------------------
408-
function menu.lastworld()
409-
if menu.last_world ~= nil and
410-
menu.last_world > 0 and
411-
menu.last_world <= #menu.worldlist then
412-
return menu.worldlist[menu.last_world]
367+
function menu.update_last_game()
368+
369+
local current_world = worldlist.get_raw_world(
370+
engine.setting_get("mainmenu_last_selected_world")
371+
)
372+
373+
if current_world == nil then
374+
return
413375
end
414-
415-
if #menu.worldlist >= 1 then
416-
menu.last_world = 1
417-
return menu.worldlist[menu.last_world]
376+
377+
for i=1,#gamemgr.games,1 do
378+
if gamemgr.games[i].id == current_world.gameid then
379+
menu.last_game = i
380+
engine.setting_set("main_menu_last_game_idx",menu.last_game)
381+
break
382+
end
418383
end
419-
420-
--error case!!
421-
return nil
422384
end
423385

424386
--------------------------------------------------------------------------------
425-
function menu.update_last_game(world_idx)
426-
if gamedata.selected_world <= #menu.worldlist then
427-
local world = menu.worldlist[gamedata.selected_world]
387+
function menu.handle_key_up_down(fields,textlist,settingname)
388+
389+
if fields["key_up"] then
390+
local oldidx = engine.get_textlist_index(textlist)
428391

429-
if world == nil then
430-
return
392+
if oldidx > 1 then
393+
local newidx = oldidx -1
394+
engine.setting_set(settingname,
395+
worldlist.get_engine_index(newidx))
431396
end
397+
end
398+
399+
if fields["key_down"] then
400+
local oldidx = engine.get_textlist_index(textlist)
432401

433-
for i=1,#gamemgr.games,1 do
434-
if gamemgr.games[i].id == world.gameid then
435-
menu.last_game = i
436-
engine.setting_set("main_menu_last_game_idx",menu.last_game)
437-
break
438-
end
402+
if oldidx < worldlist.size() then
403+
local newidx = oldidx + 1
404+
engine.setting_set(settingname,
405+
worldlist.get_engine_index(newidx))
439406
end
440407
end
441408
end
442409

410+
443411
--------------------------------------------------------------------------------
444412
function menubar.handle_buttons(fields)
445413
for i=1,#menubar.buttons,1 do
@@ -535,7 +503,7 @@ end
535503

536504
--------------------------------------------------------------------------------
537505
function tabbuilder.dialog_delete_world()
538-
return "label[2,2;Delete World \"" .. menu.lastworld().name .. "\"?]"..
506+
return "label[2,2;Delete World \"" .. worldlist.get_raw_list()[menu.world_to_del].name .. "\"?]"..
539507
"button[3.5,4.2;2.6,0.5;world_delete_confirm;Yes]" ..
540508
"button[6,4.2;2.8,0.5;world_delete_cancel;No]"
541509
end
@@ -594,19 +562,10 @@ function tabbuilder.handle_create_world_buttons(fields)
594562

595563
if gameindex > 0 and
596564
worldname ~= "" then
597-
menu.worldlist = engine.get_worlds()
598-
599-
local found = false
600-
for i=1,#menu.worldlist,1 do
601-
if menu.worldlist[i].name == worldname then
602-
found = true
603-
break
604-
end
605-
end
606565

607566
local message = nil
608567

609-
if not found then
568+
if not worldlist.exists(worldname) then
610569
engine.setting_set("mg_name",fields["dd_mapgen"])
611570
message = engine.create_world(worldname,gameindex)
612571
else
@@ -618,25 +577,10 @@ function tabbuilder.handle_create_world_buttons(fields)
618577
else
619578
menu.last_game = gameindex
620579
engine.setting_set("main_menu_last_game_idx",gameindex)
621-
menu.worldlist = engine.get_worlds()
622-
623-
local worldlist = menu.worldlist
624-
625-
if tabbuilder.current_tab == "singleplayer" then
626-
worldlist = menu.filtered_game_list_raw()
627-
end
628-
629-
local index = 0
630580

631-
for i=1,#worldlist,1 do
632-
if worldlist[i].name == worldname then
633-
index = i
634-
break
635-
end
636-
end
637-
638-
engine.setting_set("main_menu_singleplayer_world_idx", index)
639-
menu.last_world = index
581+
worldlist.refresh()
582+
engine.setting_set("mainmenu_last_selected_world",
583+
worldlist.engine_index_by_name(worldname))
640584
end
641585
else
642586
gamedata.errormessage = "No worldname given or no game selected"
@@ -648,6 +592,7 @@ function tabbuilder.handle_create_world_buttons(fields)
648592
return
649593
end
650594

595+
--close dialog
651596
tabbuilder.is_dialog = false
652597
tabbuilder.show_buttons = true
653598
tabbuilder.current_tab = engine.setting_get("main_menu_tab")
@@ -657,11 +602,11 @@ end
657602
function tabbuilder.handle_delete_world_buttons(fields)
658603

659604
if fields["world_delete_confirm"] then
660-
if menu.last_world > 0 and
661-
menu.last_world <= #menu.worldlist then
662-
engine.delete_world(menu.last_world)
663-
menu.worldlist = engine.get_worlds()
664-
menu.last_world = 1
605+
if menu.world_to_del > 0 and
606+
menu.world_to_del <= #worldlist.get_raw_list() then
607+
engine.delete_world(menu.world_to_del)
608+
menu.world_to_del = 0
609+
worldlist.refresh()
665610
end
666611
end
667612

@@ -811,27 +756,12 @@ function tabbuilder.handle_server_buttons(fields)
811756
world_doubleclick = true
812757
end
813758
if event.typ == "CHG" then
814-
engine.setting_set("main_menu_last_world_idx",engine.get_textlist_index("srv_worlds"))
759+
engine.setting_set("mainmenu_last_selected_world",
760+
worldlist.get_engine_index(engine.get_textlist_index("srv_worlds")))
815761
end
816762
end
817763

818-
if fields["key_up"] then
819-
local oldidx = engine.get_textlist_index("srv_worlds")
820-
821-
if oldidx > 1 then
822-
local newidx = oldidx -1
823-
engine.setting_set("main_menu_last_world_idx",newidx)
824-
end
825-
end
826-
827-
if fields["key_down"] then
828-
local oldidx = engine.get_textlist_index("srv_worlds")
829-
830-
if oldidx < #menu.worldlist then
831-
local newidx = oldidx + 1
832-
engine.setting_set("main_menu_last_world_idx",newidx)
833-
end
834-
end
764+
menu.handle_key_up_down(fields,"srv_worlds","mainmenu_last_selected_world")
835765

836766
if fields["cb_creative_mode"] then
837767
engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
@@ -845,21 +775,16 @@ function tabbuilder.handle_server_buttons(fields)
845775
engine.setting_setbool("server_announce",tabbuilder.tobool(fields["cb_server_announce"]))
846776
end
847777

848-
849778
if fields["start_server"] ~= nil or
850779
world_doubleclick or
851780
fields["key_enter"] then
852781
local selected = engine.get_textlist_index("srv_worlds")
853782
if selected > 0 then
854-
855783
gamedata.playername = fields["te_playername"]
856784
gamedata.password = fields["te_passwd"]
857785
gamedata.port = fields["te_serverport"]
858786
gamedata.address = ""
859-
gamedata.selected_world = selected
860-
861-
engine.setting_set("main_menu_tab",tabbuilder.current_tab)
862-
engine.setting_set("main_menu_last_world_idx",gamedata.selected_world)
787+
gamedata.selected_world = worldlist.get_engine_index(selected)
863788

864789
menu.update_last_game(gamedata.selected_world)
865790
engine.start()
@@ -874,24 +799,26 @@ function tabbuilder.handle_server_buttons(fields)
874799

875800
if fields["world_delete"] ~= nil then
876801
local selected = engine.get_textlist_index("srv_worlds")
877-
if selected > 0 then
878-
menu.last_world = engine.get_textlist_index("srv_worlds")
879-
if menu.lastworld() ~= nil and
880-
menu.lastworld().name ~= nil and
881-
menu.lastworld().name ~= "" then
802+
if selected > 0 and
803+
selected <= worldlist.size() then
804+
local world = worldlist.get_list()[selected]
805+
if world ~= nil and
806+
world.name ~= nil and
807+
world.name ~= "" then
808+
menu.world_to_del = worldlist.get_engine_index(selected)
882809
tabbuilder.current_tab = "dialog_delete_world"
883810
tabbuilder.is_dialog = true
884811
tabbuilder.show_buttons = false
885812
else
886-
menu.last_world = 0
813+
menu.world_to_del = 0
887814
end
888815
end
889816
end
890817

891818
if fields["world_configure"] ~= nil then
892819
selected = engine.get_textlist_index("srv_worlds")
893820
if selected > 0 then
894-
modmgr.world_config_selected_world = selected
821+
modmgr.world_config_selected_world = worldlist.get_engine_index(selected)
895822
if modmgr.init_worldconfig() then
896823
tabbuilder.current_tab = "dialog_configure_world"
897824
tabbuilder.is_dialog = true
@@ -970,27 +897,12 @@ function tabbuilder.handle_singleplayer_buttons(fields)
970897
end
971898

972899
if event.typ == "CHG" then
973-
engine.setting_set("main_menu_singleplayer_world_idx",engine.get_textlist_index("sp_worlds"))
900+
engine.setting_set("mainmenu_last_selected_world",
901+
worldlist.get_engine_index(engine.get_textlist_index("sp_worlds")))
974902
end
975903
end
976904

977-
if fields["key_up"] then
978-
local oldidx = engine.get_textlist_index("sp_worlds")
979-
980-
if oldidx > 1 then
981-
local newidx = oldidx -1
982-
engine.setting_set("main_menu_singleplayer_world_idx",newidx)
983-
end
984-
end
985-
986-
if fields["key_down"] then
987-
local oldidx = engine.get_textlist_index("sp_worlds")
988-
989-
if oldidx < #menu.filtered_game_list_raw() then
990-
local newidx = oldidx + 1
991-
engine.setting_set("main_menu_singleplayer_world_idx",newidx)
992-
end
993-
end
905+
menu.handle_key_up_down(fields,"sp_worlds","mainmenu_last_selected_world")
994906

995907
if fields["cb_creative_mode"] then
996908
engine.setting_setbool("creative_mode",tabbuilder.tobool(fields["cb_creative_mode"]))
@@ -1005,11 +917,8 @@ function tabbuilder.handle_singleplayer_buttons(fields)
1005917
fields["key_enter"] then
1006918
local selected = engine.get_textlist_index("sp_worlds")
1007919
if selected > 0 then
1008-
gamedata.selected_world = menu.filtered_index_to_plain(selected)
920+
gamedata.selected_world = worldlist.get_engine_index(selected)
1009921
gamedata.singleplayer = true
1010-
1011-
engine.setting_set("main_menu_tab",tabbuilder.current_tab)
1012-
engine.setting_set("main_menu_singleplayer_world_idx",selected)
1013922

1014923
menu.update_last_game(gamedata.selected_world)
1015924

@@ -1025,24 +934,26 @@ function tabbuilder.handle_singleplayer_buttons(fields)
1025934

1026935
if fields["world_delete"] ~= nil then
1027936
local selected = engine.get_textlist_index("sp_worlds")
1028-
if selected > 0 then
1029-
menu.last_world = menu.filtered_index_to_plain(selected)
1030-
if menu.lastworld() ~= nil and
1031-
menu.lastworld().name ~= nil and
1032-
menu.lastworld().name ~= "" then
937+
if selected > 0 and
938+
selected <= worldlist.size() then
939+
local world = worldlist.get_list()[selected]
940+
if world ~= nil and
941+
world.name ~= nil and
942+
world.name ~= "" then
943+
menu.world_to_del = worldlist.get_engine_index(selected)
1033944
tabbuilder.current_tab = "dialog_delete_world"
1034945
tabbuilder.is_dialog = true
1035946
tabbuilder.show_buttons = false
1036947
else
1037-
menu.last_world = 0
948+
menu.world_to_del = 0
1038949
end
1039950
end
1040951
end
1041952

1042953
if fields["world_configure"] ~= nil then
1043954
selected = engine.get_textlist_index("sp_worlds")
1044955
if selected > 0 then
1045-
modmgr.world_config_selected_world = menu.filtered_index_to_plain(selected)
956+
modmgr.world_config_selected_world = worldlist.get_engine_index(selected)
1046957
if modmgr.init_worldconfig() then
1047958
tabbuilder.current_tab = "dialog_configure_world"
1048959
tabbuilder.is_dialog = true
@@ -1180,11 +1091,10 @@ end
11801091

11811092
--------------------------------------------------------------------------------
11821093
function tabbuilder.tab_server()
1183-
local index = engine.setting_get("main_menu_last_world_idx")
1184-
1185-
if index == nil then
1186-
index = 0
1187-
end
1094+
1095+
local index = worldlist.get_current_index(
1096+
tonumber(engine.setting_get("mainmenu_last_selected_world"))
1097+
)
11881098

11891099
local retval =
11901100
"button[4,4.15;2.6,0.5;world_delete;Delete]" ..
@@ -1203,20 +1113,9 @@ function tabbuilder.tab_server()
12031113
engine.setting_get("name") .. "]" ..
12041114
"pwdfield[0.8,4.2;3,0.5;te_passwd;Password]" ..
12051115
"field[0.8,5.2;3,0.5;te_serverport;Server Port;30000]" ..
1206-
"textlist[4,0.25;7.5,3.7;srv_worlds;"
1207-
1208-
if #menu.worldlist > 0 then
1209-
retval = retval .. menu.worldlist[1].name ..
1210-
" \\[" .. menu.worldlist[1].gameid .. "\\]"
1211-
1212-
for i=2,#menu.worldlist,1 do
1213-
retval = retval .. "," .. menu.worldlist[i].name ..
1214-
" \\[" .. menu.worldlist[i].gameid .. "\\]"
1215-
end
1216-
retval = retval .. ";" .. index .. "]"
1217-
else
1218-
retval = retval .. ";0]"
1219-
end
1116+
"textlist[4,0.25;7.5,3.7;srv_worlds;" ..
1117+
menu.render_world_list() ..
1118+
";" .. index .. "]"
12201119

12211120
return retval
12221121
end
@@ -1244,12 +1143,10 @@ end
12441143

12451144
--------------------------------------------------------------------------------
12461145
function tabbuilder.tab_singleplayer()
1247-
local index = engine.setting_get("main_menu_singleplayer_world_idx")
1248-
1249-
if index == nil or
1250-
#menu.filtered_game_list_raw() == 0 then
1251-
index = 0
1252-
end
1146+
1147+
local index = worldlist.get_current_index(
1148+
tonumber(engine.setting_get("mainmenu_last_selected_world"))
1149+
)
12531150

12541151
return "button[4,4.15;2.6,0.5;world_delete;Delete]" ..
12551152
"button[6.5,4.15;2.8,0.5;world_create;New]" ..
@@ -1262,7 +1159,7 @@ function tabbuilder.tab_singleplayer()
12621159
"checkbox[0.5,0.7;cb_enable_damage;Enable Damage;" ..
12631160
dump(engine.setting_getbool("enable_damage")) .. "]"..
12641161
"textlist[4,0.25;7.5,3.7;sp_worlds;" ..
1265-
menu.filtered_game_list() ..
1162+
menu.render_world_list() ..
12661163
";" .. index .. "]" ..
12671164
menubar.formspec
12681165
end
@@ -1406,8 +1303,10 @@ end
14061303
--------------------------------------------------------------------------------
14071304
--------------------------------------------------------------------------------
14081305
init_globals()
1306+
worldlist.init()
14091307
menu.init()
14101308
tabbuilder.init()
14111309
menubar.refresh()
14121310
modstore.init()
1311+
14131312
update_menu()

Diff for: ‎builtin/mainmenu_worldlist.lua

+211
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
worldlist = {}
2+
3+
--------------------------------------------------------------------------------
4+
function worldlist.refresh()
5+
worldlist.m_raw_worldlist = engine.get_worlds()
6+
worldlist.process()
7+
end
8+
9+
--------------------------------------------------------------------------------
10+
function worldlist.init()
11+
worldlist.m_gamefilter = nil
12+
worldlist.m_sortmode = "alphabetic"
13+
14+
worldlist.m_processed_worldlist = nil
15+
worldlist.m_raw_worldlist = engine.get_worlds()
16+
17+
worldlist.process()
18+
end
19+
20+
--------------------------------------------------------------------------------
21+
function worldlist.set_gamefilter(gameid)
22+
if gameid == worldlist.m_gamefilter then
23+
return
24+
end
25+
worldlist.m_gamefilter = gameid
26+
worldlist.process()
27+
end
28+
29+
--------------------------------------------------------------------------------
30+
function worldlist.get_gamefilter()
31+
return worldlist.m_gamefilter
32+
end
33+
34+
--------------------------------------------------------------------------------
35+
--supported sort mode "alphabetic|none"
36+
function worldlist.set_sortmode(mode)
37+
if (mode == worldlist.m_sortmode) then
38+
return
39+
end
40+
worldlist.m_sortmode = mode
41+
worldlist.process()
42+
end
43+
44+
--------------------------------------------------------------------------------
45+
function worldlist.get_list()
46+
return worldlist.m_processed_worldlist
47+
end
48+
49+
--------------------------------------------------------------------------------
50+
function worldlist.get_raw_list()
51+
return worldlist.m_raw_worldlist
52+
end
53+
54+
--------------------------------------------------------------------------------
55+
function worldlist.get_raw_world(idx)
56+
if type(idx) ~= "number" then
57+
idx = tonumber(idx)
58+
end
59+
60+
if idx ~= nil and idx > 0 and idx < #worldlist.m_raw_worldlist then
61+
return worldlist.m_raw_worldlist[idx]
62+
end
63+
64+
return nil
65+
end
66+
67+
--------------------------------------------------------------------------------
68+
function worldlist.get_engine_index(worldlistindex)
69+
assert(worldlist.m_processed_worldlist ~= nil)
70+
71+
if worldlistindex ~= nil and worldlistindex > 0 and
72+
worldlistindex <= #worldlist.m_processed_worldlist then
73+
local entry = worldlist.m_processed_worldlist[worldlistindex]
74+
75+
for i,v in ipairs(worldlist.m_raw_worldlist) do
76+
77+
if worldlist.compare(v,entry) then
78+
return i
79+
end
80+
end
81+
end
82+
83+
return 0
84+
end
85+
86+
--------------------------------------------------------------------------------
87+
function worldlist.get_current_index(worldlistindex)
88+
assert(worldlist.m_processed_worldlist ~= nil)
89+
90+
if worldlistindex ~= nil and worldlistindex > 0 and
91+
worldlistindex <= #worldlist.m_raw_worldlist then
92+
local entry = worldlist.m_raw_worldlist[worldlistindex]
93+
94+
for i,v in ipairs(worldlist.m_processed_worldlist) do
95+
96+
if worldlist.compare(v,entry) then
97+
return i
98+
end
99+
end
100+
end
101+
102+
return 0
103+
end
104+
105+
--------------------------------------------------------------------------------
106+
function worldlist.process()
107+
assert(worldlist.m_raw_worldlist ~= nil)
108+
109+
if worldlist.m_sortmode == "none" and
110+
worldlist.m_gamefilter == nil then
111+
worldlist.m_processed_worldlist = worldlist.m_raw_worldlist
112+
return
113+
end
114+
115+
worldlist.m_processed_worldlist = {}
116+
117+
for i,v in ipairs(worldlist.m_raw_worldlist) do
118+
119+
if worldlist.m_gamefilter == nil or
120+
v.gameid == worldlist.m_gamefilter then
121+
table.insert(worldlist.m_processed_worldlist,v)
122+
end
123+
end
124+
125+
if worldlist.m_sortmode == "none" then
126+
return
127+
end
128+
129+
if worldlist.m_sortmode == "alphabetic" then
130+
worldlist.sort_alphabetic()
131+
end
132+
133+
end
134+
135+
--------------------------------------------------------------------------------
136+
function worldlist.compare(world1,world2)
137+
138+
if world1.path ~= world2.path then
139+
return false
140+
end
141+
142+
if world1.name ~= world2.name then
143+
return false
144+
end
145+
146+
if world1.gameid ~= world2.gameid then
147+
return false
148+
end
149+
150+
return true
151+
end
152+
153+
--------------------------------------------------------------------------------
154+
function worldlist.size()
155+
if worldlist.m_processed_worldlist == nil then
156+
return 0
157+
end
158+
159+
return #worldlist.m_processed_worldlist
160+
end
161+
162+
--------------------------------------------------------------------------------
163+
function worldlist.exists(worldname)
164+
for i,v in ipairs(worldlist.m_raw_worldlist) do
165+
if v.name == worldname then
166+
return true
167+
end
168+
end
169+
return false
170+
end
171+
172+
173+
--------------------------------------------------------------------------------
174+
function worldlist.engine_index_by_name(worldname)
175+
local worldcount = 0
176+
local worldidx = 0
177+
for i,v in ipairs(worldlist.m_raw_worldlist) do
178+
if v.name == worldname then
179+
worldcount = worldcount +1
180+
worldidx = i
181+
end
182+
end
183+
184+
185+
-- If there are more worlds than one with same name we can't decide which
186+
-- one is meant. This shouldn't be possible but just for sure.
187+
if worldcount > 1 then
188+
worldidx=0
189+
end
190+
191+
return worldidx
192+
end
193+
194+
--------------------------------------------------------------------------------
195+
function worldlist.sort_alphabetic()
196+
197+
table.sort(worldlist.m_processed_worldlist, function(a, b)
198+
local n1 = a.name
199+
local n2 = b.name
200+
local count = math.min(#n1, #n2)
201+
202+
for i=1,count do
203+
if n1:sub(i, i):lower() < n2:sub(i, i):lower() then
204+
return true
205+
elseif n1:sub(i, i):lower() > n2:sub(i, i):lower() then
206+
return false
207+
end
208+
end
209+
return (#n1 <= #n2)
210+
end)
211+
end

0 commit comments

Comments
 (0)
Please sign in to comment.