Skip to content

Commit

Permalink
Update RoyalMod lib
Browse files Browse the repository at this point in the history
  • Loading branch information
fcelsa committed Apr 10, 2021
1 parent aa2b055 commit 67e4fc9
Show file tree
Hide file tree
Showing 13 changed files with 611 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/lib/rmod/Main.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--- Royal Mod

---@author Royal Modding
---@version 1.4.0.0
---@version 1.5.0.0
---@date 03/12/2020

--- Initialize RoyalMod
Expand Down
152 changes: 132 additions & 20 deletions src/lib/rmod/RoyalMod.lua

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions src/lib/utility/Array.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--- Royal Utility

---@author Royal Modding
---@version 2.1.0.0
---@date 26/02/2021

---@alias Array table<integer, any> Table with numeric indexes only, always ordered and sequential

--- Array utilities class built with performances in mind (with 'array' we mean tables with numeric indexes only, always ordered and sequential)
---@class ArrayUtility
ArrayUtility = ArrayUtility or {}

--- Remove matching elements from an array
---@param array Array
---@param removeFunc fun(array: Array, index: number, moveAt: number): boolean | "function(array, index, moveAt) local element = array[index] return true end"
---@return number removedCount count of removed elements
function ArrayUtility.remove(array, removeFunc)
local removedCount = 0
local moveAt, length = 1, #array
for index = 1, length do
if removeFunc(array, index, moveAt) then
array[index] = nil
removedCount = removedCount + 1
else
-- move kept element's value to moveAt's position, if it's not already there
if (index ~= moveAt) then
array[moveAt] = array[index]
array[index] = nil
end
-- increment position of where we'll place the next kept value
moveAt = moveAt + 1
end
end
return removedCount
end

--- Remove element at the given index from an array
---@param array Array
---@param index number
---@return Array
function ArrayUtility.removeAt(array, index)
ArrayUtility.remove(
array,
function(_, i)
return index == i
end
)
end
121 changes: 62 additions & 59 deletions src/lib/utility/UtilityDebug.lua → src/lib/utility/Debug.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
--- Royal Utility

---@author Royal Modding
---@version 1.8.0.0
---@version 2.1.0.0
---@date 05/01/2021

---@class DebugUtility
DebugUtility = DebugUtility or {}

--- Render a table (for debugging purpose)
---@param posX number
---@param posY number
---@param textSize number
---@param inputTable table
---@param maxDepth integer|nil
---@param hideFunc boolean|nil
function Utility.renderTable(posX, posY, textSize, inputTable, maxDepth, hideFunc)
function DebugUtility.renderTable(posX, posY, textSize, inputTable, maxDepth, hideFunc)
inputTable = inputTable or {tableIs = "nil"}
hideFunc = hideFunc or false
maxDepth = maxDepth or 2
Expand Down Expand Up @@ -67,7 +70,7 @@ end
---@param textSize number
---@param inputNode integer
---@param maxDepth integer|nil
function Utility.renderNodeHierarchy(posX, posY, textSize, inputNode, maxDepth)
function DebugUtility.renderNodeHierarchy(posX, posY, textSize, inputNode, maxDepth)
if inputNode == nil or inputNode == 0 then
return
end
Expand All @@ -79,7 +82,7 @@ function Utility.renderNodeHierarchy(posX, posY, textSize, inputNode, maxDepth)
return i
end
local offset = i * textSize * 1.05
local _, className = Utility.getObjectClass(node)
local _, className = EntityUtility.getObjectClass(node)
renderText(x, posY - offset, textSize, string.format("%s (%s)", getName(node), className))
i = i + 1
for ni = 0, getNumOfChildren(node) - 1 do
Expand All @@ -92,7 +95,7 @@ function Utility.renderNodeHierarchy(posX, posY, textSize, inputNode, maxDepth)
setTextColor(1, 1, 1, 1)
setTextBold(false)
textSize = getCorrectTextSize(textSize)
local _, className = Utility.getObjectClass(inputNode)
local _, className = EntityUtility.getObjectClass(inputNode)
renderText(posX, posY, textSize, string.format("%s (%s)", getName(inputNode), className))
for ni = 0, getNumOfChildren(inputNode) - 1 do
i = renderNodeHierarchyRecursively(posX + textSize * 1.8, getChildAt(inputNode, ni), 1, i)
Expand All @@ -115,7 +118,7 @@ end
---@param ag number g color if active
---@param ab number b color if active
---@param active boolean active?
function Utility.drawDebugRectangle(node, minX, maxX, minZ, maxZ, yOffset, alignToGround, r, g, b, ar, ag, ab, active)
function DebugUtility.drawDebugRectangle(node, minX, maxX, minZ, maxZ, yOffset, alignToGround, r, g, b, ar, ag, ab, active)
if active then
r, g, b = ar, ag, ab
end
Expand Down Expand Up @@ -149,7 +152,7 @@ end
---@param ag number g color if active
---@param ab number b color if active
---@param active boolean active?
function Utility.drawDebugCube(node, size, r, g, b, ar, ag, ab, active)
function DebugUtility.drawDebugCube(node, size, r, g, b, ar, ag, ab, active)
if active then
r, g, b = ar, ag, ab
end
Expand All @@ -174,31 +177,31 @@ function Utility.drawDebugCube(node, size, r, g, b, ar, ag, ab, active)
corners[7] = {x - offsets, y - offsets, z - offsets}
corners[8] = {x - offsets, y - offsets, z + offsets}

