Skip to content

Commit

Permalink
Post #650 UEHelpers improvements (#676)
Browse files Browse the repository at this point in the history
  • Loading branch information
igromanru authored Oct 11, 2024
1 parent 833b84b commit 8d38572
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 15 deletions.
93 changes: 92 additions & 1 deletion assets/Mods/shared/Types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,100 @@ function LoopAsync(DelayInMilliseconds, Callback) end
---You also use `.__name` and `.__absolute_path` for files.
function IterateGameDirectories() end


-- # Classes

---Class for interacting with UE4SS metadata
---@class UE4SS
UE4SS = {}

---Returns current version of UE4SS
---@return integer, integer, integer
function UE4SS:GetVersion() end

---Returns current version of UE4SS
---@return integer, integer, integer
function UE4SS.GetVersion() end

---Contains helper functions for retrieving which version of Unreal Engine that is being used.
---@class UnrealVersion
UnrealVersion = {}

---Returns major version of game's Unreal Engine
---@return integer
function UnrealVersion:GetMajor() end

---Returns major version of game's Unreal Engine
---@return integer
function UnrealVersion.GetMajor() end

---Returns minor version of game's Unreal Engine
---@return integer
function UnrealVersion:GetMinor() end

---Returns minor version of game's Unreal Engine
---@return integer
function UnrealVersion.GetMinor() end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion:IsEqual(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion.IsEqual(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion:IsAtLeast(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion.IsAtLeast(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion:IsAtMost(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion.IsAtMost(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion:IsBelow(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion.IsBelow(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion:IsAbove(MajorVersion, MinorVersion) end

---Compares game's Unreal Engine version
---@param MajorVersion integer
---@param MinorVersion integer
---@return boolean
function UnrealVersion.IsAbove(MajorVersion, MinorVersion) end

---@class UFunction : UObject
UFunction = {}

Expand Down
26 changes: 13 additions & 13 deletions assets/Mods/shared/UEHelpers/UEHelpers.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local UEHelpers = {}
-- Uncomment the below require to use the Lua VM profiler on these functions
-- local jsb = require "jsbProfi"
-- local jsb = require("jsbProfiler.jsbProfi")

-- Version 1 does not exist, we start at version 2 because the original version didn't have a version at all.
local Version = 3
Expand Down Expand Up @@ -39,7 +39,7 @@ local EngineCache = CreateInvalidObject() ---@cast EngineCache UEngine
function UEHelpers.GetEngine()
if EngineCache:IsValid() then return EngineCache end

EngineCache = FindFirstOf("Engine") ---@type UEngine
EngineCache = FindFirstOf("Engine") ---@cast EngineCache UEngine
return EngineCache
end

Expand All @@ -49,15 +49,15 @@ local GameInstanceCache = CreateInvalidObject() ---@cast GameInstanceCache UGame
function UEHelpers.GetGameInstance()
if GameInstanceCache:IsValid() then return GameInstanceCache end

GameInstanceCache = FindFirstOf("GameInstance") ---@type UGameInstance
GameInstanceCache = FindFirstOf("GameInstance") ---@cast GameInstanceCache UGameInstance
return GameInstanceCache
end

---Returns the main UGameViewportClient
---@return UGameViewportClient
function UEHelpers.GetGameViewportClient()
local Engine = UEHelpers.GetEngine()
if Engine:IsValid() then
if Engine:IsValid() and Engine.GameViewport then
return Engine.GameViewport
end
return CreateInvalidObject() ---@type UGameViewportClient
Expand All @@ -69,10 +69,12 @@ local PlayerControllerCache = CreateInvalidObject() ---@cast PlayerControllerCac
function UEHelpers.GetPlayerController()
if PlayerControllerCache:IsValid() then return PlayerControllerCache end

local Controllers = FindAllOf("Controller") ---@type AController[]?
-- local Controllers = jsb.simpleBench("FindAllOf: PlayerController", FindAllOf, "PlayerController")
-- Controllers = jsb.simpleBench("FindAllOf: Controller", FindAllOf, "Controller")
local Controllers = FindAllOf("PlayerController") or FindAllOf("Controller") ---@type AController[]?
if Controllers then
for _, Controller in ipairs(Controllers) do
if Controller:IsValid() and Controller:IsPlayerController() then
if Controller:IsValid() and (Controller.IsPlayerController and Controller:IsPlayerController() or Controller:IsLocalPlayerController()) then
PlayerControllerCache = Controller
break
end
Expand All @@ -86,7 +88,7 @@ end
---@return APawn
function UEHelpers.GetPlayer()
local playerController = UEHelpers.GetPlayerController()
if playerController:IsValid() then
if playerController:IsValid() and playerController.Pawn then
return playerController.Pawn
end
return CreateInvalidObject() ---@type APawn
Expand All @@ -101,17 +103,15 @@ function UEHelpers.GetWorld()
local PlayerController = UEHelpers.GetPlayerController()
if PlayerController:IsValid() then
WorldCache = PlayerController:GetWorld()
return WorldCache
end

return WorldCache
end

---Returns UWorld->PersistentLevel
---@return ULevel
function UEHelpers.GetPersistentLevel()
local World = UEHelpers.GetWorld()
if World:IsValid() and World.PersistentLevel:IsValid() then
if World:IsValid() and World.PersistentLevel then
return World.PersistentLevel
end
return CreateInvalidObject() ---@type ULevel
Expand All @@ -122,7 +122,7 @@ end
---@return AGameModeBase
function UEHelpers.GetGameModeBase()
local World = UEHelpers.GetWorld()
if World:IsValid() and World.AuthorityGameMode:IsValid() then
if World:IsValid() and World.AuthorityGameMode then
return World.AuthorityGameMode
end
return CreateInvalidObject() ---@type AGameModeBase
Expand All @@ -133,7 +133,7 @@ end
---@return AGameStateBase
function UEHelpers.GetGameStateBase()
local World = UEHelpers.GetWorld()
if World:IsValid() and World.GameState:IsValid() then
if World:IsValid() and World.GameState then
return World.GameState
end
return CreateInvalidObject() ---@type AGameStateBase
Expand All @@ -143,7 +143,7 @@ end
---@return AWorldSettings
function UEHelpers.GetWorldSettings()
local PersistentLevel = UEHelpers.GetPersistentLevel()
if PersistentLevel:IsValid() and PersistentLevel.WorldSettings:IsValid() then
if PersistentLevel:IsValid() and PersistentLevel.WorldSettings then
return PersistentLevel.WorldSettings
end
return CreateInvalidObject() ---@type AWorldSettings
Expand Down
2 changes: 1 addition & 1 deletion assets/Mods/shared/jsbProfiler/jsbProfi.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ end
-- simple bench
-- 2 simple func that can stand inline of a call (former a single call and the latter can be made to run the call n times)
-- will output the call performance to log file as time to execute in seconds
-- see example UEHelpers line 33
-- see example UEHelpers line 72
function jsb.simpleBench(name, func, ...)
if not name or type(name) ~= "string" then return print("Error in bench args!") end
local data_return
Expand Down

0 comments on commit 8d38572

Please sign in to comment.