Skip to content

Commit f9a4e98

Browse files
Fix downloadFile for external files (like SAMP Objects) - v6.0.1
1 parent c6178fa commit f9a4e98

File tree

10 files changed

+1495
-1487
lines changed

10 files changed

+1495
-1487
lines changed

[examples]/sampobj_red/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ With [`newmodels_red`](https://github.com/Fernando-A-Rocha/mta-add-models) (and
44

55
## Large Size ⚠️
66

7-
There are over 1,400 SA-MP object models! It is recommended to keep `download="false"` in the resource's `meta.xml` as well as `metaDownloadFalse=true` in `server.lua` to prevent clients from downloading all the models when they join your server.
7+
There are over 1,400 SA-MP object models! It is recommended to keep `download="false"` in the resource's `meta.xml` as well as `metaDownloadFalse=true` in `s_load.lua` to prevent clients from downloading all the models when they join your server.
88

99
## How to install
1010

[examples]/sampobj_red/meta.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<info author="Fernando" name="SAMP Objects Red"
77
description="uses newmodels functions to add all SA-MP objects"
8-
version="6.0.0"
8+
version="6.0.1"
99
type="script" />
1010

1111
<!-- IMPORTANT TO HAVE THIS VERSION OR HIGHER SO THAT IT WORKS AS EXPECTED
@@ -16,5 +16,6 @@
1616
<file src="models/*.txd" download="false"/>
1717
<file src="models/*.dff" download="false"/>
1818

19-
<script type="server" src="server.lua" />
19+
<script type="server" src="s_data.lua" />
20+
<script type="server" src="s_load.lua" />
2021
</meta>

[examples]/sampobj_red/s_data.lua

Lines changed: 1439 additions & 0 deletions
Large diffs are not rendered by default.

[examples]/sampobj_red/s_load.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--[[
2+
Author: https://github.com/Fernando-A-Rocha
3+
4+
SA-MP Objects [Red] for Newmodels v6
5+
6+
Adds all SA-MP objects to the game using the newmodels system
7+
]]
8+
9+
addEventHandler("onResourceStart", resourceRoot, function()
10+
local MODELS_FOLDER = "models/"
11+
local folderPath = ":" .. getResourceName(resource) .. "/" .. MODELS_FOLDER
12+
local listToAdd = {}
13+
14+
for id, modelInfo in pairs(getSAMPObjectModels()) do
15+
listToAdd[#listToAdd + 1] = {
16+
type = "object",
17+
base_id = 1337,
18+
id = id,
19+
name = modelInfo[1]:gsub(".dff", ""),
20+
path = {
21+
dff = folderPath .. id .. ".dff",
22+
txd = folderPath .. id .. ".txd",
23+
col = folderPath .. id .. ".col",
24+
},
25+
metaDownloadFalse = true,
26+
}
27+
end
28+
29+
-- Async loading
30+
exports["newmodels_red"]:addExternalModels(listToAdd, true)
31+
end, false)

[examples]/sampobj_red/server.lua

Lines changed: 0 additions & 1467 deletions
This file was deleted.

[examples]/test_vehicles/meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<meta>
2-
<include resource="newmodels_red" minversion="6.0.0"/>
2+
<include resource="newmodels_red" minversion="6.0.1"/>
33
<script src="s_vehicles.lua" type="server"/>
44
<script src="s_vehicles_alt.lua" type="server"/>
55
<script src="s_test_cmd.lua" type="server"/>

newmodels_red/meta.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<meta>
2-
<info author="FernandoMTA" version="6.0.0" type="script" name="newmodels_red"
2+
<info author="FernandoMTA" version="6.0.1" type="script" name="newmodels_red"
33
description="Codename Newmodels Red - Adds new vehicle/ped/object models, automatically synced with all players."/>
44

55
<!-- IMPORTANT TO HAVE THIS VERSION OR HIGHER SO THAT IT WORKS AS EXPECTED

newmodels_red/scripts/core/client_logic.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ local function finishLoadCustomModel(customModel)
139139
},
140140
disableAutoFree = disableAutoFree or false,
141141
}
142-
143142
if isElement(elementToApply) then
144143
applyElementCustomModel(elementToApply)
145144
end
@@ -282,7 +281,11 @@ local function onFailedToDownloadModFile(customModel, filePath)
282281
end
283282

