Skip to content

Commit

Permalink
add custom model settings in .txt
Browse files Browse the repository at this point in the history
disableAutoFree, disableTextureFiltering, enableAlphaTransparency
  • Loading branch information
Fernando-A-Rocha committed Aug 1, 2024
1 parent 4cb80c2 commit 385725b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
2 changes: 1 addition & 1 deletion newmodels_reborn/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<!-- IMPORTANT TO HAVE THIS VERSION OR HIGHER SO THAT IT WORKS AS EXPECTED
https://nightly.mtasa.com -->
<min_mta_version client="1.6.0-9.22505.0" server="1.6.0-9.22505.0"></min_mta_version>
<min_mta_version client="1.6.0-9.22649.0" server="1.6.0-9.22649.0"></min_mta_version>

<!-- Main Scripts -->
<script src="scripts/core/shared_local.lua" type="shared"/>
Expand Down
3 changes: 3 additions & 0 deletions newmodels_reborn/models/vehicle/490/-1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
disableAutoFree
disableTextureFiltering
enableAlphaTransparency
28 changes: 20 additions & 8 deletions newmodels_reborn/scripts/core/client_logic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ loadedModels = {}

local reusableModelElements = {}

local currFreeIdDelay = 9500 -- ms
local currFreeIdDelay = 9500 -- ms
local FREE_ID_DELAY_STEP = 500 -- ms

local function applyElementCustomModel(element)
Expand Down Expand Up @@ -61,20 +61,22 @@ local function loadCustomModel(customModel, elementToApply)

local colPath, txdPath, dffPath = customInfo.col, customInfo.txd, customInfo.dff

local disableTextureFiltering = customInfo.settings.disableTextureFiltering

local col, txd, dff
if colPath then
col = reusableModelElements[colPath] or engineLoadCOL(colPath)
end
if txdPath then
txd = reusableModelElements[txdPath] or engineLoadTXD(txdPath)
txd = reusableModelElements[txdPath] or engineLoadTXD(txdPath, disableTextureFiltering and false or nil)
end
if dffPath then
dff = reusableModelElements[dffPath] or engineLoadDFF(dffPath)
end

if (colPath and not col)
or (txdPath and not txd)
or (dffPath and not dff) then
or (txdPath and not txd)
or (dffPath and not dff) then
if col and isElement(col) then destroyElement(col) end
if txd and isElement(txd) then destroyElement(txd) end
if dff and isElement(dff) then destroyElement(dff) end
Expand All @@ -83,9 +85,11 @@ local function loadCustomModel(customModel, elementToApply)
return
end

local enableAlphaTransparency = customInfo.settings.enableAlphaTransparency

if (col and not engineReplaceCOL(col, allocatedModel))
or (txd and not engineImportTXD(txd, allocatedModel))
or (dff and not engineReplaceModel(dff, allocatedModel)) then
or (txd and not engineImportTXD(txd, allocatedModel))
or (dff and not engineReplaceModel(dff, allocatedModel, enableAlphaTransparency or nil)) then
if col and isElement(col) then destroyElement(col) end
if txd and isElement(txd) then destroyElement(txd) end
if dff and isElement(dff) then destroyElement(dff) end
Expand Down Expand Up @@ -113,14 +117,17 @@ local function loadCustomModel(customModel, elementToApply)
reusableModelElements[dffPath] = dff
end

local disableAutoFree = customInfo.settings.disableAutoFree

-- Set loadedModel info
loadedModels[customModel] = {
id = allocatedModel,
baseModel = customInfo.baseModel,
name = customInfo.name,
elementTypes = elementTypes,
freeAllocatedTimer = nil,
modelPaths = { txd = txdPath, dff = dffPath, col = colPath }
modelPaths = { txd = txdPath, dff = dffPath, col = colPath },
disableAutoFree = disableAutoFree or false,
}

