Skip to content

Commit

Permalink
Add a mechanism to declare capability dependencies at widget/gadget G…
Browse files Browse the repository at this point in the history
…etInfo (#4034)

Add a mechanism to declare capability dependencies at widgets and gadgets
* Through the new 'depends' field.
* For now added: gl, gl4, shaders, fbo.
* This makes the widgets and gadgets automatically inactive if capabilities not present.

Extends the Platform module for this.
* Adds Platform.check(capabilities), also Platform.gl, Platform.gl4, Platform.glHaveFBO.
* These also allow testing for the features partially inside modules in a cleaner way than the current way of gl.CreateShader or similar, and it can be a central place where the tests can be improved.
  • Loading branch information
saurtron authored Dec 19, 2024
1 parent 0b43940 commit f17b394
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 12 deletions.
53 changes: 53 additions & 0 deletions common/platformFunctions.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
if not Platform then return end
local hasGL4 = false
local hasGL = false
local hasShaders = false
local hasFBO = false
local isSyncedCode = (SendToUnsynced ~= nil)

local function determineCapabilities()
if not gl then
return
end
if Platform.glVendor ~= "" then
hasGL = true
end
if gl.CreateShader and Platform.glHaveGLSL then
hasShaders = true
end
if gl.CreateFBO then
hasFBO = true
end

if hasFBO and hasShaders and Platform.glHaveGL4 then
hasGL4 = true
end
end

local function checkRequires(allRequires)
if not allRequires or isSyncedCode then
return true
end
for _, req in pairs(allRequires) do
if req == 'gl' and not hasGL then
return false
elseif req == 'gl4' and not hasGL4 then
return false
elseif req == 'shaders' and not hasShaders then
return false
elseif req == 'fbo' and not hasVBO then
return false
end
end
return true
end

local function extendPlatform()
Platform.gl = Platform.gl or hasGL
Platform.gl4 = Platform.gl4 or hasGL4
Platform.glHaveFBO = Platform.glHaveFBO or hasFBO
Platform.check = checkRequires
end

determineCapabilities()
extendPlatform()
2 changes: 2 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ if commonFunctions.spring[environment] then
local springFunctions = VFS.Include('common/springFunctions.lua')
Spring.Utilities = Spring.Utilities or springFunctions.Utilities
Spring.Debug = Spring.Debug or springFunctions.Debug
-- extend platform
VFS.Include('common/platformFunctions.lua')
end

if commonFunctions.i18n[environment] then
Expand Down
5 changes: 5 additions & 0 deletions luarules/gadgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ function gadgetHandler:LoadGadget(filename, overridevfsmode)
return nil -- gadget asked for a quiet death
end

if gadget.GetInfo and (Platform and not Platform.check(gadget.GetInfo().depends)) then
Spring.Echo('Missing capabilities: ' .. gadget:GetInfo().name .. '. Disabling.')
return nil
end

-- raw access to gadgetHandler
if gadget.GetInfo and gadget:GetInfo().handler then
gadget.gadgetHandler = self
Expand Down
1 change: 1 addition & 0 deletions luarules/gadgets/cus_gl4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function gadget:GetInfo()
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true,
depends = {'gl4'},
}
end

Expand Down
5 changes: 1 addition & 4 deletions luaui/Widgets/gfx_DrawUnitShape_GL4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function widget:GetInfo()
license = "GNU GPL, v2 or later",
layer = -9999,
enabled = true,
depends = {'gl4'},
}
end

Expand Down Expand Up @@ -438,10 +439,6 @@ if TESTMODE then
end

function widget:Initialize()
if not gl.CreateShader then -- no shader support, so just remove the widget itself, especially for headless
widgetHandler:RemoveWidget()
return
end
for unitDefID, unitDef in pairs(UnitDefs) do
if unitDef.model and unitDef.model.textures and unitDef.model.textures.tex1 then
unitDefIDtoTex1[unitDefID] = unitDef.model.textures.tex1:lower()
Expand Down
5 changes: 1 addition & 4 deletions luaui/Widgets/gfx_decals_gl4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function widget:GetInfo()
license = "Lua code: GNU GPL, v2 or later, Shader GLSL code: (c) Beherith ([email protected])",
layer = 999,
enabled = true,
depends = {'gl4'},
}
end

Expand Down Expand Up @@ -1925,10 +1926,6 @@ local function UnitScriptDecal(unitID, unitDefID, whichDecal, posx, posz, headin
end

function widget:Initialize()
if not gl.CreateShader then -- no shader support, so just remove the widget itself, especially for headless
widgetHandler:RemoveWidget()
return
end
local t0 = Spring.GetTimer()
--if makeAtlases() == false then
-- goodbye("Failed to init texture atlas for DecalsGL4")
Expand Down
5 changes: 1 addition & 4 deletions luaui/Widgets/gui_attackrange_gl4.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ function widget:GetInfo()
license = "Lua: GPLv2, GLSL: (c) Beherith ([email protected])",
layer = -99,
enabled = true,
depends = {'gl4'},
}
end

Expand Down Expand Up @@ -708,10 +709,6 @@ function widget:PlayerChanged(playerID)
end

function widget:Initialize()
if not gl.CreateShader then -- no shader support, so just remove the widget itself, especially for headless
widgetHandler:RemoveWidget(self)
return
end
initUnitList()

if initGL4() == false then
Expand Down
11 changes: 11 additions & 0 deletions luaui/barwidgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,14 @@ function widgetHandler:InsertWidgetRaw(widget)
if widget == nil then
return
end
if widget.GetInfo and not Platform.check(widget:GetInfo().depends) then
local name = widget.whInfo.name
if self.knownWidgets[name] then
self.knownWidgets[name].active = false
end
Spring.Echo('Missing capabilities: ' .. name .. '. Disabling.')
return
end

SafeWrapWidget(widget)

Expand All @@ -932,6 +940,9 @@ function widgetHandler:RemoveWidgetRaw(widget)
if widget == nil or widget.whInfo == nil then
return
end
if not Platform.check(widget.whInfo.depends) then
return
end

if self.textOwner == widget then
self.textOwner = nil
Expand Down

0 comments on commit f17b394

Please sign in to comment.