Repository navigation
Expand file tree
/
Copy pathserver.lua
More file actions
274 lines (228 loc) · 8.75 KB
/
Copy pathserver.lua
File metadata and controls
274 lines (228 loc) · 8.75 KB
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
-- ---------------------------------------------------------------------------
-- Content Creator Morphs - Server
-- ---------------------------------------------------------------------------
-- Server side authorisation, model streaming and application of the morphs.
-- Any player who wants a morph must hold the matching ace permission, which
-- is checked server side. The command is guarded by ace permissions so that
-- unpermitted players cannot even trigger the command.
-- ---------------------------------------------------------------------------
local function GetMorphByName(name)
if not name then
return nil
end
local lower = string.lower(name)
-- Allow picking by numeric index (as shown by the chat menu).
local numericIndex = tonumber(lower)
if numericIndex then
local byIndex = Config.Morphs[numericIndex]
if byIndex then
return byIndex
end
end
for _, morph in ipairs(Config.Morphs) do
local morphName = string.lower(morph.name)
local morphModel = string.lower(morph.model)
if morphName == lower or morphModel == lower then
return morph
end
-- Allow fuzzy matching: the morph matches if the query is a
-- substring of the morph name or model, so "cc1" matches
-- "Content Creator 1". This makes the command forgiving.
if morphName:find(lower, 1, true) or morphModel:find(lower, 1, true) then
return morph
end
end
return nil
end
-- Find a morph by its stable persistence key (see config.lua).
local function GetMorphByKey(key)
for _, morph in ipairs(Config.Morphs) do
if morph.key == key then
return morph
end
end
return nil
end
-- Return a stable identifier for a player that survives reconnects.
local function GetPlayerIdentifier(source)
for _, id in ipairs(GetPlayerIdentifiers(source)) do
if string.match(id, '^license:') or string.match(id, '^steam:') or string.match(id, '^discord:') then
return id
end
end
return 'player:' .. tostring(source)
end
local function CanUseMorph(source, morph)
return IsPlayerAceAllowed(source, morph.ace)
end
local function ApplyMorph(source, morph)
local modelHash = GetHashKey(morph.model)
if not IsModelInCdimage(modelHash) then
TriggerClientEvent('gemera:ccmorph:notify', source, 'error',
('The model "%s" is not available on this server.'):format(morph.model))
return
end
-- Stream the model onto the running FXServer instance.
RequestModel(modelHash)
local timeLeft = 1000
while not HasModelLoaded(modelHash) and timeLeft > 0 do
Wait(10)
timeLeft = timeLeft - 10
end
if not HasModelLoaded(modelHash) then
TriggerClientEvent('gemera:ccmorph:notify', source, 'error',
('Failed to load the model "%s".'):format(morph.model))
return
end
SetPlayerModel(source, modelHash)
SetModelAsNoLongerNeeded(modelHash)
-- Persist the applied morph so it survives reconnects / restarts.
if Config.Persistence and Config.Persistence.Enabled then
Persistence.SetMorph(GetPlayerIdentifier(source), morph.key)
end
TriggerClientEvent('gemera:ccmorph:notify', source, 'success',
('Morph applied: %s'):format(morph.name))
end
local function ResetMorph(source)
local modelHash = GetHashKey('mp_m_freemode_01')
if IsModelInCdimage(modelHash) then
RequestModel(modelHash)
local timeLeft = 1000
while not HasModelLoaded(modelHash) and timeLeft > 0 do
Wait(10)
timeLeft = timeLeft - 10
end
if HasModelLoaded(modelHash) then
SetPlayerModel(source, modelHash)
SetModelAsNoLongerNeeded(modelHash)
-- Clear the persisted morph for this player.
if Config.Persistence and Config.Persistence.Enabled then
Persistence.SetMorph(GetPlayerIdentifier(source), nil)
end
TriggerClientEvent('gemera:ccmorph:notify', source, 'success',
'Morph reset to default.')
end
end
end
-- ---------------------------------------------------------------------------
-- Commands
-- ---------------------------------------------------------------------------
RegisterCommand(Config.Command, function(source, args, rawCommand)
if source == 0 then
-- Console cannot morph itself, but print the help.
print('[gemera-ccmorph] Console cannot apply morphs. Usage: /' .. Config.Command .. ' <morph name>')
return
end
local arg = args[1]
if not arg then
if Config.EnableMenu then
-- Open the chat menu on the client for this player.
TriggerClientEvent('gemera:ccmorph:openmenu', source)
else
TriggerClientEvent('gemera:ccmorph:notify', source, 'error',
('Usage: /%s <morph name>'):format(Config.Command))
end
return
end
if string.lower(arg) == 'reset' then
ResetMorph(source)
return
end
local morph = GetMorphByName(arg)
if not morph then
TriggerClientEvent('gemera:ccmorph:notify', source, 'error',
('No morph matching "%s" was found. Use /%s with no arguments to see the list.'):format(arg, Config.Command))
return
end
if not CanUseMorph(source, morph) then
TriggerClientEvent('gemera:ccmorph:notify', source, 'error',
'You do not have permission to use this morph.')
return
end
ApplyMorph(source, morph)
end, false)
-- ---------------------------------------------------------------------------
-- Chat suggestions (only where the framework provides /suggest support).
-- ---------------------------------------------------------------------------
RegisterCommand('ccmorphs', function(source)
if source == 0 then
return
end
local lines = { 'Available content creator morphs:' }
for _, morph in ipairs(Config.Morphs) do
local can = IsPlayerAceAllowed(source, morph.ace) and 'AVAILABLE' or 'LOCKED'
lines[#lines + 1] = (' - %s (%s) [%s]'):format(morph.name, morph.model, can)
end
for _, line in ipairs(lines) do
TriggerClientEvent('gemera:ccmorph:print', source, line)
end
end, false)
-- ---------------------------------------------------------------------------
-- Persistence lifecycle
-- ---------------------------------------------------------------------------
-- Load any previously saved morphs when the resource starts (covers restarts).
CreateThread(function()
if Config.Persistence and Config.Persistence.Enabled then
Persistence.Load()
print(('[gemera-ccmorph] Loaded persisted morphs from %s'):format(Persistence.GetPath()))
end
end)
-- Save this player's morph before they leave, so it survives a reconnect.
AddEventHandler('playerDropped', function(reason)
local source = source
if not Config.Persistence or not Config.Persistence.Enabled then
return
end
local morphKey = Persistence.GetMorph(GetPlayerIdentifier(source))
if morphKey then
Persistence.SetMorph(GetPlayerIdentifier(source), morphKey)
end
end)
-- Flush everything to disk when the resource stops (covers clean restarts).
AddEventHandler('onResourceStop', function(resource)
if resource ~= GetCurrentResourceName() then
return
end
if Config.Persistence and Config.Persistence.Enabled then
Persistence.Flush()
end
end)
-- Restore a player's saved morph shortly after they join.
local function RestoreSavedMorph(source)
if not Config.Persistence or not Config.Persistence.Enabled or not Config.Persistence.RestoreOnJoin then
return
end
-- Wait for the player to finish loading in before touching their ped.
local tries = 0
while not GetPlayerPed(source) or not DoesEntityExist(GetPlayerPed(source)) do
Wait(100)
tries = tries + 1
if tries > 100 then
return
end
end
-- A little extra delay so the game is stable after login.
Wait(3000)
local morphKey = Persistence.GetMorph(GetPlayerIdentifier(source))
if not morphKey then
return
end
local morph = GetMorphByKey(morphKey)
if not morph then
-- The morph no longer exists in config; drop the stale entry.
Persistence.SetMorph(GetPlayerIdentifier(source), nil)
return
end
-- Security: never restore a morph the player is no longer allowed to use.
if not CanUseMorph(source, morph) then
Persistence.SetMorph(GetPlayerIdentifier(source), nil)
return
end
ApplyMorph(source, morph)
end
AddEventHandler('playerJoining', function()
local source = source
CreateThread(function()
RestoreSavedMorph(source)
end)
end)