This repository has been archived by the owner on Oct 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.lua
267 lines (216 loc) · 7.52 KB
/
init.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
if not _VERSION:find('5.4') then
error("^1 Vous devez activer Lua 5.4 dans la resources où vous utilisez l'import, (lua54 'yes') dans votre fxmanifest!^0", 2)
end
local s_ui <const> = 'sublime_nativeui'
if not GetResourceState(s_ui):find('start') then
error('^1sublime_nativeui doit être lancé avant cette ressource!^0', 2)
end
if IsDuplicityVersion() then
return warn("Isn't supported in server side!")
end
local export <const> = exports[s_ui]
-- [fr]: Pas besoin de réécrire la fonction require si un des ses objets existe | [en]: No need to rewrite the require function if one of its objects exists
if not supv and not lib and not sublime then
local loaded = {}
package = {
loaded = setmetatable({}, {
__index = loaded,
__newindex = function() end,
__metatable = false,
}),
path = './?.lua;'
}
local _require = require
---@param modname string
---@return unknown?
local function LoadModule(modname)
if type(modname) ~= 'string' then return end
local module = loaded[modname]
if not module then
if module == false then
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
end
if not modname:find('^@') then
local success, result = pcall(_require, modname)
if success then
loaded[modname] = result
return result
end
local modpath = modname:gsub('%.', '/')
for path in package.path:gmatch('[^;]+') do
local scriptPath = path:gsub('?', modpath):gsub('%.+%/+', '')
local resourceFile = LoadResourceFile(nativeui.env, scriptPath)
if resourceFile then
loaded[modname] = false
scriptPath = ('@@%s/%s'):format(nativeui.env, scriptPath)
local chunk, err = load(resourceFile, scriptPath)
if err or not chunk then
loaded[modname] = nil
return error(err or ("unable to load module '%s'"):format(modname), 3)
end
module = chunk(modname) or true
loaded[modname] = module
return module
end
end
else
local rss, dir = modname:gsub('%.', '/'):match('^(.-)/(.+)$')
if not rss or not dir then return error('Invalid path format: '..modname, 2) end
rss, dir = rss:gsub('^@', ''), dir..'.lua'
local chunk = LoadResourceFile(rss, dir)
if chunk then
local scriptPath = ('@@%s/%s'):format(rss, dir)
local func, err = load(chunk, scriptPath)
if err or not func then
return error(err or ("unable to load module '%s'"):format(modname), 2)
end
module = func(modname) or true
loaded[modname] = module
return module
end
end
return error(("module '%s' not found"):format(modname), 2)
end
return module
end
require = LoadModule
end
if not supv and not sublime then
local function void() end
_ENV.void = void
else
_ENV.void = void
end
local function call_module(self, index, ...)
local module = rawget(self, index)
-- print('module', module, index, ...)
if not module then
self[index] = void
if self.exportsMethod[index] then
local function method(...)
return export[index](nil, ...)
end
if not ... then
self[index] = method
end
return method
else
local path <const> = export:GetPathModule(index)
if not path then
error(('module %s not found'):format(index), 2)
return
end
module = require(path)
--[[@? no need that for now
if not ... then
module = require(path)
else
module = require(path)(...)
end
--]]
rawset(self, index, module)
end
end
return module
end
local nativeui = setmetatable({
menus = {},
exportsMethod = export:GetExportMethod(),
env = GetCurrentResourceName(),
cache = export:GetCache(),
await = Citizen.Await
}, {
__index = call_module,
__call = call_module,
})
AddEventHandler('sublime_nativeui:close:'..nativeui.env, function(id, resolve)
local menu <const> = nativeui.menus[id]
--print('close', id, menu.id)
if not menu then
warn(('Menu with id %s not found'):format(id))
resolve(false, ('Menu with id %s not found'):format(id))
return
end
resolve(menu:GoClose())
end)
AddEventHandler('sublime_nativeui:open:'..nativeui.env, function(id, resolve)
local menu <const> = nativeui.menus[id]
--print('open', id, menu.id)
if not menu then
warn(('Menu with id %s not found'):format(id))
resolve(false, ('Menu with id %s not found'):format(id))
return
end
resolve(menu:GoOpen())
end)
AddEventHandler('sublime_nativeui:cache:set', function(key, value)
nativeui.cache[key] = value
local menu = nativeui.menus[nativeui.CurrentOpen()]
if menu then
menu:SetSizeResponsive()
end
end)
_ENV.nativeui = nativeui
--[[
Old work
LocalPlayer.state:set('menuOpen', nil, false)
AddStateBagChangeHandler('menuOpen', nil, function(bagName, key, value, reserved, replicated)
if replicated then return end
if not nativeui.cache.playerid then
return warn('Player id not found in cache!')
end
local ply = GetPlayerFromStateBagName(bagName)
if ply ~= nativeui.cache.playerid then return end
if not value then
return
else
print('val: '..value)
--LocalPlayer.state:set('menuOpen', nil, false)
local menu = nativeui.menus[value]
print(menu.opened)
if menu and not menu.opened then
print('opeen')
menu:GoOpen()
elseif menu and menu.opened then
print('clooose')
menu:GoClose()
end
end
end)
---@param id string
---@return boolean, string?
local function GoClose(id)
local menu <const> = nativeui.menus[id]
if not menu then
warn(('Menu with id %s not found'):format(id))
return false, ('Menu with id %s not found'):format(id)
end
local closed <const>, reason <const> = menu:GoClose() -- 'export'
return closed, reason
end
---@param id string
---@return boolean, string?
local function GoOpen(id)
local menu <const> = nativeui.menus[id]
if not menu then
warn(('Menu with id %s not found'):format(id))
return false, ('Menu with id %s not found'):format(id)
end
local opened <const>, reason <const> = menu:GoOpen() -- 'export'
return opened, reason
end
---@param id string
---@return boolean, string?
local function Destroy(id)
local menu <const> = nativeui.menus[id]
if not menu then
warn(('Menu with id %s not found'):format(id))
return false, ('Menu with id %s not found'):format(id)
end
nativeui.menus[id] = nil
return true
end
exports('GoClose', GoClose)
exports('GoOpen', GoOpen)
exports('Destroy', Destroy)
]]