Utility.drawDebugLine(corners[1], corners[2], 0, 0, 0)
Utility.drawDebugLine(corners[2], corners[3], 0, 0, 0)
Utility.drawDebugLine(corners[3], corners[4], 0, 0, 0)
Utility.drawDebugLine(corners[4], corners[1], 0, 0, 0)
Utility.drawDebugLine(corners[1], corners[5], 0, 0, 0)
Utility.drawDebugLine(corners[2], corners[6], 0, 0, 0)
Utility.drawDebugLine(corners[3], corners[7], 0, 0, 0)
Utility.drawDebugLine(corners[4], corners[8], 0, 0, 0)
Utility.drawDebugLine(corners[5], corners[6], 0, 0, 0)
Utility.drawDebugLine(corners[6], corners[7], 0, 0, 0)
Utility.drawDebugLine(corners[7], corners[8], 0, 0, 0)
Utility.drawDebugLine(corners[8], corners[5], 0, 0, 0)
DebugUtility.drawDebugLine(corners[1], corners[2], 0, 0, 0)
DebugUtility.drawDebugLine(corners[2], corners[3], 0, 0, 0)
DebugUtility.drawDebugLine(corners[3], corners[4], 0, 0, 0)
DebugUtility.drawDebugLine(corners[4], corners[1], 0, 0, 0)
DebugUtility.drawDebugLine(corners[1], corners[5], 0, 0, 0)
DebugUtility.drawDebugLine(corners[2], corners[6], 0, 0, 0)
DebugUtility.drawDebugLine(corners[3], corners[7], 0, 0, 0)
DebugUtility.drawDebugLine(corners[4], corners[8], 0, 0, 0)
DebugUtility.drawDebugLine(corners[5], corners[6], 0, 0, 0)
DebugUtility.drawDebugLine(corners[6], corners[7], 0, 0, 0)
DebugUtility.drawDebugLine(corners[7], corners[8], 0, 0, 0)
DebugUtility.drawDebugLine(corners[8], corners[5], 0, 0, 0)

