Skip to content

Commit

Permalink
Add option to ignore DFF/TXD(/COL) for a model (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fernando-A-Rocha authored Mar 10, 2022
1 parent aa23bea commit 2b868df
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 75 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ This library lets you load mods stored within the `newmodels` resource, and also
Check the [quick testing](#quick-testing) to understand how to load mods from within the `newmodels` resource (easier).

You have at your disposal the following exported functions, [see code to understand](/newmodels/server.lua) and [example to see implementation](/[examples]/newmodels-example/server.lua):
- `addExternalMod_IDFilenames(elementType, id, base_id, name, path)`
- `addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff, path_txd, path_col)`
- `addExternalMod_IDFilenames(elementType, id, base_id, name, path, ignoreTXD, ignoreDFF, ignoreCOL)`
- `addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff, path_txd, path_col, ignoreTXD, ignoreDFF, ignoreCOL)`
- `removeExternalMod(id)`

### Using Custom IDs
Expand Down
82 changes: 43 additions & 39 deletions newmodels/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ function allocateNewMod(element, elementType, id)
if not foundMod then
return false, "Failed to retrieve "..elementType.." mod ID "..id.." from list stored in client"
end

local ignoreTXD, ignoreDFF, ignoreCOL = foundMod.ignoreTXD, foundMod.ignoreDFF, foundMod.ignoreCOL

local paths
local path = foundMod.path
Expand All @@ -80,32 +82,19 @@ function allocateNewMod(element, elementType, id)
paths = getActualModPaths(path, id)
end

local txdpath = paths.txd

if not txdpath then
return false, "Failed to get TXD path for mod ID "..id
end
if not fileExists(txdpath) then
local txdpath = (ignoreTXD ~= true) and paths.txd or nil
if (txdpath ~= nil) and not fileExists(txdpath) then
return false, "File doesn't exist: "..txdpath
end

local dffpath = paths.dff

if not dffpath then
return false, "Failed to get DFF path for mod ID "..id
end
if not fileExists(dffpath) then
local dffpath = (ignoreDFF ~= true) and paths.dff or nil
if (dffpath ~= nil) and not fileExists(dffpath) then
return false, "File doesn't exist: "..dffpath
end

local colpath
if elementType == "object" then
if paths.col then
colpath = paths.col
if not fileExists(colpath) then
return false, "File doesn't exist: "..colpath
end
end
local colpath = (elementType == "object" and ignoreCOL ~= true) and paths.col or nil
if (colpath ~= nil) and not fileExists(colpath) then
return false, "File doesn't exist: "..colpath
end

-- /!\ only this function doesn't accept 'player'
Expand All @@ -118,23 +107,35 @@ function allocateNewMod(element, elementType, id)
return false, "Failed: engineRequestModel('"..elementType2.."')"
end


local allgood = true
local txdworked,dffworked,colworked = false,false,false
local txdmodel,dffmodel,colmodel = nil,nil,nil

local txd = engineLoadTXD(txdpath)
if txd then
txdmodel = txd
if engineImportTXD(txd,allocated_id) then
txdworked = true
if txdpath then
local txd = engineLoadTXD(txdpath)
if txd then
txdmodel = txd
if engineImportTXD(txd,allocated_id) then
txdworked = true
else
allgood = false
end
else
allgood = false
end
end

local dff = engineLoadDFF(dffpath, allocated_id)
if dff then
dffmodel = dff
if engineReplaceModel(dff,allocated_id) then
dffworked = true
if dffpath then
local dff = engineLoadDFF(dffpath, allocated_id)
if dff then
dffmodel = dff
if engineReplaceModel(dff,allocated_id) then
dffworked = true
else
allgood = false
end
else
allgood = false
end
end

Expand All @@ -144,17 +145,14 @@ function allocateNewMod(element, elementType, id)
colmodel = col
if engineReplaceCOL(col, allocated_id) then
colworked = true
else
allgood = false
end
else
allgood = false
end
end

local allgood = false
if col then
allgood = txdworked and dffworked and colworked
else
allgood = txdworked and dffworked
end

if not (allgood) then
engineFreeModel(allocated_id)
if txdmodel then destroyElement(txdmodel) end -- free memory
Expand All @@ -167,7 +165,13 @@ function allocateNewMod(element, elementType, id)

allocated_ids[id] = allocated_id
-- outputDebugString("["..(eventName or "?").."] New "..elementType.." model ID "..id.." allocated to ID "..allocated_id)
model_elements[allocated_id] = {dffmodel,txdmodel} -- Save model elements for destroying on deallocation
model_elements[allocated_id] = {} -- Save model elements for destroying on deallocation
if isElement(dffmodel) then
table.insert(model_elements[allocated_id], dffmodel)
end
if isElement(txdmodel) then
table.insert(model_elements[allocated_id], txdmodel)
end
if isElement(colmodel) then
table.insert(model_elements[allocated_id], colmodel)
end
Expand Down
1 change: 1 addition & 0 deletions newmodels/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<file src="models/80003.txd"/>
<file src="models/80004.dff"/>
<file src="models/80004.txd"/>
<file src="models/80005.dff"/>

<!-- objects -->
<file src="models/50001.dff"/>
Expand Down
16 changes: 9 additions & 7 deletions newmodels/mod_list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ modList = {
-- element type
ped = {

-- ID must be unique and out of the default GTA & SA-MP ID ranges
-- ID must be unique and out of the default GTA (& preferrably SA-MP too) ID ranges

-- Base ID is the model the mod will inherit some properties from
-- doesn't have much effect on peds or objects, but has on vehicles

-- file names should be ID.dff ID.txd (ID.col if it's an object)
-- path can be:
-- local (this resource) when it doesn't start with :
-- external (other resource) when it starts with :
-- a string, in which case it expects files to be named ID.dff or ID.txd in that folder
-- an array, in which case it expects an array of file names like {dff="filepath.dff", txd="filepath.txd"}
--
-- paths defined manually in this file need to be local (this resource)
-- to add a mod from another resource see the examples provided in the documentation.

-- name can be whatever you want (string)

-- name can be whatever you want

{id=20001, base_id=1, path=modsFolder, name="Mafioso 1"},
{id=20003, base_id=1, path=modsFolder, name="Mafioso 2"},
Expand All @@ -44,13 +47,12 @@ modList = {
{id=80002, base_id=489, path=modsFolder, name="02 Landstalker"},
{id=80003, base_id=400, path=modsFolder, name="86 Landstalker 1"},
{id=80004, base_id=400, path=modsFolder, name="98 Landstalker 1"},
{id=80005, base_id=468, path=modsFolder, name="Sanchez Test", ignoreTXD=true},
},

object = {
{id=50001, base_id=1337, path=modsFolder, name="Engine Hoist"},
},

-- Mods added via another resource's script will be appended to this Lua array
}


Expand Down
Binary file added newmodels/models/80005.dff
Binary file not shown.
83 changes: 56 additions & 27 deletions newmodels/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,13 @@ function doModListChecks()
end

-- 4. verify file exists
local ignoreTXD, ignoreDFF, ignoreCOL = mod.ignoreTXD, mod.ignoreDFF, mod.ignoreCOL
local paths = getActualModPaths(mod.path, mod.id)
for k, path in pairs(paths) do
if not fileExists(path) then

-- only check .col exists for objects which actually need it
if (k == "col" and elementType == "object") or (k ~= "col") then
if (not ignoreTXD and k == "txd")
or (not ignoreDFF and k == "dff")
or ((not ignoreCOL) and elementType == "object" and k == "col") then

return modCheckError("File does not exist: '"..tostring(path).."' for mod ID "..mod.id)
end
Expand Down Expand Up @@ -442,7 +443,7 @@ local prevent_addrem_spam = {
The difference between this function and addExternalMod_CustomFilenames is that
you pass a folder path in 'path' and it will search for ID.dff ID.txd etc
]]
function addExternalMod_IDFilenames(elementType, id, base_id, name, path) -- [Exported]
function addExternalMod_IDFilenames(elementType, id, base_id, name, path, ignoreTXD, ignoreDFF, ignoreCOL) -- [Exported]

local sourceResName = getResourceName(sourceResource)
if sourceResName == resName then
Expand Down Expand Up @@ -477,6 +478,15 @@ function addExternalMod_IDFilenames(elementType, id, base_id, name, path) -- [Ex
if not (type(path) == "string") then
return false, "Missing/Invalid 'path' passed: "..tostring(path)
end
if (ignoreTXD ~= nil and type(ignoreTXD) ~= "boolean") then
return false, "ignoreTXD passed must be true/false"
end
if (ignoreDFF ~= nil and type(ignoreDFF) ~= "boolean") then
return false, "ignoreDFF passed must be true/false"
end
if (ignoreCOL ~= nil and type(ignoreCOL) ~= "boolean") then
return false, "ignoreCOL passed must be true/false"
end

if string.sub(path, 1,1) ~= ":" then
path = ":"..sourceResName.."/"..path
Expand All @@ -490,7 +500,7 @@ function addExternalMod_IDFilenames(elementType, id, base_id, name, path) -- [Ex
return false, "'base_id' passed is not a default GTA:SA ID, it needs to be!"
end

for elementType,mods in pairs(modList) do
for elementType2,mods in pairs(modList) do
for k,mod in pairs(mods) do
if mod.id == id then
return false, "Duplicated 'id' passed, already exists in modList"
Expand All @@ -501,10 +511,9 @@ function addExternalMod_IDFilenames(elementType, id, base_id, name, path) -- [Ex
local paths = getActualModPaths(path, id)
for k, path2 in pairs(paths) do
if not fileExists(path2) then

-- only check .col exists for objects which actually need it
if (k == "col" and elementType == "object") or (k ~= "col") then

if (not ignoreTXD and k == "txd")
or (not ignoreDFF and k == "dff")
or ((not ignoreCOL) and elementType == "object" and k == "col") then
return false, "File does not exist: '"..tostring(path2).."', check folder: '"..path.."'"
end
end
Expand Down Expand Up @@ -538,7 +547,7 @@ end
The difference between this function and addExternalMod_IDFilenames is that
you pass directly individual file paths for dff, txd and col files
]]
function addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff, path_txd, path_col) -- [Exported]
function addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff, path_txd, path_col, ignoreTXD, ignoreDFF, ignoreCOL) -- [Exported]

local sourceResName = getResourceName(sourceResource)
if sourceResName == resName then
Expand Down Expand Up @@ -571,25 +580,45 @@ function addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff
return false, "Missing/Invalid 'name' passed: "..tostring(name)
end

local paths = {}

if not (type(path_dff) == "string") then
return false, "Missing/Invalid 'path_dff' passed: "..tostring(path_dff)
if (ignoreTXD ~= nil and type(ignoreTXD) ~= "boolean") then
return false, "ignoreTXD passed must be true/false"
end
if (ignoreDFF ~= nil and type(ignoreDFF) ~= "boolean") then
return false, "ignoreDFF passed must be true/false"
end
if string.sub(path_dff, 1,1) ~= ":" then
path_dff = ":"..sourceResName.."/"..path_dff
if (ignoreCOL ~= nil and type(ignoreCOL) ~= "boolean") then
return false, "ignoreCOL passed must be true/false"
end
paths.dff = path_dff

if not (type(path_txd) == "string") then
return false, "Missing/Invalid 'path_txd' passed: "..tostring(path_txd)

local paths = {}

if (ignoreDFF ~= true) then

if not (type(path_dff) == "string") then
return false, "Missing/Invalid 'path_dff' passed: "..tostring(path_dff)
end
if string.sub(path_dff, 1,1) ~= ":" then
path_dff = ":"..sourceResName.."/"..path_dff
end
paths.dff = path_dff

end
if string.sub(path_txd, 1,1) ~= ":" then
path_txd = ":"..sourceResName.."/"..path_txd

if (ignoreTXD ~= true) then

if not (type(path_txd) == "string") then
return false, "Missing/Invalid 'path_txd' passed: "..tostring(path_txd)
end
if string.sub(path_txd, 1,1) ~= ":" then
path_txd = ":"..sourceResName.."/"..path_txd
end
paths.txd = path_txd

end
paths.txd = path_txd

if path_col then
if (ignoreCOL ~= true and elementType == "object") then

if (type(path_col) ~= "string") then
return false, "Missing/Invalid 'path_col' passed: "..tostring(path_col)
end
Expand All @@ -608,7 +637,7 @@ function addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff
return false, "'base_id' passed is not a default GTA:SA ID, it needs to be!"
end

for elementType,mods in pairs(modList) do
for elementType2,mods in pairs(modList) do
for k,mod in pairs(mods) do
if mod.id == id then
return false, "Duplicated 'id' passed, already exists in modList"
Expand All @@ -617,9 +646,9 @@ function addExternalMod_CustomFilenames(elementType, id, base_id, name, path_dff
end
for k, path2 in pairs(paths) do
if not fileExists(path2) then

-- only check .col exists for objects which actually need it
if (k == "col" and elementType == "object") or (k ~= "col") then
if (not ignoreTXD and k == "txd")
or (not ignoreDFF and k == "dff")
or ((not ignoreCOL) and elementType == "object" and k == "col") then

return false, "File does not exist: '"..tostring(path2).."'"
end
Expand Down

0 comments on commit 2b868df

Please sign in to comment.