refactor: remove modules system and fix indentation

Setting a "standard" module and resource structure is too limiting, so stick to import only.
This commit is contained in:
Linden
2021-11-11 07:52:38 +11:00
parent f72665b197
commit ab4807fee0
6 changed files with 72 additions and 119 deletions

16
.editorconfig Normal file
View File

@@ -0,0 +1,16 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
end_of_line = lf
insert_final_newline = false
charset = utf-8
trim_trailing_whitespace = true
indent_size = 2
indent_style = space
[*.lua]
indent_size = 4
indent_style = tab

View File

@@ -21,10 +21,10 @@ lua54 'yes'
game 'gta5' game 'gta5'
--[[ Resource Information ]]-- --[[ Resource Information ]]--
name 'library' name 'lua library'
author 'Linden' author 'Linden'
version '1.0.0' version '1.0.0'
repository 'https://github.com/project-error/pe-lib' repository 'https://github.com/project-error/pe-lualib'
description 'A library of shared functions to utilise in other resources.' description 'A library of shared functions to utilise in other resources.'
--[[ Manifest ]]-- --[[ Manifest ]]--

View File

@@ -1,13 +1,13 @@
local ServerCallbacks = {} local ServerCallbacks = {}
local function CallbackTimer(event, delay) local function CallbackTimer(event, delay)
if type(delay) == 'number' then if type(delay) == 'number' then
local time = GetGameTimer() local time = GetGameTimer()
if (ServerCallbacks[event] or 0) > time then if (ServerCallbacks[event] or 0) > time then
return false return false
end end
ServerCallbacks[event] = time + delay ServerCallbacks[event] = time + delay
end end
end end
local ServerCallback = table.create(0, 2) local ServerCallback = table.create(0, 2)
@@ -16,28 +16,28 @@ local ServerCallback = table.create(0, 2)
---@param event string ---@param event string
---@param delay number ---@param delay number
ServerCallback.Await = function(resource, event, delay, ...) ServerCallback.Await = function(resource, event, delay, ...)
CallbackTimer(event, delay) CallbackTimer(event, delay)
event = ('__cb_%s:%s'):format(resource, event) event = ('__cb_%s:%s'):format(resource, event)
TriggerServerEvent(event, ...) TriggerServerEvent(event, ...)
local promise = promise.new() local promise = promise.new()
event = RegisterNetEvent(event, function(...) event = RegisterNetEvent(event, function(...)
promise:resolve({...}) promise:resolve({...})
RemoveEventHandler(event) RemoveEventHandler(event)
end) end)
return table.unpack(Citizen.Await(promise)) return table.unpack(Citizen.Await(promise))
end end
--- Sends an event to the server and triggers a callback function once the response is returned. --- Sends an event to the server and triggers a callback function once the response is returned.
---@param event string ---@param event string
---@param delay number ---@param delay number
ServerCallback.Async = function(resource, event, delay, cb, ...) ServerCallback.Async = function(resource, event, delay, cb, ...)
CallbackTimer(event, delay) CallbackTimer(event, delay)
event = ('__cb_%s:%s'):format(resource, event) event = ('__cb_%s:%s'):format(resource, event)
TriggerServerEvent(event, ...) TriggerServerEvent(event, ...)
event = RegisterNetEvent(event, function(...) event = RegisterNetEvent(event, function(...)
cb(...) cb(...)
RemoveEventHandler(event) RemoveEventHandler(event)
end) end)
end end
return ServerCallback return ServerCallback

View File

@@ -1,16 +1,14 @@
local ServerCallbacks = {}
local ServerCallback = { local ServerCallback = {
Register = function(name, callback) Register = function(name, callback)
name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name) name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name)
RegisterServerEvent(name, function(...) RegisterServerEvent(name, function(...)
local source = source local source = source
callback(source, function(...) callback(source, function(...)
TriggerClientEvent(name, source, ...) TriggerClientEvent(name, source, ...)
end, ...) end, ...)
end) end)
end end
} }
return ServerCallback return ServerCallback

