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

Commit 893e7b6

Browse files
Initial commit
0 parents  commit 893e7b6

File tree

17 files changed

+968
-0
lines changed

17 files changed

+968
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

fxmanifest.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
fx_version 'cerulean'
2+
game 'gta5'
3+
use_experimental_fxv2_oal 'yes'
4+
lua54 'yes'
5+
6+
author 'ZF Labo'
7+
description 'A library for FiveM developers to make their life easier when using QBCore & ESX.'
8+
version '1.2.0'
9+
10+
dependencies {
11+
'/server:5848',
12+
'/onesync',
13+
}
14+
15+
files {
16+
'init.lua',
17+
'imports/**/client.lua',
18+
'imports/**/shared.lua',
19+
}
20+
21+
shared_script 'modules/init.lua'
22+
23+
shared_scripts {
24+
'@ox_lib/init.lua',
25+
'@es_extended/imports.lua',
26+
27+
'modules/**/shared.lua',
28+
'locales.lua',
29+
}
30+
31+
client_scripts {
32+
'modules/**/client.lua',
33+
'modules/**/client/*.lua'
34+
}
35+
36+
server_scripts {
37+
'modules/**/server.lua',
38+
}

imports/require/shared.lua

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
local loaded = {}
2+
3+
package = {
4+
loaded = setmetatable({}, {
5+
__index = loaded,
6+
__newindex = noop,
7+
__metatable = false,
8+
}),
9+
path = './?.lua;'
10+
}
11+
12+
local _require = require
13+
14+
---Loads the given module inside the current resource, returning any values returned by the file or `true` when `nil`.
15+
---@param modname string
16+
---@return unknown?
17+
function zf.require(modname)
18+
if type(modname) ~= 'string' then return end
19+
20+
local module = loaded[modname]
21+
22+
if not module then
23+
if module == false then
24+
error(("^1circular-dependency occurred when loading module '%s'^0"):format(modname), 2)
25+
end
26+
27+
local success, result = pcall(_require, modname)
28+
29+
if success then
30+
loaded[modname] = result
31+
return result
32+
end
33+
34+
local modpath = modname:gsub('%.', '/')
35+
36+
for path in package.path:gmatch('[^;]+') do
37+
local scriptPath = path:gsub('?', modpath):gsub('%.+%/+', '')
38+
local resourceFile = LoadResourceFile(cache.resource, scriptPath)
39+
40+
if resourceFile then
41+
loaded[modname] = false
42+
scriptPath = ('@@%s/%s'):format(cache.resource, scriptPath)
43+
44+
local chunk, err = load(resourceFile, scriptPath)
45+
46+
if err or not chunk then
47+
loaded[modname] = nil
48+
return error(err or ("unable to load module '%s'"):format(modname), 3)
49+
end
50+
51+
module = chunk(modname) or true
52+
loaded[modname] = module
53+
54+
return module
55+
end
56+
end
57+
58+
return error(("module '%s' not found"):format(modname), 2)
59+
end
60+
61+
return module
62+
end
63+
64+
return zf.require

