forked from Fernando-A-Rocha/mta-add-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.lua
335 lines (275 loc) · 9.12 KB
/
server.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
--[[
Author: Fernando
server.lua
Test Implementation #1
What it does:
Adds a new skin & vehicle by reading the myMods array,
with mod files stored in this resource by calling addExternalMod_CustomFilenames
See documentation for the newmodels test commands
Commands:
/removemod
/testhandling
/testvehicles
]]
local myMods = {
-- this is completely a personal choice, you can have your own way of loading mods
-- unique ID, type, base model ID or Name, Mod Name, dff path, txd path, col path
-- https://wiki.multitheftauto.com/wiki/Vehicle_IDs
{-1, "ped", 1, "American Biker", "mymodels/biker.dff", "mymodels/biker.txd", nil},
{-2, "vehicle", "Rancher", "Samoa", "mymodels/samoa.dff", "mymodels/samoa.txd", nil},
}
addEventHandler( "onResourceStart", resourceRoot,
function (startedResource)
if not startUpChecks() then return end
for k,mod in pairs(myMods) do
local uid = mod[1]
local et = mod[2]
local baseid = mod[3]
local name = mod[4]
local dff = mod[5]
local txd = mod[6]
local col = mod[7]
if type(baseid) == "string" then
baseid = getVehicleModelFromName(baseid)
end
if not baseid then
outputDebugString("Failed to get vehicle model from name: "..tostring(mod[2]), 0, 255,55,55)
else
local worked, reason = exports.newmodels:addExternalMod_CustomFilenames(
et, uid, baseid, name,
dff, txd, col
)
if not worked then
outputDebugString(reason or "Unknown error", 0, 255,110,61)
end
end
end
checkPossibleExistingElements()
end)
--[[
This function makes sure the mods you define in myMods exist & are in the meta.xml (automatically adds/removes in the file)
]]
function startUpChecks()
print("STARTUP", "Starting scan") -- You can comment this out to not have startup spam
local metaXML = xmlLoadFile("meta.xml")
if not metaXML then
print("Failed to open meta.xml")
return false
end
local addedAny = false
local nodes = xmlNodeGetChildren(metaXML)
-- Check if any of the listed mod files no longer exist in the meta.xml
for i, node in pairs(nodes) do
if xmlNodeGetName(node) == "file" then
local src = xmlNodeGetAttribute(node, "src")
if not fileExists(src) then
xmlDestroyNode(node)
print("AUTO-META","Destroyed XML node coz file not found:", src)
end
end
end
nodes = xmlNodeGetChildren(metaXML)
local existing_filenames = {}
for k,mod in pairs(myMods) do
local uid = mod[1]
local et = mod[2]
local baseid = mod[3]
local name = mod[4]
local dff = mod[5]
local txd = mod[6]
local col = mod[7]
local filenames = {dff,txd}
if col then
table.insert(filenames, col)
end
for _, filename in pairs(filenames) do
if not fileExists(filename) then
local found = nil
for i, node in pairs(nodes) do
if node and xmlNodeGetName(node) == "file" then
local src = xmlNodeGetAttribute(node, "src")
if string.lower(src) == string.lower(filename) then
found = node
break
end
end
end
if found then
xmlDestroyNode(found)
print("AUTO-META","Mod #"..k.." "..name, "Destroyed XML node coz file not found:", filename)
end
print("AUTO-META", "Mod #"..k.." "..name, "CANCELLING | File not found:", filename)
return false
else
local found = nil
for i, node in pairs(nodes) do
if node and xmlNodeGetName(node) == "file" then
local src = xmlNodeGetAttribute(node, "src")
if string.lower(src) == string.lower(filename) then
found = node
break
end
end
end
if not found then
local node = xmlCreateChild(metaXML, "file")
xmlNodeSetAttribute(node, "src", filename)
print("AUTO-META","Mod #"..k.." "..name, "Saved in XML:", filename)
addedAny = true
end
existing_filenames[filename] = true
end
end
end
nodes = xmlNodeGetChildren(metaXML)
-- Purge any files listed in meta.xml that are no longer used in mods
for i, node in pairs(nodes) do
if node and xmlNodeGetName(node) == "file" then
local src = xmlNodeGetAttribute(node, "src")
if not existing_filenames[src] then
xmlDestroyNode(node)
print("AUTO-META","Destroyed XML node coz file no longer used:", src)
end
end
end
xmlSaveFile(metaXML)
xmlUnloadFile(metaXML)
if addedAny then
print("AUTO-META", "Added at least 1 mod file to meta.xml, restarting...")
restartResource(getThisResource())
return false
end
print("STARTUP", "All checks passed") -- You can comment this out to not have startup spam
return true
end
--[[
Checks for existing elements (peds, vehicles) spawned in the server with these mods' unique IDs
]]
local fixDelay = 15000
function checkPossibleExistingElements()
local check = {}
for k,mod in pairs(myMods) do
local uid = mod[1]
local et = mod[2]
local baseid = mod[3]
local name = mod[4]
local dff = mod[5]
local txd = mod[6]
local col = mod[7]
if not check[et] then check[et] = {} end
check[et][uid] = true
end
-- hack
check["player"] = check["ped"]
local fix = {}
local count = 0
for type,ids in pairs(check) do
local dataName = exports.newmodels:getDataNameFromType(type)
for i,e in pairs(getElementsByType(type)) do
local uid = tonumber(getElementData(e, dataName))
if uid and check[type][uid] then
check[type][uid] = nil
removeElementData(e, dataName)
fix[e] = {dataName, uid}
count = count + 1
end
end
end
if count > 0 then
-- Reset after a certain delay (all players need to free the old bugged elements to receive the refresh)
print("Fixing "..count.." existing leftover modded elements in "..(fixDelay/1000).." seconds")
setTimer(function()
for element,v in pairs(fix) do
-- iprint("FIXED", element, v[1], v[2])
setElementData(element, v[1], v[2])
end
end, fixDelay, 1)
end
end
--[[
Some testing commands below
]]
function removeModCmd(thePlayer, cmd, id)
if not tonumber(id) then
return outputChatBox("SYNTAX: /"..cmd.." [ID from myMods]", thePlayer,255,194,14)
end
id = tonumber(id)
for k,v in pairs(myMods) do
if v[1] == id then
local worked, reason = exports.newmodels:removeExternalMod(id)
if not worked then
outputDebugString(reason, 0,255, 110, 61)
end
return
end
end
outputChatBox("Mod ID "..id.." not found in myMods because it wasn't added.", thePlayer,255,0,0)
end
addCommandHandler("removemod", removeModCmd, false, false)
function testHandling(thePlayer, cmd)
local theVehicle = getPedOccupiedVehicle(thePlayer)
if not theVehicle then
return outputChatBox("Get inside a vehicle", thePlayer,255,200,0)
end
local properties = {
-- name => min,max that I want
["engineAcceleration"] = {10, 100},
["maxVelocity"] = {50, 500},
["brakeDeceleration"] = {1, 100},
}
local randomProperty
local randomVal
math.randomseed(getRealTime().timestamp)
while not randomVal do
for k,v in pairs(properties) do
if math.random(1,2) == 1 then
randomProperty = k
randomVal = math.random(v[1], v[2])
break
end
end
end
if setVehicleHandling(theVehicle, randomProperty, randomVal) then
outputChatBox("Set "..randomProperty.." to "..randomVal, thePlayer, 0,255,100)
else
outputChatBox("Failed to set "..randomProperty.." to "..randomVal, thePlayer, 255,0,0)
end
end
addCommandHandler("testhandling", testHandling, false, false)
--[[
Fetches all mods using exported func getModList
Spawns all added vehicles at the airport
]]
local spawned_vehs = {}
local ix,iy,iz = 2045.732421875, -2493.884765625, 13.546875 -- initial position for vehicle spawn (airport)
local rx,ry,rz = 0,0,90
function testVehiclesCmd(thePlayer, cmd)
for k,veh in pairs(spawned_vehs) do
if isElement(veh) then destroyElement(veh) end
end
spawned_vehs = {}
local x,y,z = ix,iy,iz
if getPedOccupiedVehicle(thePlayer) then
removePedFromVehicle(thePlayer)
end
setElementPosition(thePlayer, x+6,y,z)
setElementDimension(thePlayer, 0)
setElementInterior(thePlayer, 0)
local elementType2 = "vehicle"
local data_name = exports.newmodels:getDataNameFromType(elementType2)
local modList = exports.newmodels:getModList()
for elementType, mods in pairs(modList) do
if elementType == elementType2 then
for k,mod in pairs(mods) do
local veh = createVehicle(400, x,y,z,rx,ry,rz)
if veh then
setElementData(veh, data_name, mod.id)
table.insert(spawned_vehs, veh)
x = x-12
end
end
end
end
outputChatBox("Created "..#spawned_vehs.." new vehicles.", thePlayer, 0,255,0)
end
addCommandHandler("testvehicles", testVehiclesCmd, false, false)