-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.lua
More file actions
137 lines (111 loc) · 3.48 KB
/
core.lua
File metadata and controls
137 lines (111 loc) · 3.48 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
MPAPI = SMODS.current_mod
-- Add mod root and networking/ to package.path so the MQTT thread can
-- require("mqtt") (luamqtt at mqtt/init.lua) and require("openssl_ffi")
package.path = MPAPI.path .. '/?.lua;' .. MPAPI.path .. '/?/init.lua;' .. MPAPI.path .. '/networking/?.lua;' .. package.path
-----------------------------
-- CORE FUNCTIONS
-----------------------------
function MPAPI.sendDebugMessage(msg)
sendDebugMessage(msg, MPAPI.id)
end
function MPAPI.sendWarnMessage(msg)
sendWarnMessage(msg, MPAPI.id)
end
function MPAPI.load_mpapi_file(file)
local chunk, err = SMODS.load_file(file, MPAPI.id)
if chunk then
local ok, func = pcall(chunk)
if ok then
return func
else
MPAPI.sendWarnMessage('Failed to process file: ' .. func)
end
else
MPAPI.sendWarnMessage('Failed to find or compile file: ' .. tostring(err))
end
return nil
end
function MPAPI.load_mpapi_dir(directory, recursive)
recursive = recursive or false
local dir_path = MPAPI.path .. '/' .. directory
local items = NFS.getDirectoryItemsInfo(dir_path)
for _, item in ipairs(items) do
local path = directory .. '/' .. item.name
MPAPI.sendDebugMessage('Loading item: ' .. path)
if item.type ~= 'directory' then
MPAPI.load_mpapi_file(path)
elseif recursive then
MPAPI.load_mpapi_dir(path, recursive)
end
end
end
-----------------------------
-- MODULES
-----------------------------
MPAPI.modules = {}
MPAPI.modules.openssl_ffi = MPAPI.load_mpapi_file('networking/openssl_ffi.lua')
if MPAPI.modules.openssl_ffi then
MPAPI.sendDebugMessage('OpenSSL FFI module loaded')
local available = MPAPI.modules.openssl_ffi.available()
if available then
local ctx, err = MPAPI.modules.openssl_ffi.new_context({ verify = false })
if ctx then
MPAPI.modules.openssl_ffi.free_context(ctx)
else
MPAPI.sendWarnMessage('SSL context creation FAILED: ' .. tostring(err))
end
end
else
MPAPI.sendWarnMessage('OpenSSL FFI module failed to load')
end
MPAPI.modules.mqtt_client = MPAPI.load_mpapi_file('networking/mqtt_client.lua')
if MPAPI.modules.mqtt_client then
MPAPI.sendDebugMessage('MQTT client wrapper loaded')
else
MPAPI.sendWarnMessage('MQTT client wrapper failed to load')
end
MPAPI.modules.steam = MPAPI.load_mpapi_file('networking/steam.lua')
if MPAPI.modules.steam then
MPAPI.sendDebugMessage('Steam module loaded (G.STEAM available after love.load)')
else
MPAPI.sendWarnMessage('Steam module failed to load')
end
MPAPI.modules.token_store = MPAPI.load_mpapi_file('networking/token_store.lua')
MPAPI.modules.api_client = MPAPI.load_mpapi_file('networking/api_client.lua')
MPAPI.modules.connection = MPAPI.load_mpapi_file('networking/connection.lua')
-----------------------------
-- FILE LOADING & STARTUP
-----------------------------
function MPAPI.update()
-- This will be intentionally hooked by other files in the mod
end
MPAPI._internal = {}
MPAPI.load_mpapi_dir('lib')
MPAPI.load_mpapi_dir('api')
MPAPI.load_mpapi_dir('ui')
-- Load dev overrides if the dev/ directory exists (stripped in release builds)
local dev_init = MPAPI.load_mpapi_file('dev/init.lua')
if dev_init then
MPAPI.sendDebugMessage('Dev overrides loaded')
end
G.E_MANAGER:add_event(Event({
blockable = false,
blocking = false,
no_delete = true,
func = function()
MPAPI.update()
end,
}))
G.E_MANAGER:add_event(Event({
blockable = false,
blocking = false,
func = function()
if not G.STEAM then
return false
end
MPAPI._internal.set_ready(true)
MPAPI.connect()
MPAPI._internal.run_ready_callbacks()
return true
end,
}))