Skip to content
This repository has been archived by the owner on Apr 13, 2024. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
zf-development committed Jul 6, 2023
0 parents commit 893e7b6
Show file tree
Hide file tree
Showing 17 changed files with 968 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
38 changes: 38 additions & 0 deletions fxmanifest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
fx_version 'cerulean'
game 'gta5'
use_experimental_fxv2_oal 'yes'
lua54 'yes'

author 'ZF Labo'
description 'A library for FiveM developers to make their life easier when using QBCore & ESX.'
version '1.2.0'

dependencies {
'/server:5848',
'/onesync',
}

files {
'init.lua',
'imports/**/client.lua',
'imports/**/shared.lua',
}

shared_script 'modules/init.lua'

shared_scripts {
'@ox_lib/init.lua',
'@es_extended/imports.lua',

'modules/**/shared.lua',
'locales.lua',
}

client_scripts {
'modules/**/client.lua',
'modules/**/client/*.lua'
}

server_scripts {
'modules/**/server.lua',
}
64 changes: 64 additions & 0 deletions imports/require/shared.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local loaded = {}

package = {
loaded = setmetatable({}, {
__index = loaded,
__newindex = noop,
__metatable = false,
}),
path = './?.lua;'
}

local _require = require

---Loads the given module inside the current resource, returning any values returned by the file or `true` when `nil`.
---@param modname string
---@return unknown?
function zf.require(modname)
if type(modname) ~= 'string' then return end

local module = loaded[modname]

if not module then
if module == false then
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
end

local success, result = pcall(_require, modname)

if success then
loaded[modname] = result
return result
end

local modpath = modname:gsub('%.', '/')

for path in package.path:gmatch('[^;]+') do
local scriptPath = path:gsub('?', modpath):gsub('%.+%/+', '')
local resourceFile = LoadResourceFile(cache.resource, scriptPath)

if resourceFile then
loaded[modname] = false
scriptPath = ('@@%s/%s'):format(cache.resource, scriptPath)

local chunk, err = load(resourceFile, scriptPath)

if err or not chunk then
loaded[modname] = nil
return error(err or ("unable to load module '%s'"):format(modname), 3)
end

module = chunk(modname) or true
loaded[modname] = module

return module
end
end

return error(("module '%s' not found"):format(modname), 2)
end

return module
end

return zf.require
186 changes: 186 additions & 0 deletions init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
local zflib = 'zf-lib'
local export = exports[zflib]

if not GetResourceState(zflib):find('start') then
error('^1zf-lib should be started before this resource.^0', 2)
end



-----------------------------------------------
--------------- Module handling ---------------
-----------------------------------------------
local LoadResourceFile = LoadResourceFile
local context = IsDuplicityVersion() and 'server' or 'client'

function noop() end

local function loadModule(self, module)
local dir = ('modules/%s'):format(module)
local chunk = LoadResourceFile(zflib, ('%s/%s.lua'):format(dir, context))
local shared = LoadResourceFile(zflib, ('%s/shared.lua'):format(dir))

if shared then
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
end

if chunk then
local fn, err = load(chunk, ('@@' .. zflib .. '/%s/%s.lua'):format(module, context))

if not fn or err then
return error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
end

local result = fn()
self[module] = result or noop
return self[module]
end
end

local function call(self, index, ...)
local module = rawget(self, index)

if not module then
self[index] = noop
module = loadModule(self, index)

if not module then
local function method(...)
return export[index](nil, ...)
end

if not ... then
self[index] = method
end

return method
end
end

return module
end

zf = setmetatable({
name = zflib,
context = context
}, {
__index = call,
__call = call,
})

require = zf.require



-----------------------------------------------
------------- Locales from QBCore -------------
-----------------------------------------------
Locale = {}
Locale.__index = Locale

local function translateKey(phrase, subs)
if type(phrase) ~= 'string' then
error('TypeError: translateKey function expects arg #1 to be a string')
end

if not subs then
return phrase
end

local result = phrase

