Skip to content

Commit ca797ed

Browse files
Feature/map list optimizations (#844)
* Map list performance optimizations. * Sort featured maps by name so that batched population of list looks nicer. Comments: - I don't want think about a metatable change that should be a noop in an unrelated file. - Localised some globals that are only used in init. --------- Co-authored-by: GoogleFrog <googl3frog@gmail.com>
1 parent d1c47f7 commit ca797ed

5 files changed

Lines changed: 135 additions & 123 deletions

File tree

LuaMenu/widgets/api_featured_maps.lua

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,38 @@ local function ToMapType(mapItem)
3333
return "Special"
3434
end
3535

36+
local function ToTerrainType(mapItem)
37+
if mapItem.WaterLevel == 3 then
38+
return "Sea"
39+
end
40+
local first
41+
if mapItem.Hills == 1 then
42+
first = "Flat "
43+
elseif mapItem.Hills == 2 then
44+
first = "Hilly "
45+
else
46+
first = "Mountainous "
47+
end
48+
local second
49+
if mapItem.WaterLevel == 1 then
50+
second = "land"
51+
else
52+
second = "mixed"
53+
end
54+
55+
return first .. second
56+
end
57+
3658
local function InitMapItems()
3759
if not mapItems then
38-
mapItems = WG.CommunityWindow and WG.CommunityWindow.LoadStaticCommunityData().MapItems or {}
39-
for i = 1, #mapItems do
40-
mapItems[i].MapType = ToMapType(mapItems[i])
60+
mapItems = {}
61+
for _, v in pairs(WG.CommunityWindow and WG.CommunityWindow.LoadStaticCommunityData().MapItems or {}) do
62+
local mapItem = table.deepcopy(v)
63+
mapItem.MapType = ToMapType(mapItem)
64+
mapItem.TerrainType = ToTerrainType(mapItem)
65+
mapItems[#mapItems+1] = mapItem
4166
end
67+
table.sort(mapItems, function(l, r) return l.Name:upper() < r.Name:upper() end)
4268
end
4369
end
4470

LuaMenu/widgets/chobby/components/configuration.lua

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Configuration = LCS.class{}
33
VFS.Include("libs/liblobby/lobby/json.lua")
44

55
LIB_LOBBY_DIRNAME = "libs/liblobby/lobby/"
6+
MINIMAP_THUMB_DOWNLOAD_DIR = "LuaMenu/Images/MinimapThumbnails"
67

78

89
-- all configuration attribute changes should use the :Set*Attribute*() and :Get*Attribute*() methods in order to assure proper functionality
@@ -312,7 +313,7 @@ function Configuration:init()
312313

313314
self.animate_lobby = (gl.CreateShader ~= nil)
314315
self.minimapDownloads = {}
315-
self.minimapThumbDownloads = {}
316+
self.minimapDownloadStarted = {}
316317
self.downloadRetryCount = 3
317318

318319
local saneCharacterList = {
@@ -805,23 +806,25 @@ function Configuration:AllowNotification(playerName, playerList)
805806
end
806807

807808
function Configuration:GetMinimapSmallImage(mapName)
808-
if not self.gameConfig.minimapThumbnailPath then
809-
return LUA_DIRNAME .. "images/minimapNotFound1.png"
810-
end
811809
mapName = string.gsub(mapName, " ", "_")
810+
812811
local filePath = self.gameConfig.minimapThumbnailPath .. mapName .. ".png"
813-
if not VFS.FileExists(filePath) then
814-
filePath = "LuaMenu/Images/MinimapThumbnails" .. mapName .. ".jpg"
812+
if VFS.FileExists(filePath) then
813+
return filePath, false
815814
end
816-
if WG.WrapperLoopback and WG.WrapperLoopback.DownloadImage and (not VFS.FileExists(filePath)) then
817-
if not self.minimapThumbDownloads[mapName] then
818-
Spring.CreateDir("LuaMenu/Images/MinimapThumbnails")
819-
WG.WrapperLoopback.DownloadImage({ImageUrl = "http://zero-k.info/Resources/" .. mapName .. ".thumbnail.jpg", TargetPath = filePath})
820-
self.minimapThumbDownloads[mapName] = true
821-
end
822-
return filePath, true
815+
816+
filePath = MINIMAP_THUMB_DOWNLOAD_DIR .. mapName .. ".jpg"
817+
if VFS.FileExists(filePath) then
818+
return filePath, false
823819
end
824-
return filePath
820+
821+
if not self.minimapDownloadStarted[mapName] and WG.WrapperLoopback and WG.WrapperLoopback.DownloadImage then
822+
Spring.CreateDir(MINIMAP_THUMB_DOWNLOAD_DIR)
823+
WG.WrapperLoopback.DownloadImage({ImageUrl = "http://zero-k.info/Resources/" .. mapName .. ".thumbnail.jpg", TargetPath = filePath})
824+
self.minimapDownloadStarted[mapName] = true
825+
end
826+
827+
return filePath, true
825828
end
826829

827830
function Configuration:GetMinimapImage(mapName)

LuaMenu/widgets/gui_maplist_panel.lua

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ end
1414
--------------------------------------------------------------------------------
1515
-- Local Variables
1616

17+
local loadRate = 1
1718
local mapListWindow
1819
local lobby
19-
local loadRate = 1
2020
local oldOnlyFeaturedMaps = nil
2121
local IMG_READY = LUA_DIRNAME .. "images/ready.png"
2222
local IMG_UNREADY = LUA_DIRNAME .. "images/unready.png"
@@ -25,31 +25,7 @@ local IMG_UNREADY = LUA_DIRNAME .. "images/unready.png"
2525
--------------------------------------------------------------------------------
2626
-- Utilities
2727

28-
local function GetTerrainType(hillLevel, waterLevel)
29-
if waterLevel == 3 then
30-
return "Sea"
31-
end
32-
local first
33-
if hillLevel == 1 then
34-
first = "Flat "
35-
elseif hillLevel == 2 then
36-
first = "Hilly "
37-
else
38-
first = "Mountainous "
39-
end
40-
local second
41-
if waterLevel == 1 then
42-
second = "land"
43-
else
44-
second = "mixed"
45-
end
46-
47-
return first .. second
48-
end
49-
50-
local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"Name":"2_Mountains_Battlefield","SupportLevel":2,"Width":16,"Height":16,"IsAssymetrical":false,"Hills":2,"WaterLevel":1,"Is1v1":false,"IsTeams":true,"IsFFA":false,"IsChickens":false,"FFAMaxTeams":null,"RatingCount":3,"RatingSum":10,"IsSpecial":false},
51-
local Configuration = WG.Chobby.Configuration
52-
28+
local function CreateMapEntry(mapName, mapData, CloseFunc, Configuration, listFont)--{"ResourceID":7098,"Name":"2_Mountains_Battlefield","SupportLevel":2,"Width":16,"Height":16,"IsAssymetrical":false,"Hills":2,"WaterLevel":1,"Is1v1":false,"IsTeams":true,"IsFFA":false,"IsChickens":false,"FFAMaxTeams":null,"RatingCount":3,"RatingSum":10,"IsSpecial":false},
5329
local mapButton = Button:New {
5430
classname = "button_rounded",
5531
x = 0,
@@ -70,7 +46,7 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
7046
}
7147

7248
local mapImageFile, needDownload = Configuration:GetMinimapSmallImage(mapName)
73-
local minimapImage = Image:New {
49+
Image:New {
7450
name = "minimapImage",
7551
x = 3,
7652
y = 3,
@@ -79,7 +55,7 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
7955
padding = {1,1,1,1},
8056
keepAspect = true,
8157
file = mapImageFile,
82-
fallbackFile = Configuration:GetLoadingImage(2),
58+
fallbackFile = (needDownload and Configuration:GetLoadingImage(2)) or nil,
8359
checkFileExists = needDownload,
8460
parent = mapButton,
8561
}
@@ -90,7 +66,7 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
9066
width = 200,
9167
height = 16,
9268
valign = 'center',
93-
objectOverrideFont = Configuration:GetFont(2),
69+
objectOverrideFont = listFont,
9470
caption = mapName:gsub("_", " "),
9571
parent = mapButton,
9672
}
@@ -108,15 +84,16 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
10884
local sortData
10985
if mapData then
11086
local mapSizeText = (mapData.Width or " ?") .. "x" .. (mapData.Height or " ?")
111-
local terrainType = GetTerrainType(mapData.Hills, mapData.WaterLevel)
87+
local mapType = mapData.MapType
88+
local terrainType = mapData.TerrainType
11289

11390
Label:New {
11491
x = 274,
11592
y = 15,
11693
width = 68,
11794
height = 16,
11895
valign = 'center',
119-
objectOverrideFont = Configuration:GetFont(2),
96+
objectOverrideFont = listFont,
12097
caption = mapSizeText,
12198
parent = mapButton,
12299
}
@@ -126,8 +103,8 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
126103
width = 68,
127104
height = 16,
128105
valign = 'center',
129-
objectOverrideFont = Configuration:GetFont(2),
130-
caption = mapData.MapType,
106+
objectOverrideFont = listFont,
107+
caption = mapType,
131108
parent = mapButton,
132109
}
133110
Label:New {
@@ -136,12 +113,12 @@ local function CreateMapEntry(mapName, mapData, CloseFunc)--{"ResourceID":7098,"
136113
width = 160,
137114
height = 16,
138115
valign = 'center',
139-
objectOverrideFont = Configuration:GetFont(2),
116+
objectOverrideFont = listFont,
140117
caption = terrainType,
141118
parent = mapButton,
142119
}
143120

144-
sortData = {string.lower(mapName), (mapData.Width or 0)*100 + (mapData.Height or 0), string.lower(mapData.MapType), string.lower(terrainType), (haveMap and 1) or 0}
121+
sortData = {string.lower(mapName), (mapData.Width or 0)*100 + (mapData.Height or 0), string.lower(mapType), string.lower(terrainType), (haveMap and 1) or 0}
145122
sortData[6] = sortData[1] .. " " .. mapSizeText .. " " .. sortData[3] .. " " .. sortData[4] -- Used for text filter by name, type, terrain or size.
146123
else
147124
sortData = {string.lower(mapName), 0, "", "", (haveMap and 1) or 0}
@@ -170,6 +147,7 @@ local function InitializeControls()
170147
--Spring.Echo("LuaMenu KB", lmkb, "allocs", lmalloc, "Lua global KB", lgkb, "allocs", lgalloc)
171148

172149
local Configuration = WG.Chobby.Configuration
150+
local listFont = Configuration:GetFont(2)
173151

174152
local mapListWindow = Window:New {
175153
classname = "main_window",
@@ -265,7 +243,7 @@ local function InitializeControls()
265243
for i = 1, loadRate do
266244
if featuredMapList[featuredMapIndex] then
267245
local mapName = featuredMapList[featuredMapIndex].Name
268-
control, sortData, mapFuncs[mapName] = CreateMapEntry(mapName, featuredMapList[featuredMapIndex], CloseFunc)
246+
control, sortData, mapFuncs[mapName] = CreateMapEntry(mapName, featuredMapList[featuredMapIndex], CloseFunc, Configuration, listFont)
269247
mapItems[#mapItems + 1] = {mapName, control, sortData}
270248
featuredMapIndex = featuredMapIndex + 1
271249
end
@@ -278,7 +256,7 @@ local function InitializeControls()
278256
for i, archive in pairs(VFS.GetAllArchives()) do
279257
local info = VFS.GetArchiveInfo(archive)
280258
if info and info.modtype == 3 and not mapFuncs[info.name] then
281-
control, sortData, mapFuncs[info.name] = CreateMapEntry(info.name, nil, CloseFunc)
259+
control, sortData, mapFuncs[info.name] = CreateMapEntry(info.name, nil, CloseFunc, Configuration, listFont)
282260
mapItems[#mapItems + 1] = {info.name, control, sortData}
283261
end
284262
end
@@ -298,7 +276,7 @@ local function InitializeControls()
298276
width = 80,
299277
height = WG.BUTTON_HEIGHT,
300278
caption = i18n("close"),
301-
objectOverrideFont = Configuration:GetButtonFont(3),
279+
objectOverrideFont = Configuration:GetFont(3),
302280
classname = "negative_button",
303281
parent = mapListWindow,
304282
OnClick = {
@@ -309,13 +287,13 @@ local function InitializeControls()
309287
}
310288

311289
if Configuration.gameConfig.link_maps ~= nil then
312-
local btnOnlineMaps = Button:New {
290+
Button:New {
313291
right = 98,
314292
y = WG.TOP_BUTTON_Y,
315293
width = 180,
316294
height = WG.BUTTON_HEIGHT,
317295
caption = i18n("download_maps"),
318-
objectOverrideFont = Configuration:GetButtonFont(3),
296+
objectOverrideFont = Configuration:GetFont(3),
319297
classname = "option_button",
320298
parent = mapListWindow,
321299
OnClick = {
@@ -387,7 +365,7 @@ local function InitializeControls()
387365
local info = VFS.GetArchiveInfo(thingName)
388366
if info and info.modtype == 3 and not mapFuncs[info.name] then
389367
local control, sortData
390-
control, sortData, mapFuncs[info.name] = CreateMapEntry(info.name, nil, CloseFunc)
368+
control, sortData, mapFuncs[info.name] = CreateMapEntry(info.name, nil, CloseFunc, Configuration, listFont)
391369
mapList:AddItem(info.name, control, sortData)
392370
end
393371
end

0 commit comments

Comments
 (0)