Utility.drawDebugTriangle(corners[3], corners[2], corners[1], r, g, b)
Utility.drawDebugTriangle(corners[1], corners[4], corners[3], r, g, b)
Utility.drawDebugTriangle(corners[4], corners[1], corners[5], r, g, b)
Utility.drawDebugTriangle(corners[5], corners[8], corners[4], r, g, b)
Utility.drawDebugTriangle(corners[8], corners[5], corners[6], r, g, b)
Utility.drawDebugTriangle(corners[6], corners[7], corners[8], r, g, b)
Utility.drawDebugTriangle(corners[7], corners[6], corners[2], r, g, b)
Utility.drawDebugTriangle(corners[2], corners[3], corners[7], r, g, b)
Utility.drawDebugTriangle(corners[2], corners[6], corners[5], r, g, b)
Utility.drawDebugTriangle(corners[5], corners[1], corners[2], r, g, b)
Utility.drawDebugTriangle(corners[4], corners[8], corners[7], r, g, b)
Utility.drawDebugTriangle(corners[7], corners[3], corners[4], r, g, b)
DebugUtility.drawDebugTriangle(corners[3], corners[2], corners[1], r, g, b)
DebugUtility.drawDebugTriangle(corners[1], corners[4], corners[3], r, g, b)
DebugUtility.drawDebugTriangle(corners[4], corners[1], corners[5], r, g, b)
DebugUtility.drawDebugTriangle(corners[5], corners[8], corners[4], r, g, b)
DebugUtility.drawDebugTriangle(corners[8], corners[5], corners[6], r, g, b)
DebugUtility.drawDebugTriangle(corners[6], corners[7], corners[8], r, g, b)
DebugUtility.drawDebugTriangle(corners[7], corners[6], corners[2], r, g, b)
DebugUtility.drawDebugTriangle(corners[2], corners[3], corners[7], r, g, b)
DebugUtility.drawDebugTriangle(corners[2], corners[6], corners[5], r, g, b)
DebugUtility.drawDebugTriangle(corners[5], corners[1], corners[2], r, g, b)
DebugUtility.drawDebugTriangle(corners[4], corners[8], corners[7], r, g, b)
DebugUtility.drawDebugTriangle(corners[7], corners[3], corners[4], r, g, b)
end

--- Draw a triangle (for debugging purpose)
Expand All @@ -208,7 +211,7 @@ end
---@param r number r
---@param g number g
---@param b number b
function Utility.drawDebugTriangle(c1, c2, c3, r, g, b)
function DebugUtility.drawDebugTriangle(c1, c2, c3, r, g, b)
drawDebugTriangle(c1[1], c1[2], c1[3], c2[1], c2[2], c2[3], c3[1], c3[2], c3[3], r, g, b, 1, false)
end

Expand All @@ -218,7 +221,7 @@ end
---@param r number r
---@param g number g
---@param b number b
function Utility.drawDebugLine(p1, p2, r, g, b)
function DebugUtility.drawDebugLine(p1, p2, r, g, b)
drawDebugLine(p1[1], p1[2], p1[3], r, g, b, p2[1], p2[2], p2[3], r, g, b)
end

Expand All @@ -229,7 +232,7 @@ end
---@param h number height
---@param curve table AnimCurve object
---@param numPointsToShow? integer number of points to render
function Utility.renderAnimCurve(x, y, w, h, curve, numPointsToShow)
function DebugUtility.renderAnimCurve(x, y, w, h, curve, numPointsToShow)
local graph = curve.debugGraph
local numPoints = numPointsToShow or #curve.keyframes
local minTime = 0
Expand Down Expand Up @@ -261,45 +264,45 @@ end

