Skip to content

Commit

Permalink
add settings option
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha committed Aug 1, 2024
1 parent 88da9f0 commit 095475c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 39 deletions.
1 change: 1 addition & 0 deletions newmodels_reborn/models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ See the example files to visualize the structure of this system
- `dff=PATH_TO_DFF_FILE_INSIDE_models_FOLDER` (used for shared models)
- `col=PATH_TO_COL_FILE_INSIDE_models_FOLDER` (used for shared collisions)
- `lodDistance=NUMBER` (used for setting https://wiki.multitheftauto.com/wiki/EngineSetModelLODDistance)
- `settings=PATH_TO_ANOTHER_SETTINGS_FILE_INSIDE_models_FOLDER` (in case you want to share the same settings between multiple models)
3 changes: 1 addition & 2 deletions newmodels_reborn/models/object/1337/big_box/40001.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
txd=object/1337/boxes.txd
lodDistance=300
settings=object/1337/boxes.txt
2 changes: 2 additions & 0 deletions newmodels_reborn/models/object/1337/boxes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
txd=object/1337/boxes.txd
lodDistance=300
2 changes: 1 addition & 1 deletion newmodels_reborn/models/object/1337/small_box/40002.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
txd=object/1337/boxes.txd
settings=object/1337/boxes.txt
96 changes: 60 additions & 36 deletions newmodels_reborn/scripts/core/server_logic.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,67 @@ local CUSTOM_MODEL_SETTINGS = {
-- - dff=path
-- - col=path
-- - lodDistance=number
-- - settings=path

local function stringStartswith(str, start)
return str:sub(1, #start) == start
end

local function parseModelSettings(thisFullPath, customModel, customModelInfo, isFromSettingsOption)
local customModelSettings = {}
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 lines = split(info, "\n")
for _, settingStr in pairs(lines) do
settingStr = settingStr:gsub("\r", "")
if CUSTOM_MODEL_SETTINGS[settingStr] then
customModelSettings[settingStr] = true
elseif stringStartswith(settingStr, "lodDistance=") then
local lodDistance = tonumber(settingStr:sub(13))
if not lodDistance then
return false, "invalid lodDistance value: " .. settingStr
end
customModelSettings.lodDistance = lodDistance
elseif stringStartswith(settingStr, "settings=") then
if isFromSettingsOption then
return false, "settings option cannot point to a settings file that contains another settings option @ " .. thisFullPath
end
local settingsPath = settingStr:sub(10)
local settingsFullPath = "models/" .. settingsPath
if not fileExists(settingsFullPath) then
return false, "settings file not found: " .. settingsPath
end
local settingsInfo = parseModelSettings(settingsFullPath, customModel, customModelInfo, true)
if not settingsInfo then
return false, "failed to parse settings file: " .. settingsPath
end
return settingsInfo
else
for _, settingModelType in pairs({"txd", "dff", "col"}) do
if stringStartswith(settingStr, settingModelType.."=") then
local settingModelPath = settingStr:sub(#settingModelType + 2)
local settingModelFullPath = "models/" .. settingModelPath
if not fileExists(settingModelFullPath) then
return false, "setting " .. settingModelType .. " file not found: " .. settingModelPath
end
if customModelInfo[customModel][settingModelType] then
return false, "duplicate " .. settingModelType .. " file for custom model: " .. customModel
end
customModelInfo[customModel][settingModelType] = settingModelFullPath
end
end
end
end
return customModelSettings
end

local function loadModels()
if not pathIsDirectory("models") then
return false, "models directory not found"
Expand Down Expand Up @@ -58,42 +114,9 @@ local function loadModels()
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 _, settingStr in pairs(lines) do
settingStr = settingStr:gsub("\r", "")
if CUSTOM_MODEL_SETTINGS[settingStr] then
customModelSettings[settingStr] = true
elseif stringStartswith(settingStr, "lodDistance=") then
local lodDistance = tonumber(settingStr:sub(13))
if not lodDistance then
return false, "invalid lodDistance value: " .. settingStr
end
customModelSettings.lodDistance = lodDistance
else
for _, settingModelType in pairs({"txd", "dff", "col"}) do
if stringStartswith(settingStr, settingModelType.."=") then
local settingModelPath = settingStr:sub(#settingModelType + 2)
local settingModelFullPath = "models/" .. settingModelPath
if not fileExists(settingModelFullPath) then
return false, "setting " .. settingModelType .. " file not found: " .. settingModelPath
end
if customModelInfo[customModel][settingModelType] then
return false, "duplicate " .. settingModelType .. " file for custom " .. modelType .. " model: " .. customModel
end
customModelInfo[customModel][settingModelType] = settingModelFullPath
end
end
end
local customModelSettings, failReason = parseModelSettings(thisFullPath, customModel, customModelInfo)
if not customModelSettings then
return false, failReason
end
customModelInfo[customModel].settings = customModelSettings
else
Expand Down Expand Up @@ -140,6 +163,7 @@ local function loadModels()
name = info.name or "Unnamed",
settings = info.settings or {},
}
if modelType == "object" then iprint(customModel, customModels[customModel]) end
end
end
end
Expand Down

0 comments on commit 095475c

Please sign in to comment.