Skip to content

Commit

Permalink
Replace c++ packlist with a lua one
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Nov 28, 2018
1 parent 13581f7 commit 8da0bbd
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Themes/Til Death/BGAnimations/packlistDisplay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ local o =
end,
BeginCommand = function(self)
self:SetUpdateFunction(highlight)
packlist = DLMAN:GetPacklist()
packlist = PackList:new()
packlist:SetFromAll()
self:queuecommand("PackTableRefresh")
end,
Expand Down Expand Up @@ -71,9 +71,11 @@ local o =
ind = ind - numpacks
self:queuecommand("Update")
end,
Def.Quad {InitCommand = function(self)
Def.Quad {
InitCommand = function(self)
self:zoomto(width, height - headeroff):halign(0):valign(0):diffuse(color("#888888"))
end},
end
},
-- headers
Def.Quad {
InitCommand = function(self)
Expand Down
103 changes: 103 additions & 0 deletions Themes/_fallback/Scripts/09 PackList.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
PackList = {}

local getSizePropName = "GetSize"
local getAvgDiffPropName = "GetAvgDifficulty"
local getNamePropName = "GetName"

function PackList:GetPackTable()
SCREENMAN:SystemMessage(tostring(#(self.packs)))
return self.packs
end
local foldr = function(func, val, tbl)
for i, v in pairs(tbl) do
val = func(val, v)
end
return val
end
function PackList:GetTotalSumByProp(propName)
return foldr(
function(sum, x)
return sum + x[propName](x)
end,
0,
self.packs
)
end
function PackList:GetTotalSize()
return self:GetTotalSumByProp(getSizePropName)
end
function PackList:GetAvgDiff()
return self:GetTotalSumByProp(getAvgDiffPropName)
end
function PackList:SetFromCoreBundle(bundleName)
local bundle = DLMAN:GetCoreBundle(bundleName)
self.packs = bundle
return self
end
function PackList:SortByProp(propName)
if self.lastsort == propName then
self.asc = not self.asc
end
self.lastsort = propName
local asc = self.asc
table.sort(
self.packs,
asc and function(a, b)
return a[propName](a) > b[propName](b)
end or function(a, b)
return a[propName](a) < b[propName](b)
end
)
return self
end
function PackList:SortByName()
return self:SortByProp(getNamePropName)
end
function PackList:SortByDiff()
return self:SortByProp(getAvgDiffPropName)
end
function PackList:SortBySize()
return self:SortByProp(getSizePropName)
end
local function filter(func, tbl)
local newtbl = {}
for i, v in pairs(tbl) do
if func(v) then
newtbl[i] = v
end
end
return newtbl
end
function PackList:FilterAndSearch(name, avgMin, avgMax, sizeMin, sizeMax)
self.packs =
filter(
function(x)
local d = x[getAvgDiffPropName]:x()
local n = x[getNamePropName]:x()
local s = x[getSizePropName]:x()
return n == name and ((d > avgMin and d < avgMax) or d <= 0) and ((s > sizeMin and d < sizeMax) or s <= 0)
end,
self.packs
)
return self
end
function PackList:SetFromAll()
self.packs = DLMAN:GetAllPacks()
return self
end
function PackList:new()
local packlist = {}
packlist.asc = true
packlist.lastsort = nil
packlist.packs = {}
setmetatable(
packlist,
{
__index = function(t, i)
return t.packs[i] or PackList[i]
end
}
)
packlist:SetFromAll()
return packlist
end
11 changes: 11 additions & 0 deletions src/DownloadManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,16 @@ class LunaDownloadManager : public Luna<DownloadManager>
DLMAN->pl.PushSelf(L);
return 1;
}
static int GetAllPacks(T* p, lua_State* L)
{
vector<DownloadablePack>& packs = DLMAN->downloadablePacks;
lua_createtable(L, packs.size(), 0);
for (unsigned i = 0; i < packs.size(); ++i) {
packs[i].PushSelf(L);
lua_rawseti(L, -2, i + 1);
}
return 1;
}
static int GetDownloadingPacks(T* p, lua_State* L)
{
vector<DownloadablePack>& packs = DLMAN->downloadablePacks;
Expand Down Expand Up @@ -2410,6 +2420,7 @@ class LunaDownloadManager : public Luna<DownloadManager>
ADD_METHOD(DownloadCoreBundle);
ADD_METHOD(GetCoreBundle);
ADD_METHOD(GetPacklist);
ADD_METHOD(GetAllPacks);
ADD_METHOD(GetDownloadingPacks);
ADD_METHOD(GetDownloads);
ADD_METHOD(GetToken);
Expand Down

0 comments on commit 8da0bbd

Please sign in to comment.