--- Get the loading speed meter object
---@return LoadingSpeedMeter loadingSpeedMeter
function Utility.getVehicleLoadingSpeedMeter()
if Utility.loadingSpeedMeter == nil then
function DebugUtility.getVehicleLoadingSpeedMeter()
if DebugUtility.loadingSpeedMeter == nil then
---@class LoadingSpeedMeter
Utility.loadingSpeedMeter = {}
Utility.loadingSpeedMeter.vehicles = {}
Utility.loadingSpeedMeter.filters = {}
DebugUtility.loadingSpeedMeter = {}
DebugUtility.loadingSpeedMeter.vehicles = {}
DebugUtility.loadingSpeedMeter.filters = {}
--- Add a new filter
---@param filterFunction function | 'function(vehicleData) return true, "meter name" end'
Utility.loadingSpeedMeter.addFilter = function(filterFunction)
table.insert(Utility.loadingSpeedMeter.filters, filterFunction)
DebugUtility.loadingSpeedMeter.addFilter = function(filterFunction)
table.insert(DebugUtility.loadingSpeedMeter.filters, filterFunction)
end
Utility.overwrittenFunction(
Vehicle,
"load",
function(self, superFunc, vehicleData, asyncCallbackFunction, asyncCallbackObject, asyncCallbackArguments)
local smEnabled = false
local smName = ""
for _, filter in ipairs(Utility.loadingSpeedMeter.filters) do
for _, filter in ipairs(DebugUtility.loadingSpeedMeter.filters) do
smEnabled, smName = filter(vehicleData)
if smEnabled then
break
end
end

if smEnabled then
Utility.loadingSpeedMeter.vehicles[self] = {}
Utility.loadingSpeedMeter.vehicles[self].smName = smName
Utility.loadingSpeedMeter.vehicles[self].totalStartTime = getTimeSec()
DebugUtility.loadingSpeedMeter.vehicles[self] = {}
DebugUtility.loadingSpeedMeter.vehicles[self].smName = smName
DebugUtility.loadingSpeedMeter.vehicles[self].totalStartTime = getTimeSec()
end

local state = superFunc(self, vehicleData, asyncCallbackFunction, asyncCallbackObject, asyncCallbackArguments)

if smEnabled then
Utility.loadingSpeedMeter.vehicles[self].totalTime = getTimeSec() - Utility.loadingSpeedMeter.vehicles[self].totalStartTime
print(string.format("[%s] Pre time: %.4f ms", Utility.loadingSpeedMeter.vehicles[self].smName, (Utility.loadingSpeedMeter.vehicles[self].preLoadTime or 0) * 1000))
print(string.format("[%s] Load time: %.4f ms", Utility.loadingSpeedMeter.vehicles[self].smName, (Utility.loadingSpeedMeter.vehicles[self].loadTime or 0) * 1000))
print(string.format("[%s] Post time: %.4f ms", Utility.loadingSpeedMeter.vehicles[self].smName, (Utility.loadingSpeedMeter.vehicles[self].postLoadTime or 0) * 1000))
print(string.format("[%s] Total time: %.4f ms", Utility.loadingSpeedMeter.vehicles[self].smName, (Utility.loadingSpeedMeter.vehicles[self].totalTime or 0) * 1000))
Utility.loadingSpeedMeter.vehicles[self] = nil
DebugUtility.loadingSpeedMeter.vehicles[self].totalTime = getTimeSec() - DebugUtility.loadingSpeedMeter.vehicles[self].totalStartTime
print(string.format("[%s] Pre time: %.4f ms", DebugUtility.loadingSpeedMeter.vehicles[self].smName, (DebugUtility.loadingSpeedMeter.vehicles[self].preLoadTime or 0) * 1000))
print(string.format("[%s] Load time: %.4f ms", DebugUtility.loadingSpeedMeter.vehicles[self].smName, (DebugUtility.loadingSpeedMeter.vehicles[self].loadTime or 0) * 1000))
print(string.format("[%s] Post time: %.4f ms", DebugUtility.loadingSpeedMeter.vehicles[self].smName, (DebugUtility.loadingSpeedMeter.vehicles[self].postLoadTime or 0) * 1000))
print(string.format("[%s] Total time: %.4f ms", DebugUtility.loadingSpeedMeter.vehicles[self].smName, (DebugUtility.loadingSpeedMeter.vehicles[self].totalTime or 0) * 1000))
DebugUtility.loadingSpeedMeter.vehicles[self] = nil
end
return state
end
Expand All @@ -308,27 +311,27 @@ function Utility.getVehicleLoadingSpeedMeter()
SpecializationUtil,
"raiseEvent",
function(superFunc, vehicle, eventName, ...)
if Utility.loadingSpeedMeter.vehicles[vehicle] ~= nil then
if DebugUtility.loadingSpeedMeter.vehicles[vehicle] ~= nil then
if eventName == "onPreLoad" then
Utility.loadingSpeedMeter.vehicles[vehicle].preLoadStartTime = getTimeSec()
DebugUtility.loadingSpeedMeter.vehicles[vehicle].preLoadStartTime = getTimeSec()
superFunc(vehicle, eventName, ...)
Utility.loadingSpeedMeter.vehicles[vehicle].preLoadTime = getTimeSec() - Utility.loadingSpeedMeter.vehicles[vehicle].preLoadStartTime
DebugUtility.loadingSpeedMeter.vehicles[vehicle].preLoadTime = getTimeSec() - DebugUtility.loadingSpeedMeter.vehicles[vehicle].preLoadStartTime
end
if eventName == "onLoad" then
Utility.loadingSpeedMeter.vehicles[vehicle].loadStartTime = getTimeSec()
DebugUtility.loadingSpeedMeter.vehicles[vehicle].loadStartTime = getTimeSec()
superFunc(vehicle, eventName, ...)
Utility.loadingSpeedMeter.vehicles[vehicle].loadTime = getTimeSec() - Utility.loadingSpeedMeter.vehicles[vehicle].loadStartTime
DebugUtility.loadingSpeedMeter.vehicles[vehicle].loadTime = getTimeSec() - DebugUtility.loadingSpeedMeter.vehicles[vehicle].loadStartTime
end
if eventName == "onPostLoad" then
Utility.loadingSpeedMeter.vehicles[vehicle].postLoadStartTime = getTimeSec()
DebugUtility.loadingSpeedMeter.vehicles[vehicle].postLoadStartTime = getTimeSec()
superFunc(vehicle, eventName, ...)
Utility.loadingSpeedMeter.vehicles[vehicle].postLoadTime = getTimeSec() - Utility.loadingSpeedMeter.vehicles[vehicle].postLoadStartTime
DebugUtility.loadingSpeedMeter.vehicles[vehicle].postLoadTime = getTimeSec() - DebugUtility.loadingSpeedMeter.vehicles[vehicle].postLoadStartTime
end
else
superFunc(vehicle, eventName, ...)
end
end
)
end
return Utility.loadingSpeedMeter
return DebugUtility.loadingSpeedMeter
end
63 changes: 63 additions & 0 deletions src/lib/utility/DelayedCallBack.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
--- Royal Utility

