Skip to content

Commit 3dbca44

Browse files
LocalIdentityLocalIdentity
andauthored
Avoid rescanning all builds when closing a build (#10405)
Reuse the cached recursive build index when returning to the same folder and refresh only the build that was just closed. Full rescans are still performed when the build folder changes. This also avoids scanning a nested folder twice when returning from a build opened through recursive search. Co-authored-by: LocalIdentity <localidentity2@gmail.com>
1 parent 2886887 commit 3dbca44

3 files changed

Lines changed: 83 additions & 14 deletions

File tree

‎spec/System/TestBuildListHelpers_spec.lua‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ describe("BuildListHelpers", function()
22
local originalBuildPath
33
local originalCloudErrorPopup
44
local originalFileSearch
5+
local originalFilterBuildList
56
local originalOpen
67
local buildListHelpers
78
local fileHeaders
@@ -14,6 +15,7 @@ describe("BuildListHelpers", function()
1415
originalBuildPath = main.buildPath
1516
originalCloudErrorPopup = main.OpenCloudErrorPopup
1617
originalFileSearch = _G.NewFileSearch
18+
originalFilterBuildList = main.filterBuildList
1719
originalOpen = io.open
1820
buildListHelpers = LoadModule("Modules/BuildListHelpers")
1921
fileHeaders = { }
@@ -22,6 +24,7 @@ describe("BuildListHelpers", function()
2224
searchCount = 0
2325
cloudErrorPath = nil
2426
main.buildPath = "Builds/"
27+
main.filterBuildList = ""
2528
main.OpenCloudErrorPopup = function(_, path)
2629
cloudErrorPath = path
2730
end
@@ -53,6 +56,7 @@ describe("BuildListHelpers", function()
5356
main.buildPath = originalBuildPath
5457
main.OpenCloudErrorPopup = originalCloudErrorPopup
5558
_G.NewFileSearch = originalFileSearch
59+
main.filterBuildList = originalFilterBuildList
5660
io.open = originalOpen
5761
end)
5862

@@ -77,6 +81,33 @@ describe("BuildListHelpers", function()
7781
assert.are.same(opensAfterIndex, fileOpenCount)
7882
end)
7983

84+
it("refreshes only the reopened build when returning to the build list", function()
85+
searches["files:Builds/*.xml"] = { { name = "Root.xml", modified = 1 } }
86+
searches["folders:Builds/*"] = { { name = "League", modified = 2 } }
87+
searches["files:Builds/League/*.xml"] = { { name = "Nested.xml", modified = 3 } }
88+
fileHeaders["Builds/Root.xml"] = '<Build level="80" className="Witch" ascendClassName="Occultist">'
89+
fileHeaders["Builds/League/Nested.xml"] = '<Build level="90" className="Shadow" ascendClassName="Assassin">'
90+
local listMode = require("Modules.BuildList")
91+
92+
listMode:Init()
93+
local scansAfterIndex = searchCount
94+
local opensAfterIndex = fileOpenCount
95+
searches["files:Builds/Root.xml"] = { { name = "Root.xml", modified = 4 } }
96+
fileHeaders["Builds/Root.xml"] = '<Build level="81" className="Shadow" ascendClassName="Assassin">'
97+
listMode:Init("Root", "")
98+
99+
assert.are.same(scansAfterIndex + 1, searchCount)
100+
assert.are.same(opensAfterIndex + 1, fileOpenCount)
101+
assert.are.same(81, listMode.controls.buildList.selValue.level)
102+
assert.are.same("Assassin", listMode.controls.buildList.selValue.ascendClassName)
103+
assert.are.same(4, listMode.controls.buildList.selValue.modified)
104+
105+
local scansBeforeFolderChange = searchCount
106+
listMode:Init("Nested", "League/")
107+
assert.are.same(scansBeforeFolderChange + 2, searchCount)
108+
assert.are.same("Builds/League/Nested.xml", listMode.controls.buildList.selValue.fullFileName)
109+
end)
110+
80111
it("reports cloud read failures", function()
81112
searches["files:Builds/*.xml"] = { { name = "Cloud.xml" } }
82113
fileHeaders["Builds/Cloud.xml"] = false

‎src/Modules/BuildList.lua‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ local listMode = new("ControlHost"):ControlHost()
1414

1515
function listMode:Init(selBuildName, subPath)
1616
if self.initialised then
17+
local subPathChanged = subPath and subPath ~= self.subPath
18+
local reuseBuildIndex = not subPathChanged and self.indexedBuildPath == main.buildPath
1719
self.subPath = subPath or self.subPath
1820
self.controls.buildList.controls.path:SetSubPath(self.subPath)
1921
--if main.showPublicBuilds then
@@ -22,7 +24,14 @@ function listMode:Init(selBuildName, subPath)
2224
else
2325
self.controls.ExtBuildList = nil
2426
end
25-
self:BuildList()
27+
if reuseBuildIndex then
28+
if selBuildName then
29+
buildListHelpers.RefreshBuild(self.buildIndex, self.subPath, selBuildName..".xml")
30+
end
31+
self:FilterBuildList()
32+
elseif not subPathChanged then
33+
self:BuildList()
34+
end
2635
self.controls.buildList:SelByFullFileName(selBuildName and main.buildPath..self.subPath..selBuildName..".xml")
2736
self:SelectControl(self.controls.buildList)
2837
return
@@ -199,6 +208,7 @@ function listMode:GetDestName(subPath, fileName)
199208
end
200209

201210
function listMode:BuildList()
211+
self.indexedBuildPath = main.buildPath
202212
self.buildIndex = buildListHelpers.ScanFolder(self.subPath)
203213
self:FilterBuildList()
204214
end

‎src/Modules/BuildListHelpers.lua‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ local function ReadBuildHeader(fullFileName)
3434
}
3535
end
3636

37+
local function ReadBuild(subPath, fileName, modified)
38+
local fullFileName = main.buildPath..subPath..fileName
39+
local header = ReadBuildHeader(fullFileName)
40+
if not header then return end
41+
return {
42+
fileName = fileName,
43+
subPath = subPath,
44+
fullFileName = fullFileName,
45+
modified = modified,
46+
buildName = fileName:gsub("%.xml$",""),
47+
level = header.level,
48+
className = header.className,
49+
ascendClassName = header.ascendClassName,
50+
}
51+
end
52+
3753
local function MatchEntry(entry, terms)
3854
for _, term in ipairs(terms) do
3955
local termLower = term:lower()
@@ -66,19 +82,9 @@ local function ScanFolder(subPath)
6682
local handle = NewFileSearch(main.buildPath..currentSubPath.."*.xml")
6783
while handle do
6884
local fileName = handle:GetFileName()
69-
local fullFileName = main.buildPath..currentSubPath..fileName
70-
local header = ReadBuildHeader(fullFileName)
71-
if header then
72-
t_insert(list, {
73-
fileName = fileName,
74-
subPath = currentSubPath,
75-
fullFileName = fullFileName,
76-
modified = handle:GetFileModifiedTime(),
77-
buildName = fileName:gsub("%.xml$",""),
78-
level = header.level,
79-
className = header.className,
80-
ascendClassName = header.ascendClassName,
81-
})
85+
local build = ReadBuild(currentSubPath, fileName, handle:GetFileModifiedTime())
86+
if build then
87+
t_insert(list, build)
8288
end
8389

8490
if not handle:NextFile() then break end
@@ -110,6 +116,27 @@ local function ScanFolder(subPath)
110116
return list
111117
end
112118

119+
local function RefreshBuild(index, subPath, fileName)
120+
local fullFileName = main.buildPath..subPath..fileName
121+
local handle = NewFileSearch(fullFileName)
122+
local build = handle and ReadBuild(subPath, fileName, handle:GetFileModifiedTime())
123+
if handle and not build then return end
124+
125+
for buildIndex, entry in ipairs(index) do
126+
if entry.fullFileName == fullFileName then
127+
if build then
128+
index[buildIndex] = build
129+
else
130+
table.remove(index, buildIndex)
131+
end
132+
return
133+
end
134+
end
135+
if build then
136+
t_insert(index, build)
137+
end
138+
end
139+
113140
-- Filtering the cached index avoids filesystem work on every keystroke.
114141
local function FilterList(index, subPath, filterText)
115142
local terms = { }
@@ -196,6 +223,7 @@ return {
196223
buildSortDropList = buildSortDropList,
197224
CanMoveToSubPath = CanMoveToSubPath,
198225
FilterList = FilterList,
226+
RefreshBuild = RefreshBuild,
199227
ScanFolder = ScanFolder,
200228
SortList = SortList,
201229
}

0 commit comments

Comments
 (0)