-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport.lua
More file actions
217 lines (186 loc) · 6.21 KB
/
import.lua
File metadata and controls
217 lines (186 loc) · 6.21 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
local function canProcceed()
local resourceName <const> = GetCurrentResourceName() ~= "vorp_lib"
local isLibStarted <const> = GetResourceState("vorp_lib") == 'started'
local noLib <const> = resourceName and isLibStarted
if not noLib then return false, 'vorp_lib must must be ensured before this resource' end
return true
end
local result <const>, message <const> = canProcceed()
if not result then
return error(("^1[ERROR] ^3%s^0"):format(message))
end
local side <const> = IsDuplicityVersion() and 'server' or 'client'
local loadedModules <const> = {}
local importModules <const> = {}
importModules.__index = importModules
importModules.__call = function()
return "importModules"
end
local content <const> = {
-- data files for the path
client_data = {
gameEvents = true,
},
-- model names
shared_data = {
animals = true,
peds = true,
vehicles = true,
},
-- shared files for the path
shared = {
class = true,
functions = true,
notify = true,
},
}
function importModules:GetPath(file)
local resource, path
if file:sub(1, 1) == "@" then
-- resource contains @
resource = file:match("@(.-)/")
path = file:match("@.-/(.+)")
elseif file:sub(1, 1) == "/" or file:sub(1, 1) == "." then
-- own resource contains / or .
resource = GetCurrentResourceName()
path = file
else
-- lib contains no symbols
resource = "vorp_lib"
-- is shared file
if content.shared[file] then
path = ("shared/%s"):format(file)
elseif content.client_data[file] then
path = ("client/data/%s"):format(file)
elseif content.shared_data[file] then
path = ("shared/data/%s"):format(file)
else
path = ("%s/modules/%s"):format(side, file)
end
end
return resource, path .. ".lua"
end
function importModules:Normalize(value)
if type(value) ~= "table" then
value = { value }
end
return value
end
function importModules:LoadModule(resource, path)
loadedModules[resource] = loadedModules[resource] or {}
local cached <const> = loadedModules[resource][path]
if cached ~= nil then
return cached
end
local raw <const> = LoadResourceFile(resource, path)
assert(raw, ("Failed to load file: %s/%s does not exist or the path is wrong"):format(resource, path))
local env <const> = setmetatable({}, { __index = _ENV })
local chunk, err = load(raw, ("@%s/%s"):format(resource, path), 't', env)
assert(chunk, err)
local ok <const>, ret <const> = pcall(chunk)
assert(ok, ret)
local mod <const> = ret ~= nil and ret or env
loadedModules[resource][path] = mod
return mod
end
function importModules:GetModules(value)
local results <const> = {}
local data <const> = self:Normalize(value)
for _, file in ipairs(data) do
local resource <const>, path <const> = self:GetPath(file)
local mod <const> = self:LoadModule(resource, path)
if type(mod) == "table" then
for k, v in pairs(mod) do
results[k] = v
end
else
local name <const> = path:match("([^/%.]+)%.lua$") or path
results[name] = mod
end
end
return results
end
function importModules:New(moduels)
local import <const> = setmetatable({}, self)
return import:GetModules(moduels)
end
--- [documentation](https://docs.vorp-core.com/api-reference/lib) **learn how to use it**
---@param modules table| string Import a module or multiple modules from the library or from any resource
function Import(modules)
return importModules:New(modules)
end
_ENV.Import = Import
if side == "client" then
---@class CACHE
---@field Ped integer player ped id
---@field Player integer player id
---@field ServerID integer player server id
---@field Mount integer current mounted ped id
---@field Vehicle integer current in vehicle ped id
---@field Weapon integer current held weapon ped id
CACHE = {}
CreateThread(function()
CACHE.Ped = 0
CACHE.Player = PlayerId()
CACHE.ServerID = GetPlayerServerId(CACHE.Player)
CACHE.Mount = 0
CACHE.Vehicle = 0
CACHE.Weapon = 0
CACHE.IsDead = false
-- adjustable vars
CACHE.SkipMount = false
CACHE.SkipVehicle = false
CACHE.SkipWeapon = false
CACHE.SkipIsDead = false
CACHE.Wait = 500
while true do
Wait(CACHE.Wait)
local ped <const> = PlayerPedId()
if CACHE.Ped ~= ped then
CACHE.Ped = ped
end
if not CACHE.SkipMount then
local mount <const> = GetMount(CACHE.Ped)
if mount ~= 0 then
if CACHE.Mount ~= mount then
CACHE.Mount = mount
end
else
if CACHE.Mount ~= 0 then
CACHE.Mount = 0
end
end
end
if not CACHE.SkipVehicle then
local vehicle <const> = GetVehiclePedIsIn(CACHE.Ped, false)
if vehicle ~= 0 then
if CACHE.Vehicle ~= vehicle then
CACHE.Vehicle = vehicle
end
else
if CACHE.Vehicle ~= 0 then
CACHE.Vehicle = 0
end
end
end
if not CACHE.SkipWeapon then
local weapon <const> = GetPedCurrentHeldWeapon(CACHE.Ped)
if weapon ~= 0 then
if CACHE.Weapon ~= weapon then
CACHE.Weapon = weapon
end
else
if CACHE.Weapon ~= 0 then
CACHE.Weapon = 0
end
end
end
if not CACHE.SkipIsDead then
local isDead <const> = IsPlayerDead(CACHE.Player)
if isDead ~= CACHE.IsDead then
CACHE.IsDead = isDead
end
end
end
end)
end