---@author Royal Modding
---@version 2.1.0.0
---@date 08/03/17

---@class DelayedCallBack
DelayedCallBack = {}

---@param callback function
---@param callbackObject any
---@return DelayedCallBack
function DelayedCallBack:new(callback, callbackObject)
if DelayedCallBack_mt == nil then
DelayedCallBack_mt = Class(DelayedCallBack)
end

---@type DelayedCallBack
local dcb = setmetatable({}, DelayedCallBack_mt)
dcb.callBack = callback
dcb.callbackObject = callbackObject
dcb.callbackCalled = true
dcb.delay = 0
dcb.timer = 0
dcb.skipOneFrame = false
return dcb
end

---@param dt number
function DelayedCallBack:update(dt)
if not self.callbackCalled then
if not self.skipOneFrame then
self.timer = self.timer + dt
end
if self.timer >= self.delay then
self:callCallBack()
end
if self.skipOneFrame then
self.timer = self.timer + dt
end
end
end

---@param delay number
function DelayedCallBack:call(delay, ...)
self.callbackCalled = false
self.callbackParams = {...}
if delay == nil or delay == 0 then
self:callCallBack()
else
self.delay = delay
self.timer = 0
end
end

function DelayedCallBack:callCallBack()
if self.callbackObject ~= nil then
self.callBack(self.callbackObject, unpack(self.callbackParams))
else
self.callBack(unpack(self.callbackParams))
end
self.callbackCalled = true
end
Loading

0 comments on commit 67e4fc9

Please sign in to comment.