FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ox_lib/init.lua at master · Red40-Development/ox_lib · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Red40-Development
/
ox_lib
Public
forked from
overextended/ox_lib
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ox_lib
/
init.lua
Copy path
More file actions
More file actions
Latest commit
History
History
History
299 lines (229 loc) · 8.16 KB
Breadcrumbs
ox_lib
/
init.lua
Copy path
File metadata and controls
299 lines (229 loc) · 8.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
---
@meta
--[[
https://github.com/overextended/ox_lib
This file is licensed under LGPL-3.0 or higher <https://www.gnu.org/licenses/lgpl-3.0.en.html>
Copyright © 2025 Linden <https://github.com/thelindat>
]]
if
not
_VERSION
:
find
(
'
5.4
'
)
then
error
(
'
Lua 5.4 must be enabled in the resource manifest!
'
,
2
)
end
local
resourceName
=
GetCurrentResourceName
()
local
ox_lib
=
'
ox_lib
'
--
Some people have decided to load this file as part of ox_lib's fxmanifest?
if
resourceName
==
ox_lib
then
return
end
if
lib
and
lib
.
name
==
ox_lib
then
error
((
"
Cannot load ox_lib more than once.
\n\t
Remove any duplicate entries from '@%s/fxmanifest.lua'
"
):
format
(
resourceName
))
end
local
export
=
exports
[
ox_lib
]
if
GetResourceState
(
ox_lib
)
~=
'
started
'
then
error
(
'
^1ox_lib must be started before this resource.^0
'
,
0
)
end
local
status
=
export
.
hasLoaded
()
if
status
~=
true
then
error
(
status
,
2
)
end
--
Ignore invalid types during msgpack.pack (e.g. userdata)
msgpack
.
setoption
(
'
ignore_invalid
'
,
true
)
----
-------------------------------------------------------------------------------------------
--
Module
----
-------------------------------------------------------------------------------------------
local
LoadResourceFile
=
LoadResourceFile
local
context
=
IsDuplicityVersion
()
and
'
server
'
or
'
client
'
function
noop
()
end
local
function
loadModule
(
self
,
module
)
local
dir
=
(
'
imports/%s
'
):
format
(
module
)
local
chunk
=
LoadResourceFile
(
ox_lib
, (
'
%s/%s.lua
'
):
format
(
dir
,
context
))
local
shared
=
LoadResourceFile
(
ox_lib
, (
'
%s/shared.lua
'
):
format
(
dir
))
if
shared
then
chunk
=
(
chunk
and
(
'
%s
\n
%s
'
):
format
(
shared
,
chunk
))
or
shared
end
if
chunk
then
local
fn
,
err
=
load
(
chunk
, (
'
@@ox_lib/imports/%s/%s.lua
'
):
format
(
module
,
context
))
if
not
fn
or
err
then
if
shared
then
lib
.
print
.
warn
((
"
An error occurred when importing '@ox_lib/imports/%s'.
\n
This is likely caused by improperly updating ox_lib.
\n
%s'
"
)
:
format
(
module
,
err
))
fn
,
err
=
load
(
shared
, (
'
@@ox_lib/imports/%s/shared.lua
'
):
format
(
module
))
end
if
not
fn
or
err
then
return
error
((
'
\n
^1Error importing module (%s): %s^0
'
):
format
(
dir
,
err
),
3
)
end
end
local
result
=
fn
()
self
[
module
]
=
result
or
noop
return
self
[
module
]
end
end
----
-------------------------------------------------------------------------------------------
--
API
----
-------------------------------------------------------------------------------------------
local
function
call
(
self
,
index
, ...)
local
module
=
rawget
(
self
,
index
)
if
not
module
then
self
[
index
]
=
noop
module
=
loadModule
(
self
,
index
)
if
not
module
then
local
function
method
(...)
return
export
[
index
](
nil
,
...
)
end
if
not
...
then
self
[
index
]
=
method
end
return
method
end
end
return
module
end
local
lib
=
setmetatable
({
name
=
ox_lib
,
context
=
context
,
}, {
__index
=
call
,
__call
=
call
,
})
local
intervals
=
{}
---
Dream of a world where this PR gets accepted.
---
@param
callback
function
|
number
---
@param
interval
?
number
---
@param
...
any
function
SetInterval
(
callback
,
interval
, ...)
interval
=
interval
or
0
if
type
(
interval
)
~=
'
number
'
then
return
error
((
'
Interval must be a number. Received %s
'
):
format
(
json
.
encode
(
interval
--[[
@as unknown
]]
)))
end
local
cbType
=
type
(
callback
)
if
cbType
==
'
number
'
and
intervals
[
callback
]
then
intervals
[
callback
]
=
interval
or
0
return
end
if
cbType
~=
'
function
'
then
return
error
((
'
Callback must be a function. Received %s
'
):
format
(
cbType
))
end
local
args
,
id
=
{
...
}
Citizen
.
CreateThreadNow
(
function
(
ref
)
id
=
ref
intervals
[
id
]
=
interval
or
0
repeat
interval
=
intervals
[
id
]
Wait
(
interval
)
if
interval
<
0
then
break
end
callback
(
table.unpack
(
args
))
until
false
intervals
[
id
]
=
nil
end
)
return
id
end
---
@param
id
number
function
ClearInterval
(
id
)
if
type
(
id
)
~=
'
number
'
then
return
error
((
'
Interval id must be a number. Received %s
'
):
format
(
json
.
encode
(
id
--[[
@as unknown
]]
)))
end
if
not
intervals
[
id
]
then
return
error
((
'
No interval exists with id %s
'
):
format
(
id
))
end
intervals
[
id
]
=
-
1
end
--[[
lua language server doesn't support generics when using @overload
see https://github.com/LuaLS/lua-language-server/issues/723
this function stub allows the following to work
local key = cache('key', function() return 'abc' end) -- fff: 'abc'
local game = cache.game -- game: string
]]
---
@generic
T
---
@param
key
string
---
@param
func
fun
(
...
:
any
):
T
---
@param
timeout
?
number
---
@return
T
---
Caches the result of a function, optionally clearing it after timeout ms.
function
cache
(
key
,
func
,
timeout
)
end
local
cacheEvents
=
{}
local
cache
=
setmetatable
({
game
=
GetGameName
(),
resource
=
resourceName
}, {
__index
=
function
(
self
,
key
)
cacheEvents
[
key
]
=
{}
AddEventHandler
((
'
ox_lib:cache:%s
'
):
format
(
key
),
function
(
value
)
local
oldValue
=
self
[
key
]
local
events
=
cacheEvents
[
key
]
for
i
=
1
,
#
events
do
Citizen
.
CreateThreadNow
(
function
()
events
[
i
](
value
,
oldValue
)
end
)
end
self
[
key
]
=
value
end
)
return
rawset
(
self
,
key
,
export
.
cache
(
nil
,
key
)
or
false
)[
key
]
end
,
__call
=
function
(
self
,
key
,
func
,
timeout
)
local
value
=
rawget
(
self
,
key
)
if
value
==
nil
then
value
=
func
()
rawset
(
self
,
key
,
value
)
if
timeout
then
SetTimeout
(
timeout
,
function
()
self
[
key
]
=
nil
end
)
end
end
return
value
end
,
})
function
lib
.
onCache
(
key
,
cb
)
if
not
cacheEvents
[
key
]
then
getmetatable
(
cache
).
__index
(
cache
,
key
)
end
table.insert
(
cacheEvents
[
key
],
cb
)
end
_ENV
.
lib
=
lib
_ENV
.
cache
=
cache
_ENV
.
require
=
lib
.
require
local
notifyEvent
=
(
'
__ox_notify_%s
'
):
format
(
cache
.
resource
)
if
context
==
'
client
'
then
RegisterNetEvent
(
notifyEvent
,
function
(
data
)
if
locale
then
if
data
.
title
then
data
.
title
=
locale
(
data
.
title
)
or
data
.
title
end
if
data
.
description
then
data
.
description
=
locale
(
data
.
description
)
or
data
.
description
end
end
return
export
:
notify
(
data
)
end
)
cache
.
playerId
=
PlayerId
()
cache
.
serverId
=
GetPlayerServerId
(
cache
.
playerId
)
else
---
`server`\
---
Trigger a notification on the target playerId from the server.\
---
If locales are loaded, the title and description will be formatted automatically.\
---
Note: No support for locale placeholders when using this function.
---
@param
playerId
number
---
@param
data
NotifyProps
---
@deprecated
---
@diagnostic
disable-next-line
:
duplicate-set-field
function
lib
.
notify
(
playerId
,
data
)
TriggerClientEvent
(
notifyEvent
,
playerId
,
data
)
end
local
poolNatives
=
{
CPed
=
GetAllPeds
,
CObject
=
GetAllObjects
,
CVehicle
=
GetAllVehicles
,
}
---
@param
poolName
'
CPed
'
|
'
CObject
'
|
'
CVehicle
'
---
@return
number[]
---
Server-side parity for the `GetGamePool` client native.
function
GetGamePool
(
poolName
)
local
fn
=
poolNatives
[
poolName
]
return
fn
and
fn
()
--[[
@as number[]
]]
end
---
@return
number[]
---
Server-side parity for the `GetPlayers` client native.
function
GetActivePlayers
()
local
playerNum
=
GetNumPlayerIndices
()
local
players
=
table.create
(
playerNum
,
0
)
for
i
=
1
,
playerNum
do
players
[
i
]
=
tonumber
(
GetPlayerFromIndex
(
i
-
1
))
end
return
players
end
end
for
i
=
1
,
GetNumResourceMetadata
(
cache
.
resource
,
'
ox_lib
'
)
do
local
name
=
GetResourceMetadata
(
cache
.
resource
,
'
ox_lib
'
,
i
-
1
)
if
not
rawget
(
lib
,
name
)
then
local
module
=
loadModule
(
lib
,
name
)
if
type
(
module
)
==
'
function
'
then
pcall
(
module
)
end
end
end
Back
|
FazBrowse Home
|
New Git URL