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:
Linden
2022-02-28 02:40:25 +00:00
parent 37032fe0dd
commit e83aa653cc
4 changed files with 41 additions and 40 deletions

View File

@@ -23,7 +23,7 @@ game 'gta5'
--[[ Resource Information ]]-- --[[ Resource Information ]]--
name 'ox_lib' name 'ox_lib'
author 'Linden' author 'Linden'
version '1.4.0' version '1.5.0'
repository 'https://github.com/overextended/ox_lib' repository 'https://github.com/overextended/ox_lib'
description 'A library of shared functions to utilise in other resources.' description 'A library of shared functions to utilise in other resources.'
@@ -41,6 +41,11 @@ files {
shared_script 'resource/main.lua' shared_script 'resource/main.lua'
shared_scripts {
'resource/**/shared.lua',
'resource/**/shared/*.lua'
}
client_scripts { client_scripts {
'resource/**/client.lua', 'resource/**/client.lua',
'resource/**/client/*.lua' 'resource/**/client/*.lua'

View File

@@ -16,7 +16,7 @@ local rawget = rawget
local rawset = rawset local rawset = rawset
local LoadResourceFile = LoadResourceFile local LoadResourceFile = LoadResourceFile
local function loadFile(self, module) local function loadModule(self, module)
local dir = ('imports/%s'):format(module) local dir = ('imports/%s'):format(module)
local chunk = LoadResourceFile(lualib, ('%s/%s.lua'):format(dir, file)) local chunk = LoadResourceFile(lualib, ('%s/%s.lua'):format(dir, file))
local shared = LoadResourceFile(lualib, ('%s/shared.lua'):format(dir)) local shared = LoadResourceFile(lualib, ('%s/shared.lua'):format(dir))
@@ -34,41 +34,39 @@ local function loadFile(self, module)
rawset(self, module, chunk()) rawset(self, module, chunk())
return self[module] return self[module]
end end
else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end 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 -- API
----------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------
import = setmetatable({}, { local function call(self, index, ...)
__index = getImport, 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() if not ... then
error('Cannot set index on import') rawset(self, index, method)
end end
})
local export = exports[lualib] return method
lib = setmetatable({}, {
__index = function(_, method)
return function(...)
return export[method](nil, ...)
end end
end, end
return module
end
lib = setmetatable({
exports = {},
}, {
__index = call,
__call = call,
__newindex = function() __newindex = function()
error('Cannot set index on lib') error('Cannot set index on lib')
@@ -78,6 +76,7 @@ lib = setmetatable({}, {
return lualib return lualib
end, end,
}) })
import = lib
local intervals = {} local intervals = {}
--- Dream of a world where this PR gets accepted. --- Dream of a world where this PR gets accepted.

View File

@@ -14,8 +14,6 @@ function lib.versionCheck(repository)
local latestVersion = response.tag_name:match('%d%.%d+%.%d+') local latestVersion = response.tag_name:match('%d%.%d+%.%d+')
if not latestVersion then return end if not latestVersion then return end
print(currentVersion, latestVersion)
if currentVersion >= latestVersion then return end 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)) 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)
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') lib.versionCheck('overextended/ox_lib')

View 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