if isElement(elementToApply) then
Expand Down Expand Up @@ -150,7 +157,7 @@ local function freeAllocatedModelNow(customModel)
engineFreeModel(loadedModel.id)

-- Destroy model elements unless used by another loaded model
for _, modelType in pairs({"dff", "txd", "col"}) do
for _, modelType in pairs({ "dff", "txd", "col" }) do
local modelPath = loadedModel.modelPaths[modelType]
if modelPath and reusableModelElements[modelPath] then
-- Check if another loaded model uses this model element
Expand All @@ -175,6 +182,11 @@ local function freeAllocatedModelNow(customModel)
end

local function freeAllocatedModel(customModel, loadedModel)
if loadedModel.disableAutoFree then
print("Not freeing custom model " .. customModel .. " because it has disableAutoFree set")
return
end

if isTimer(loadedModel.freeAllocatedTimer) then
killTimer(loadedModel.freeAllocatedTimer)
end
Expand Down
69 changes: 49 additions & 20 deletions newmodels_reborn/scripts/core/server_logic.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
local CUSTOM_MODEL_SETTINGS = {
["disableAutoFree"] = true,
["disableTextureFiltering"] = true,
["enableAlphaTransparency"] = true,
}

local function loadModels()
if not pathIsDirectory("models") then
return false, "models directory not found"
Expand Down Expand Up @@ -29,25 +35,47 @@ local function loadModels()
local customModelInfo = {}
local function parseOneFile(thisFileName, thisFullPath, name)
local fileType = string.sub(thisFileName, -3)
if not (fileType == "dff" or fileType == "txd" or fileType == "col") then
return false, "invalid " .. modelType .. " file type: " .. fileType
end
local customModel = tonumber(string.sub(thisFileName, 1, -5))
if not customModel then
return false, "invalid " .. modelType .. " custom model: " .. thisFileName
end
if isDefaultID(modelType, customModel) then
return false, "custom " .. modelType .. " model is a default ID: " .. customModel
end
if customModels[customModel] then
return false, "duplicate " .. modelType .. " custom model: " .. customModel
end
if not customModelInfo[customModel] then
customModelInfo[customModel] = {}
end
customModelInfo[customModel][fileType] = thisFullPath
if name then
customModelInfo[customModel].name = name
if (fileType == "dff" or fileType == "txd" or fileType == "col" or fileType == "txt") then
local customModel = tonumber(string.sub(thisFileName, 1, -5))
if not customModel then
return false, "invalid " .. modelType .. " custom model: " .. thisFileName
end
if not customModelInfo[customModel] then
if isDefaultID(modelType, customModel) then
return false, "custom " .. modelType .. " model is a default ID: " .. customModel
end
if customModels[customModel] then
return false, "duplicate " .. modelType .. " custom model: " .. customModel
end
customModelInfo[customModel] = {}
end
if fileType == "txt" then
local file = fileOpen(thisFullPath, true)
if not file then
return false, "failed to open file: " .. thisFullPath
end
local info = fileGetContents(file, false)
fileClose(file)
if not info then
return false, "failed to read file: " .. thisFullPath
end
local customModelSettings = {}
local lines = split(info, "\n")
for _, settingName in pairs(lines) do
settingName = settingName:gsub("\r", "")
local settingType = CUSTOM_MODEL_SETTINGS[settingName]
if settingType then
customModelSettings[settingName] = true
end
end
customModelInfo[customModel].settings = customModelSettings
iprint(customModelSettings)
else
customModelInfo[customModel][fileType] = thisFullPath
if name then
customModelInfo[customModel].name = name
end
end
end
return true
end
Expand Down Expand Up @@ -79,7 +107,8 @@ local function loadModels()
dff = info.dff,
txd = info.txd,
col = info.col,
name = info.name or "Unnamed"
name = info.name or "Unnamed",
settings = info.settings or {},
}
end
end
Expand Down

0 comments on commit 385725b

Please sign in to comment.