for k, v in pairs(subs) do
local templateToFind = '%%{' .. k .. '}'
result = result:gsub(templateToFind, tostring(v)) -- string to allow all types
end

return result
end

function Locale.new(_, opts)
local self = setmetatable({}, Locale)

self.fallback = opts.fallbackLang and Locale:new({
warnOnMissing = false,
phrases = opts.fallbackLang.phrases,
}) or false

self.warnOnMissing = type(opts.warnOnMissing) ~= 'boolean' and true or opts.warnOnMissing

self.phrases = {}
self:extend(opts.phrases or {})

return self
end

function Locale:extend(phrases, prefix)
for key, phrase in pairs(phrases) do
local prefixKey = prefix and ('%s.%s'):format(prefix, key) or key
-- If this is a nested table, we need to go reeeeeeeeeeeecursive
if type(phrase) == 'table' then
self:extend(phrase, prefixKey)
else
self.phrases[prefixKey] = phrase
end
end
end

function Locale:clear()
self.phrases = {}
end

function Locale:replace(phrases)
phrases = phrases or {}
self:clear()
self:extend(phrases)
end

function Locale:locale(newLocale)
if (newLocale) then
self.currentLocale = newLocale
end
return self.currentLocale
end

function Locale:t(key, subs)
local phrase, result
subs = subs or {}

if type(self.phrases[key]) == 'string' then
phrase = self.phrases[key]
else
if self.warnOnMissing then
print(('^3Warning: Missing phrase for key: "%s"'):format(key))
end
if self.fallback then
return self.fallback:t(key, subs)
end
result = key
end

if type(phrase) == 'string' then
result = translateKey(phrase, subs)
end

return result
end

function Locale:has(key)
return self.phrases[key] ~= nil
end

function Locale:delete(phraseTarget, prefix)
if type(phraseTarget) == 'string' then
self.phrases[phraseTarget] = nil
else
for key, phrase in pairs(phraseTarget) do
local prefixKey = prefix and prefix .. '.' .. key or key

if type(phrase) == 'table' then
self:delete(phrase, prefixKey)
else
self.phrases[prefixKey] = nil
end
end
end
end
65 changes: 65 additions & 0 deletions modules/blip/client.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
Blips = {}
JobBlips = {}
GangBlips = {}

function zf.registerBlip(coords, label, sprite, colour, scale, display)
local res = GetInvokingResource()
local blip = AddBlipForCoord(coords.x, coords.y, coords.z)
SetBlipSprite(blip, sprite)
SetBlipDisplay(blip, display)
SetBlipScale(blip, scale)
SetBlipColour(blip, colour)
SetBlipAsShortRange(blip, true)
BeginTextCommandSetBlipName("STRING")
AddTextComponentString(label)
EndTextCommandSetBlipName(blip)

if Blips[res] and Blips[res].blips then
Blips[res].blips[#Blips[res].blips + 1] = blip
else
Blips[res] = {}
Blips[res].blips = {}
Blips[res].blips[#Blips[res].blips + 1] = blip
end
end

function zf.registerJobBlip(job, coords, label, sprite, colour, scale, display)
end

function zf.registerGangBlip(gang, coords, label, sprite, colour, scale, display)
end

local function removeJobBlip()
end

local function removeGangBlip()
end

local function removeResourceBlips(resname)
if Blips[resname] and Blips[resname].blips then
for id,blip in pairs(Blips[resname].blips) do
RemoveBlip(blip)
end
Blips[resname] = {}
end

if JobBlips[resname] and JobBlips[resname].blips then
for id,blip in pairs(JobBlips[resname].blips) do
RemoveBlip(blip)
end
JobBlips[resname] = {}
end

if GangBlips[resname] and GangBlips[resname].blips then
for id,blip in pairs(GangBlips[resname].blips) do
RemoveBlip(blip)
end
GangBlips[resname] = {}
end
end

AddEventHandler('onResourceStop', function(resname)
if Blips[resname] or JobBlips[resname] or GangBlips[resname] then
removeResourceBlips(resname)
end
end)
Loading

0 comments on commit 893e7b6

Please sign in to comment.