-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
139 lines (115 loc) · 4.28 KB
/
init.lua
File metadata and controls
139 lines (115 loc) · 4.28 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
Bridge = _G.dBridge or {}
if not _VERSION:find('5.4') then
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 2)
end
local bridgeResName = 'div_bridge'
local currentResName = GetCurrentResourceName()
local context = IsDuplicityVersion() and 'server' or 'client'
local LoadResourceFile = LoadResourceFile
if rawget(Bridge, 'name') == bridgeResName then
error(("^1Cannot load %s more than once.^0"):format(bridgeResName))
end
msgpack.setoption('ignore_invalid', true)
local function debugPrint(...)
if not Bridge.config.Debug then return end
local args = {...}
for i = 1, #args do
args[i] = tostring(args[i])
end
print(('^3[div_bridge]^0 %s'):format(table.concat(args, ' ')))
end
local function loadOxLib()
if _G.lib then return end
if GetResourceState('ox_lib') ~= 'started' then
return error('^1[div_bridge] ox_lib must be started before this resource.^0', 0)
end
local chunk = LoadResourceFile('ox_lib', 'init.lua')
if not chunk then
return error('^1[div_bridge] Failed to load @ox_lib/init.lua^0', 0)
end
local fn, err = load(chunk, '@@ox_lib/init.lua', 't')
if not fn or err then
return error(('^1[div_bridge] Error loading ox_lib: %s^0'):format(err), 0)
end
fn()
end
local function loadModuleFile(moduleName)
local moduleDir = moduleName:lower()
local moduleType = Bridge.config[moduleName] or Bridge.config[moduleName:gsub("^%l", string.upper)]
if not moduleType then return nil end
local paths = {
('%s/modules/%s/%s/%s.lua'):format(bridgeResName, moduleDir, moduleType, context),
('%s/modules/%s/%s/shared.lua'):format(bridgeResName, moduleDir, moduleType),
('%s/modules/%s/%s.lua'):format(bridgeResName, moduleDir, moduleType)
}
local chunk, finalPath = nil, nil
for _, path in ipairs(paths) do
local fileContent = LoadResourceFile(bridgeResName, path:gsub(bridgeResName.."/", ""))
if fileContent then
chunk = fileContent
finalPath = path
break
end
end
if chunk then
local fn, err = load(chunk, ('@@%s'):format(finalPath))
if fn then
local result = fn()
rawset(Bridge, moduleName, result or {})
return result
else
print(("^1[div_bridge] Error in module %s (%s): %s^0"):format(moduleName, finalPath, err))
end
end
return nil
end
local function hasModule(name)
local key = tostring(name)
local mod = rawget(Bridge, key) or loadModuleFile(key)
return type(mod) == 'table'
end
local function initialize()
Bridge.config = Bridge.config or {}
local configContent = LoadResourceFile(bridgeResName, 'config.lua')
local configFn = configContent and load(configContent, '@@config.lua')
Bridge.config = configFn and configFn() or {}
local detectContent = LoadResourceFile(bridgeResName, 'detection.lua')
local detectFn = detectContent and load(detectContent, '@@detection.lua')
if detectFn then
Bridge.config = detectFn()(Bridge.config) or Bridge.config
end
Bridge.name = currentResName
Bridge.context = context
Bridge.debugPrint = debugPrint
Bridge.loadOxLib = loadOxLib
Bridge.HasModule = hasModule
for key, value in pairs(Bridge.config) do
if type(value) == 'string' and key ~= 'Debug' then
loadModuleFile(key)
end
end
end
initialize()
setmetatable(Bridge, {
__index = function(self, key)
local mod = loadModuleFile(key)
if mod then return mod end
return function()
if Bridge.config.Debug then
print(("^1[div_bridge] Attempted to call non-existent module/function: %s^0"):format(key))
end
end
end
})
if Bridge.config.Debug then
print("^3======= Diversity Bridge Initialized =======^7")
print("^3Debug Mode: ^2Enabled^7")
print("^3Framework: ^7" .. Bridge.config.Framework)
print("^3Inventory: ^7" .. Bridge.config.Inventory)
print("^3Database: ^7" .. Bridge.config.Database)
print("^3Interaction: ^7" .. Bridge.config.Interaction)
print("^3Notification: ^7" .. Bridge.config.Notification)
print("^3TextUI: ^7" .. Bridge.config.TextUI)
print("^3======= Diversity Bridge Initialized =======^7")
end
_G.dBridge = Bridge