mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(init): call imports and exports via lib
Deprecate import (now just references lib); when calling or indexing lib for the first time, attempt to load a module else call an export.
This commit is contained in:
@@ -23,7 +23,7 @@ game 'gta5'
|
||||
--[[ Resource Information ]]--
|
||||
name 'ox_lib'
|
||||
author 'Linden'
|
||||
version '1.4.0'
|
||||
version '1.5.0'
|
||||
repository 'https://github.com/overextended/ox_lib'
|
||||
description 'A library of shared functions to utilise in other resources.'
|
||||
|
||||
@@ -41,6 +41,11 @@ files {
|
||||
|
||||
shared_script 'resource/main.lua'
|
||||
|
||||
shared_scripts {
|
||||
'resource/**/shared.lua',
|
||||
'resource/**/shared/*.lua'
|
||||
}
|
||||
|
||||
client_scripts {
|
||||
'resource/**/client.lua',
|
||||
'resource/**/client/*.lua'
|
||||
|
||||
53
init.lua
53
init.lua
@@ -16,7 +16,7 @@ local rawget = rawget
|
||||
local rawset = rawset
|
||||
local LoadResourceFile = LoadResourceFile
|
||||
|
||||
local function loadFile(self, module)
|
||||
local function loadModule(self, module)
|
||||
local dir = ('imports/%s'):format(module)
|
||||
local chunk = LoadResourceFile(lualib, ('%s/%s.lua'):format(dir, file))
|
||||
local shared = LoadResourceFile(lualib, ('%s/shared.lua'):format(dir))
|
||||
@@ -34,41 +34,39 @@ local function loadFile(self, module)
|
||||
rawset(self, module, chunk())
|
||||
return self[module]
|
||||
end
|
||||
else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end
|
||||
end
|
||||
end
|
||||
|
||||
--- Loads a module from the library.
|
||||
--- If the module has already been loaded then it will reference the existing chunk.
|
||||
---@param file string
|
||||
---@return table
|
||||
local function getImport(self, file)
|
||||
local import = rawget(self, file)
|
||||
return import and import or loadFile(self, file)
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------------------------------
|
||||
-- API
|
||||
-----------------------------------------------------------------------------------------------
|
||||
|
||||
import = setmetatable({}, {
|
||||
__index = getImport,
|
||||
local function call(self, index, ...)
|
||||
local module = rawget(self, index)
|
||||
if not module then
|
||||
module = loadModule(self, index)
|
||||
|
||||
__call = getImport,
|
||||
if not module then
|
||||
local function method(...)
|
||||
return exports[lualib][index](nil, ...)
|
||||
end
|
||||
|
||||
__newindex = function()
|
||||
error('Cannot set index on import')
|
||||
end
|
||||
})
|
||||
if not ... then
|
||||
rawset(self, index, method)
|
||||
end
|
||||
|
||||
local export = exports[lualib]
|
||||
|
||||
lib = setmetatable({}, {
|
||||
__index = function(_, method)
|
||||
return function(...)
|
||||
return export[method](nil, ...)
|
||||
return method
|
||||
end
|
||||
end,
|
||||
end
|
||||
|
||||
return module
|
||||
end
|
||||
|
||||
lib = setmetatable({
|
||||
exports = {},
|
||||
}, {
|
||||
__index = call,
|
||||
__call = call,
|
||||
|
||||
__newindex = function()
|
||||
error('Cannot set index on lib')
|
||||
@@ -78,6 +76,7 @@ lib = setmetatable({}, {
|
||||
return lualib
|
||||
end,
|
||||
})
|
||||
import = lib
|
||||
|
||||
local intervals = {}
|
||||
--- Dream of a world where this PR gets accepted.
|
||||
@@ -132,4 +131,4 @@ end
|
||||
-- GNU General Public License for more details.
|
||||
|
||||
-- You should have received a copy of the GNU General Public License
|
||||
-- along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>
|
||||
-- along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.html>
|
||||
|
||||
@@ -14,8 +14,6 @@ function lib.versionCheck(repository)
|
||||
local latestVersion = response.tag_name:match('%d%.%d+%.%d+')
|
||||
if not latestVersion then return end
|
||||
|
||||
print(currentVersion, latestVersion)
|
||||
|
||||
if currentVersion >= latestVersion then return end
|
||||
|
||||
print(('^3An update is available for %s (current version: %s)\r\n%s^0'):format(resource, currentVersion, response.html_url))
|
||||
@@ -23,14 +21,4 @@ function lib.versionCheck(repository)
|
||||
end)
|
||||
end
|
||||
|
||||
function lib.checkDependency(resource, minimumVersion)
|
||||
local currentVersion = GetResourceMetadata(resource, 'version', 0):match('%d%.%d+%.%d+')
|
||||
|
||||
if currentVersion < minimumVersion then
|
||||
return print(("^1%s requires version '%s' of '%s' (current version: %s)^0"):format(GetInvokingResource() or GetCurrentResourceName(), minimumVersion, resource, currentVersion))
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
lib.versionCheck('overextended/ox_lib')
|
||||
|
||||
9
resource/version/shared.lua
Normal file
9
resource/version/shared.lua
Normal file
@@ -0,0 +1,9 @@
|
||||
function lib.checkDependency(resource, minimumVersion)
|
||||
local currentVersion = GetResourceMetadata(resource, 'version', 0):match('%d%.%d+%.%d+')
|
||||
|
||||
if currentVersion < minimumVersion then
|
||||
return print(("^1%s requires version '%s' of '%s' (current version: %s)^0"):format(GetInvokingResource() or GetCurrentResourceName(), minimumVersion, resource, currentVersion))
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
Reference in New Issue
Block a user