Repository navigation
Expand file tree
/
Copy pathapi.lua
More file actions
204 lines (163 loc) · 5 KB
/
Copy pathapi.lua
File metadata and controls
204 lines (163 loc) · 5 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
local existing = rawget(_ENV, 'mnr')
if existing and existing.name == 'mnr_api' then
error(('mnr_api already loaded in %s - remove duplicate "@mnr_api/api.lua" from fxmanifest.lua'):format(GetCurrentResourceName()))
end
local buildResolver = LoadResourceFile('mnr_api', 'api/loader.lua')
buildResolver = assert(load(buildResolver, '@@mnr_api/api/loader.lua', 't'))()
local pending = {}
local pendingCount = 0
local ready = false
local subscribed = {}
local TIMEOUT = GetConvarInt('mnr_api:api_timeout', 30000)
local scope = IsDuplicityVersion() and 'server' or 'client'
msgpack.setoption('ignore_invalid', true)
local resolveKey, registry, loading = buildResolver({
resource = 'mnr_api',
scope = scope,
readFile = function(path)
return LoadResourceFile('mnr_api', path)
end,
})
local function abortPending()
local snapshot = pending
local count = pendingCount
pending = {}
pendingCount = 0
for i = 1, count do
coroutine.resume(snapshot[i], nil, 'Aborted: mnr_api did not start in time')
end
end
local function preloadAPI()
local resource = GetCurrentResourceName()
local keys = {}
local count = GetNumResourceMetadata(resource, 'mnr_preload')
for i = 0, count - 1 do
local key = GetResourceMetadata(resource, 'mnr_preload', i)
if key and key ~= '' then
keys[#keys + 1] = key
end
end
local extraCount = GetNumResourceMetadata(resource, 'mnr_preload_extra')
for i = 0, extraCount - 1 do
local extra = GetResourceMetadata(resource, 'mnr_preload_extra', i)
if extra then
local decoded = json.decode(extra)
if type(decoded) == 'table' then
for _, key in ipairs(decoded) do
if type(key) == 'string' and key ~= '' then
keys[#keys + 1] = key
end
end
end
end
end
for i = 1, #keys do
resolveKey(keys[i])
end
end
local function initAPI()
ready = true
local snapshot = pending
local count = pendingCount
pending = {}
pendingCount = 0
for i = 1, count do
coroutine.resume(snapshot[i])
end
end
local function waitReady(key)
if ready then return end
local co = coroutine.running()
if not co then
error(('mnr.%s accessed outside a coroutine before mnr_api was ready'):format(key), 2)
end
pendingCount = pendingCount + 1
pending[pendingCount] = co
local _, err = coroutine.yield()
if err then error(err, 2) end
end
---@type MnrAPI
local mnr = setmetatable({}, {
__index = function(_, key)
if key == 'name' then
return 'mnr_api'
end
if key == 'scope' then
return scope
end
local cached = registry[key]
if cached and cached ~= loading then
return cached
end
if ready then
return resolveKey(key)
end
local stub = setmetatable({}, {
__index = function(_, field)
waitReady(key)
local api = resolveKey(key)
rawset(mnr, key, api)
return api[field]
end,
__call = function(_, ...)
waitReady(key)
local api = resolveKey(key)
rawset(mnr, key, api)
return api(...)
end,
__metatable = false,
})
rawset(mnr, key, stub)
return stub
end,
__newindex = function(_, key, value)
registry[key] = value
end,
__metatable = false,
})
local mnrEnv = setmetatable({ resource = GetCurrentResourceName() }, {
__index = function(self, field)
local ok, value = pcall(function()
return exports.mnr_api:getEnv(field)
end)
if not ok then
error(('mnrEnv.%s is not available'):format(field), 2)
end
if not subscribed[field] then
subscribed[field] = true
AddEventHandler(('mnr_api:update:%s'):format(field), function(newValue)
rawset(self, field, newValue)
end)
end
rawset(self, field, value)
return value
end,
__metatable = false,
})
local startEvent = IsDuplicityVersion() and 'onResourceStart' or 'onClientResourceStart'
local timer
local handler
handler = AddEventHandler(startEvent, function(name)
if name ~= 'mnr_api' then return end
RemoveEventHandler(handler)
if timer then
ClearTimeout(timer)
end
initAPI()
preloadAPI()
end)
CreateThread(function()
if GetResourceState('mnr_api') == 'started' then
RemoveEventHandler(handler)
initAPI()
preloadAPI()
return
end
timer = SetTimeout(TIMEOUT, function()
RemoveEventHandler(handler)
abortPending()
error(('Timeout: mnr_api did not start within %dms - stopping %s'):format(TIMEOUT, GetCurrentResourceName()), 0)
end)
end)
rawset(_ENV, 'mnr', mnr)
rawset(_ENV, 'mnrEnv', mnrEnv)