From bc5c7ad209872a7e9276e93b0b0f351283ef919b Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sat, 30 Oct 2021 15:58:25 +1100 Subject: [PATCH] Add files via upload --- fxmanifest.lua | 39 +++++++++++++++++++++ imports/callbacks/client.lua | 44 +++++++++++++++++++++++ imports/callbacks/server.lua | 16 +++++++++ init.lua | 67 ++++++++++++++++++++++++++++++++++++ shared.lua | 44 +++++++++++++++++++++++ 5 files changed, 210 insertions(+) create mode 100644 fxmanifest.lua create mode 100644 imports/callbacks/client.lua create mode 100644 imports/callbacks/server.lua create mode 100644 init.lua create mode 100644 shared.lua diff --git a/fxmanifest.lua b/fxmanifest.lua new file mode 100644 index 0000000..643d978 --- /dev/null +++ b/fxmanifest.lua @@ -0,0 +1,39 @@ +-- Library +-- Copyright (C) 2021 Linden + +-- 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 + +--[[ FX Information ]]-- +fx_version 'cerulean' +use_fxv2_oal 'yes' +lua54 'yes' +game 'gta5' + +--[[ Resource Information ]]-- +name 'library' +author 'Linden' +version '1.0.0' +repository 'https://github.com/project-error/pe-lib' +description 'A library of shared functions to utilise in other resources.' + +--[[ Manifest ]]-- +dependencies { + '/onesync', +} + +files { + 'init.lua', + 'imports/**/client.lua', + 'imports/**/shared.lua', +} \ No newline at end of file diff --git a/imports/callbacks/client.lua b/imports/callbacks/client.lua new file mode 100644 index 0000000..52f4933 --- /dev/null +++ b/imports/callbacks/client.lua @@ -0,0 +1,44 @@ +local ServerCallbacks = {} + +local function CallbackTimer(event, delay) + if type(delay) == 'number' then + local time = GetGameTimer() + if (ServerCallbacks[event] or 0) > time then + return false + end + ServerCallbacks[event] = time + delay + end +end + +local ServerCallback = table.create(0, 2) + +--- Sends an event to the server and halts the current thread until a response is returned. +---@param event string +---@param delay number +ServerCallback.Await = function(resource, event, delay, ...) + CallbackTimer(event, delay) + event = ('__cb_%s:%s'):format(resource, event) + TriggerServerEvent(event, ...) + local promise = promise.new() + event = RegisterNetEvent(event, function(...) + promise:resolve(...) + RemoveEventHandler(event) + end) + return Citizen.Await(promise.value) +end + +--- Sends an event to the server and triggers a callback function once the response is returned. +---@param event string +---@param delay number +ServerCallback.Async = function(resource, event, delay, cb, ...) + CallbackTimer(event, delay) + event = ('__cb_%s:%s'):format(resource, event) + print(event) + TriggerServerEvent(event, ...) + event = RegisterNetEvent(event, function(...) + cb(...) + RemoveEventHandler(event) + end) +end + +return ServerCallback \ No newline at end of file diff --git a/imports/callbacks/server.lua b/imports/callbacks/server.lua new file mode 100644 index 0000000..fcb61a1 --- /dev/null +++ b/imports/callbacks/server.lua @@ -0,0 +1,16 @@ +local ServerCallbacks = {} + +local ServerCallback = { + Register = function(name, callback) + name = ('__cb_%s:%s'):format(GetCurrentResourceName(), name) + + RegisterServerEvent(name, function(...) + local source = source + callback(source, function(...) + TriggerClientEvent(name, source, ...) + end, ...) + end) + end +} + +return ServerCallback \ No newline at end of file diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..2dbbe4d --- /dev/null +++ b/init.lua @@ -0,0 +1,67 @@ +if not _VERSION:find('5.4') then + error('^1Lua 5.4 must be enabled in the resource manifest!^0', 0) +end + +local LIBRARY = 'library' +local SERVICE = IsDuplicityVersion() and 'server' or 'client' +local RESOURCE = GetCurrentResourceName() + +local MODULES = {} +local IMPORTS = {} + +local function LoadModule(file, import) + if (import and not IMPORTS[file]) or (not import and not MODULES[file]) then + file = import and ('imports/%s'):format(file) or file + local chunk = LoadResourceFile(import or RESOURCE, ('%s/%s.lua'):format(file, SERVICE)) + local shared = LoadResourceFile(import or RESOURCE, ('%s/shared.lua'):format(file)) + if shared then + chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared + end + if chunk then + chunk, err = load(chunk, ('@@%s.lua'):format(SERVICE), 't') + if err then + error(('\n^1Error loading module (%s): %s^0'):format(file, err), 0) + else + 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 + +---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. +---If the module has already been loaded then it will reference the existing chunk. +---@param file string +---@return table +function import(file) return LoadModule(file, LIBRARY) end + +do + 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 + +-- Library +-- Copyright (C) 2021 Linden + +-- 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 \ No newline at end of file diff --git a/shared.lua b/shared.lua new file mode 100644 index 0000000..e5ebcd5 --- /dev/null +++ b/shared.lua @@ -0,0 +1,44 @@ +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 + +-- 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 \ No newline at end of file