Skip to content

Commit

Permalink
fix model freeing infinite loop problem with spawnPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Sep 1, 2024
1 parent b40b0a7 commit 55539ab
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions newmodels_reborn/scripts/core/client_logic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,17 @@ local function loadCustomModel(customModel, elementToApply)
end
end

local function countStreamedElementsWithCustomModel(elementTypes, customModel)
local count = 0
for _, elementType in pairs(elementTypes) do
for _, v in pairs(getElementsByType(elementType, root, true)) do
if getElementData(v, getCustomModelDataKey(elementType)) == customModel then
count = count + 1
local function isCustomModelInUse(customModel, loadedModel)
if loadedModel then
for _, elementType in pairs(loadedModel.elementTypes) do
for _, v in pairs(getElementsByType(elementType, root, true)) do
if getElementData(v, getCustomModelDataKey(elementType)) == customModel then
return true
end
end
end
end
return count
return false
end

local function freeAllocatedModelNow(customModel)
Expand Down Expand Up @@ -186,7 +187,7 @@ local function freeAllocatedModelNow(customModel)
loadedModels[customModel] = nil
end

local function freeAllocatedModel(customModel, loadedModel)
local function freeAllocatedModel(customModel, loadedModel, onlyIfUnused)
if loadedModel.disableAutoFree then
return
end
Expand All @@ -196,18 +197,19 @@ local function freeAllocatedModel(customModel, loadedModel)
end
-- Do not free all models at once, delay each model by a bit
currFreeIdDelay = currFreeIdDelay + FREE_ID_DELAY_STEP

loadedModel.freeAllocatedTimer = setTimer(function()
freeAllocatedModelNow(customModel)
if (not onlyIfUnused) or (onlyIfUnused and not isCustomModelInUse(customModel, loadedModel)) then
freeAllocatedModelNow(customModel)
end
currFreeIdDelay = currFreeIdDelay - FREE_ID_DELAY_STEP
end, currFreeIdDelay, 1)
end

local function freeAllocatedModelIfUnused(customModel)
local loadedModel = loadedModels[customModel]
if not loadedModel then return end
if countStreamedElementsWithCustomModel(loadedModel.elementTypes, customModel) == 0 then
freeAllocatedModel(customModel, loadedModel)
end
freeAllocatedModel(customModel, loadedModel, true)
end

local function setElementCustomModel(element)
Expand Down
2 changes: 1 addition & 1 deletion newmodels_reborn/scripts/optional/debug/s_debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ addCommandHandler("testspawn", function(thePlayer)
if customModel then
setElementData(thePlayer, dataName, customModel)
end
outputChatBox("Player spawned with skin "..( customModel or getElementModelMTA(thePlayer) )..".", thePlayer)
outputChatBox("Player spawned with skin ID "..( customModel or getElementModelMTA(thePlayer) )..".", thePlayer)
end, false, false)

0 comments on commit 55539ab

Please sign in to comment.