forked from overextended/ox_lib
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
Latest commit
191 lines (154 loc) · 4.82 KB
/
Copy pathinit.lua
File metadata and controls
191 lines (154 loc) · 4.82 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
-- ox_lib
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>
ifnot_VERSION:find('5.4') then
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 2)
end
localox_lib='ox_lib'
ifnotGetResourceState(ox_lib):find('start') then
error('^1ox_lib should be started before this resource^0', 2)
end
-----------------------------------------------------------------------------------------------
-- Module
-----------------------------------------------------------------------------------------------
localLoadResourceFile=LoadResourceFile
localservice=IsDuplicityVersion() and'server' or'client'
localfunctionloadModule(self, module)
localdir= ('imports/%s'):format(module)
localchunk=LoadResourceFile(ox_lib, ('%s/%s.lua'):format(dir, service))
localshared=LoadResourceFile(ox_lib, ('%s/shared.lua'):format(dir))
ifsharedthen
chunk= (chunkand ('%s\n%s'):format(shared, chunk)) orshared
end
ifchunkthen
localerr
chunk, err=load(chunk, ('@@ox_lib/%s/%s.lua'):format(module, service))
iferrthen
error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
else
localresult=chunk() orfunction() end
rawset(self, module, result)
returnself[module]
end
end
end
-----------------------------------------------------------------------------------------------
-- API
-----------------------------------------------------------------------------------------------
localfunctioncall(self, index, ...)
localmodule=rawget(self, index)
ifnotmodulethen
module=loadModule(self, index)
ifnotmodulethen
localfunctionmethod(...)
returnexports[ox_lib][index](nil, ...)
end
ifnot...then
rawset(self, index, method)
end
returnmethod
end
end
returnmodule
end
lib=setmetatable({
name=ox_lib,
service=service,
exports= {},
onCache=setmetatable({}, {
__call=function(self, key, cb)
self[key] =cb
end
})
}, {
__index=call,
__call=call,
})
localintervals= {}
--- Dream of a world where this PR gets accepted.
---@paramcallbackfunction
---@paraminterval?number
---@param ... any
functionSetInterval(callback, interval, ...)
interval=intervalor0
assert(type(interval) =='number', ('Interval must be a number. Received %s'):format(json.encode(interval)))
localcbType=type(callback)
ifcbType=='number' andintervals[callback] then
intervals[callback] =intervalor0
return
end
assert(cbType=='function', ('Callback must be a function. Received %s'):format(tostring(cbType)))
localid
localargs= {...}
Citizen.CreateThreadNow(function(ref)
id=ref
intervals[id] =intervalor0
repeat
interval=intervals[id]
Wait(interval)
callback(table.unpack(args))
untilinterval<0
intervals[id] =nil
end)
returnid
end
functionClearInterval(id)
assert(type(id) =='number', ('Interval id must be a number. Received %s'):format(json.encode(id)))
assert(intervals[id], ('No interval exists with id %s'):format(id))
intervals[id] =-1
end
-----------------------------------------------------------------------------------------------
-- Cache
-----------------------------------------------------------------------------------------------
localox=GetResourceState('ox_core') ~='missing' andsetmetatable({}, {
__index=function()
returntrue
end
}) or {
groups=GetResourceState('ox_groups') ~='missing',
}
localcache=setmetatable({}, {
__index=function(self, key)
returnrawset(self, key, exports[ox_lib]['cache'](nil, key) orfalse)[key]
end,
__call=function(self)
table.wipe(self)
ifservice=='client' then
self.playerId=PlayerId()
self.serverId=GetPlayerServerId(self.playerId)
end
self.resource=GetCurrentResourceName()
ifox.groupsthen
self.groups=setmetatable({}, {
__index=function(groups, index)
groups[index] =GlobalState['group:'..index]
returngroups[index]
end
})
end
end
})
Citizen.CreateThreadNow(function()
whiletruedo
cache()
Wait(60000)
end
end)
AddEventHandler('ox_lib:updateCache', function(data)
forkey, valueinpairs(data) do
iflib.onCache[key] then
lib.onCache[key](value)
end
cache[key] =value
end
end)
_ENV.cache=cache