284283
-- Handle file downloads requested by this resource
285-
addEventHandler("onClientFileDownloadComplete", resourceRoot, function(filePath, success)
284+
addEventHandler("onClientFileDownloadComplete", root, function(filePath, success)
285+
if source ~= resourceRoot then
286+
-- Append resource name to the path
287+
filePath = ":" .. getElementID(source) .. "/" .. filePath
288+
end
286289
if filesBeingPreDownloaded[filePath] then
287290
if not success then
288291
outputDebugString(
@@ -341,7 +344,7 @@ addEventHandler("onClientFileDownloadComplete", resourceRoot, function(filePath,
341344
end
342345
end
343346
end
344-
end, false)
347+
end)
345348

346349
local function beginDownloadModelFiles(customModel)
347350
local queuedInfo = loadingQueue[customModel]
@@ -362,7 +365,7 @@ local function beginDownloadModelFiles(customModel)
362365

363366
loadingQueue[customModel].countFilesDownloaded = 0
364367

365-
-- local totalFilesCount = #queuedInfo.filesList
368+
local totalFilesCount = #queuedInfo.filesList
366369
-- print(customModel, "downloading files...", totalFilesCount)
367370

368371
for _, fileInfo in pairs(queuedInfo.filesList) do
@@ -395,7 +398,6 @@ local function beginLoadCustomModel(customModel, elementToApply)
395398
-- Start in Phase 1
396399
phase = LOADING_QUEUE_PHASES.DOWNLOAD_FILES,
397400
}
398-
399401
beginDownloadModelFiles(customModel)
400402
end
401403

newmodels_red/scripts/core/shared_exported.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ if not IS_IMPORTED then
185185
-- In MTA Elements are always destroyed when the resource that created them is stopped: this cannot be changed.
186186
-- So we use an internal table to keep track of elements created by resources.
187187
newmodelsUtils.assignElementToResource = function(theElement, theResource)
188-
if not theResource then return end
188+
if not isElement(theElement) then return end
189+
if not (isElement(theResource) and getElementType(theResource) == "resource") then return end
189190
local resRoot = getResourceRootElement(theResource)
190191
if not resRoot then return end
191-
if not isElement(theElement) then return end
192192
local theResName = getResourceName(theResource)
193193
if not newmodelsUtils.resourceElements[theResName] then
194194
newmodelsUtils.resourceElements[theResName] = {}

newmodels_red/scripts/optional/debug/c_debug.lua

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ local function getElementCustomModelString(element)
2727
local customModel = getElementCustomModel(element)
2828
if customModel and customModels[customModel] then
2929
local name, baseModel = customModels[customModel].name, customModels[customModel].baseModel
30-
return {("%d \"%s\" (%d)"):format(customModel, name, baseModel), 0xffffa263}
30+
return { ("%d \"%s\" (%d)"):format(customModel, name, baseModel), 0xffffa263 }
3131
else
32-
return {("%d"):format(getElementModel(element))}
32+
return { ("%d"):format(getElementModel(element)) }
3333
end
3434
end
3535

@@ -44,9 +44,9 @@ local function updateDebugViewInfo()
4444
end
4545
end
4646
if loadedModelsStr_ ~= "" then
47-
loadedModelsStr = "Loaded new models:\n" .. loadedModelsStr_
47+
loadedModelsStr = "Loaded new models (client-side):\n" .. loadedModelsStr_
4848
else
49-
loadedModelsStr = "No new models loaded."
49+
loadedModelsStr = "No new models loaded (client-side)"
5050
end
5151

5252
streamedElements = {}
@@ -65,8 +65,8 @@ local function updateDebugViewInfo()
6565
end
6666

6767
local function drawDebug()
68-
dxDrawText("Newmodels v6 Red", SW/2, 15, SW/2, 15, 0xffff7070, 1.5, "default-bold", "center", "center")
69-
dxDrawText(loadedModelsStr, SW/2, 32, SW/2, 32, 0xFFFFFFFF, 1, "default-bold", "center", "top")
68+
dxDrawText("Newmodels v6 Red", SW / 2, 15, SW / 2, 15, 0xffff7070, 1.5, "default-bold", "center", "center")
69+
dxDrawText(loadedModelsStr, SW / 2, 32, SW / 2, 32, 0xFFFFFFFF, 1, "default-bold", "center", "top")
7070

7171
for element, customModelStr in pairs(streamedElements) do
7272
local x, y, z = getElementPosition(element)
@@ -87,7 +87,9 @@ local function toggleDebugView(cmd)
8787
addEventHandler("onClientRender", root, drawDebug, false)
8888
addEventHandler("onClientElementDestroy", root, handleElementDestroyed)
8989
else
90-
if debugTimer and isTimer(debugTimer) then killTimer(debugTimer); debugTimer = nil end
90+
if debugTimer and isTimer(debugTimer) then
91+
killTimer(debugTimer); debugTimer = nil
92+
end
9193
streamedElements = {}
9294
removeEventHandler("onClientRender", root, drawDebug)
9395
removeEventHandler("onClientElementDestroy", root, handleElementDestroyed)
@@ -99,4 +101,4 @@ addCommandHandler("newmodelsdebug", toggleDebugView, false)
99101

100102
if (DEBUG_VIEW_ON_BY_DEFAULT) then
101103
executeCommandHandler("newmodelsdebug")
102-
end
104+
end

0 commit comments

Comments
 (0)