init.lua

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
local zflib = 'zf-lib'
2+
local export = exports[zflib]
3+
4+
if not GetResourceState(zflib):find('start') then
5+
error('^1zf-lib should be started before this resource.^0', 2)
6+
end
7+
8+
9+
10+
-----------------------------------------------
11+
--------------- Module handling ---------------
12+
-----------------------------------------------
13+
local LoadResourceFile = LoadResourceFile
14+
local context = IsDuplicityVersion() and 'server' or 'client'
15+
16+
function noop() end
17+
18+
local function loadModule(self, module)
19+
local dir = ('modules/%s'):format(module)
20+
local chunk = LoadResourceFile(zflib, ('%s/%s.lua'):format(dir, context))
21+
local shared = LoadResourceFile(zflib, ('%s/shared.lua'):format(dir))
22+
23+
if shared then
24+
chunk = (chunk and ('%s\n%s'):format(shared, chunk)) or shared
25+
end
26+
27+
if chunk then
28+
local fn, err = load(chunk, ('@@' .. zflib .. '/%s/%s.lua'):format(module, context))
29+
30+
if not fn or err then
31+
return error(('\n^1Error importing module (%s): %s^0'):format(dir, err), 3)
32+
end
33+
34+
local result = fn()
35+
self[module] = result or noop
36+
return self[module]
37+
end
38+
end
39+
40+
local function call(self, index, ...)
41+
local module = rawget(self, index)
42+
43+
if not module then
44+
self[index] = noop
45+
module = loadModule(self, index)
46+
47+
if not module then
48+
local function method(...)
49+
return export[index](nil, ...)
50+
end
51+
52+
if not ... then
53+
self[index] = method
54+
end
55+
56+
return method
57+
end
58+
end
59+
60+
return module
61+
end
62+
63+
zf = setmetatable({
64+
name = zflib,
65+
context = context
66+
}, {
67+
__index = call,
68+
__call = call,
69+
})
70+
71+
require = zf.require
72+
73+
74+
75+
-----------------------------------------------
76+
------------- Locales from QBCore -------------
77+
-----------------------------------------------
78+
Locale = {}
79+
Locale.__index = Locale
80+
81+
local function translateKey(phrase, subs)
82+
if type(phrase) ~= 'string' then
83+
error('TypeError: translateKey function expects arg #1 to be a string')
84+
end
85+
86+
if not subs then
87+
return phrase
88+
end
89+
90+
local result = phrase
91+
92+
for k, v in pairs(subs) do
93+
local templateToFind = '%%{' .. k .. '}'
94+
result = result:gsub(templateToFind, tostring(v)) -- string to allow all types
95+
end
96+
97+
return result
98+
end
99+
100+
function Locale.new(_, opts)
101+
local self = setmetatable({}, Locale)
102+
103+
self.fallback = opts.fallbackLang and Locale:new({
104+
warnOnMissing = false,
105+
phrases = opts.fallbackLang.phrases,
106+
}) or false
107+
108+
self.warnOnMissing = type(opts.warnOnMissing) ~= 'boolean' and true or opts.warnOnMissing
109+
110+
self.phrases = {}
111+
self:extend(opts.phrases or {})
112+
113+
return self
114+
end
115+
116+
function Locale:extend(phrases, prefix)
117+
for key, phrase in pairs(phrases) do
118+
local prefixKey = prefix and ('%s.%s'):format(prefix, key) or key
119+
-- If this is a nested table, we need to go reeeeeeeeeeeecursive
120+
if type(phrase) == 'table' then
121+
self:extend(phrase, prefixKey)
122+
else
123+
self.phrases[prefixKey] = phrase
124+
end
125+
end
126+
end
127+
128+
function Locale:clear()
129+
self.phrases = {}
130+
end
131+
132+
function Locale:replace(phrases)
133+
phrases = phrases or {}
134+
self:clear()
135+
self:extend(phrases)
136+
end
137+
138+
function Locale:locale(newLocale)
139+
if (newLocale) then
140+
self.currentLocale = newLocale
141+
end
142+
return self.currentLocale
143+
end
144+
145+
function Locale:t(key, subs)
146+
local phrase, result
147+
subs = subs or {}
148+
149+
if type(self.phrases[key]) == 'string' then
150+
phrase = self.phrases[key]
151+
else
152+
if self.warnOnMissing then
153+
print(('^3Warning: Missing phrase for key: "%s"'):format(key))
154+
end
155+
if self.fallback then
156+
return self.fallback:t(key, subs)
157+
end
158+
result = key
159+
end
160+
161+
if type(phrase) == 'string' then
162+
result = translateKey(phrase, subs)
163+
end
164+
165+
return result
166+
end
167+
168+
function Locale:has(key)
169+
return self.phrases[key] ~= nil
170+
end
171+
172+
function Locale:delete(phraseTarget, prefix)
173+
if type(phraseTarget) == 'string' then
174+
self.phrases[phraseTarget] = nil
175+
else
176+
for key, phrase in pairs(phraseTarget) do
177+
local prefixKey = prefix and prefix .. '.' .. key or key
178+
179+
if type(phrase) == 'table' then
180+
self:delete(phrase, prefixKey)
181+
else
182+
self.phrases[prefixKey] = nil
183+
end
184+
end
185+
end
186+
end

modules/blip/client.lua

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Blips = {}
2+
JobBlips = {}
3+
GangBlips = {}
4+
5+
function zf.registerBlip(coords, label, sprite, colour, scale, display)
6+
local res = GetInvokingResource()
7+
local blip = AddBlipForCoord(coords.x, coords.y, coords.z)
8+
SetBlipSprite(blip, sprite)
9+
SetBlipDisplay(blip, display)
10+
SetBlipScale(blip, scale)
11+
SetBlipColour(blip, colour)
12+
SetBlipAsShortRange(blip, true)
13+
BeginTextCommandSetBlipName("STRING")
14+
AddTextComponentString(label)
15+
EndTextCommandSetBlipName(blip)
16+
17+
if Blips[res] and Blips[res].blips then
18+
Blips[res].blips[#Blips[res].blips + 1] = blip
19+
else
20+
Blips[res] = {}
21+
Blips[res].blips = {}
22+
Blips[res].blips[#Blips[res].blips + 1] = blip
23+
end
24+
end
25+
26+
function zf.registerJobBlip(job, coords, label, sprite, colour, scale, display)
27+
end
28+
29+
function zf.registerGangBlip(gang, coords, label, sprite, colour, scale, display)
30+
end
31+
32+
local function removeJobBlip()
33+
end
34+
35+
local function removeGangBlip()
36+
end
37+
38+
local function removeResourceBlips(resname)
39+
if Blips[resname] and Blips[resname].blips then
40+
for id,blip in pairs(Blips[resname].blips) do
41+
RemoveBlip(blip)
42+
end
43+
Blips[resname] = {}
44+
end
45+
46+
if JobBlips[resname] and JobBlips[resname].blips then
47+
for id,blip in pairs(JobBlips[resname].blips) do
48+
RemoveBlip(blip)
49+
end
50+
JobBlips[resname] = {}
51+
end
52+
53+
if GangBlips[resname] and GangBlips[resname].blips then
54+
for id,blip in pairs(GangBlips[resname].blips) do
55+
RemoveBlip(blip)
56+
end
57+
GangBlips[resname] = {}
58+
end
59+
end
60+
61+
AddEventHandler('onResourceStop', function(resname)
62+
if Blips[resname] or JobBlips[resname] or GangBlips[resname] then
63+
removeResourceBlips(resname)
64+
end
65+
end)

0 commit comments

Comments
 (0)