View File

@@ -1,53 +1,36 @@
if not _VERSION:find('5.4') then if not _VERSION:find('5.4') then
error('^1Lua 5.4 must be enabled in the resource manifest!^0', 0) error('^1Lua 5.4 must be enabled in the resource manifest!^0', 3)
end end
local LIBRARY = 'library' local LIBRARY = 'pe-lualib'
local SERVICE = IsDuplicityVersion() and 'server' or 'client' local SERVICE = IsDuplicityVersion() and 'server' or 'client'
local RESOURCE = GetCurrentResourceName() local IMPORTS = {}
local MODULES = {} local function loadFile(file)
local IMPORTS = {} local dir = ('imports/%s'):format(file)
local chunk = LoadResourceFile(LIBRARY, ('%s/%s.lua'):format(dir, SERVICE))
local function LoadModule(file, import) local shared = LoadResourceFile(LIBRARY, ('%s/shared.lua'):format(dir))
if (import and not IMPORTS[file]) or (not import and not MODULES[file]) then if shared then
file = import and ('imports/%s'):format(file) or file chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
local chunk = LoadResourceFile(import or RESOURCE, ('%s/%s.lua'):format(file, SERVICE)) end
local shared = LoadResourceFile(import or RESOURCE, ('%s/shared.lua'):format(file)) if chunk then
if shared then local err
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't')
end if err then
if chunk then error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't') else
if err then IMPORTS[file] = chunk()
error(('\n^1Error loading module (%s): %s^0'):format(file, err), 0) end
else else error(('\n^3Unable to import module (%s)^0'):format(dir), 3) end
if import then IMPORTS[file] = chunk() else MODULES[file] = chunk() end
end
else error(('\n^3Unable to load module (%s)^0'):format(file), 0) end
end
return (import and IMPORTS[file]) or MODULES[file]
end end
---Loads a module from within the current resource.
---If the module has already been loaded then it will reference the existing chunk.
---@param file string
---@return table
function include(file) return LoadModule(file) end
---Loads a module from the library. ---Loads a module from the library.
---If the module has already been loaded then it will reference the existing chunk. ---If the module has already been loaded then it will reference the existing chunk.
---@param file string ---@param file string
---@return table ---@return table
function import(file) return LoadModule(file, LIBRARY) end function import(file)
if not IMPORTS[file] then loadFile(file) end
do return IMPORTS[file]
local chunk, err = LoadResourceFile(RESOURCE, ('%s.lua'):format(SERVICE))
local shared = LoadResourceFile(RESOURCE, ('shared.lua'))
if shared then chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared end
chunk, err = load(chunk, ('@@%s/%s.lua'):format(RESOURCE, SERVICE), 't')
assert(chunk, ('^1%s^0'):format(err))
chunk()
end end
-- Library -- Library

View File

@@ -1,44 +0,0 @@
local SERVICE = IsDuplicityVersion() and 'server' or 'client'
local RESOURCE = GetCurrentResourceName()
local MODULES = {}
local function LoadChunk(file)
local chunk = LoadResourceFile(RESOURCE, ('imports/%s/%s.lua'):format(file, SERVICE))
local shared = LoadResourceFile(RESOURCE, ('imports/%s/shared.lua'):format(file))
if shared then
chunk = chunk and ('%s\n%s'):format(shared, chunk) or shared
end
if chunk then
return chunk and load(chunk, ('@%s.lua'):format(SERVICE), 't')
end
assert(nil, ('\n^Unable to load module (%s): module does not exist^0'):format(file))
end
function M(file)
if not MODULES[file] then
local chunk, err = LoadChunk(file)
if err then
assert(nil, ('\n^1Error loading module (%s): %s^0'):format(file, err))
else
MODULES[file] = chunk()
end
end
return MODULES[file]
end
-- Library
-- Copyright (C) 2021 Linden <https://github.com/thelindat>
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- 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>