mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
refactor(settings): safeGetKvp
If the kvp has an unexpected type for whatever reason, clear it.
This commit is contained in:
@@ -4,15 +4,30 @@ if GetResourceKvpInt('reset_locale') ~= 1 then
|
||||
SetResourceKvpInt('reset_locale', 1)
|
||||
end
|
||||
|
||||
---@generic T
|
||||
---@param fn fun(key): unknown
|
||||
---@param key string
|
||||
---@param default? T
|
||||
---@return T
|
||||
local function safeGetKvp(fn, key, default)
|
||||
local ok, result = pcall(fn, key)
|
||||
|
||||
if not ok then
|
||||
return DeleteResourceKvp(key)
|
||||
end
|
||||
|
||||
return result or default
|
||||
end
|
||||
|
||||
local settings = {
|
||||
default_locale = GetConvar('ox:locale', 'en'),
|
||||
notification_position = GetResourceKvpString('notification_position') or 'top-right',
|
||||
notification_audio = GetResourceKvpInt('notification_audio') == 1
|
||||
notification_position = safeGetKvp(GetResourceKvpString, 'notification_position', 'top-right'),
|
||||
notification_audio = safeGetKvp(GetResourceKvpInt, 'notification_audio') == 1
|
||||
}
|
||||
|
||||
local userLocales = GetConvarInt('ox:userLocales', 1) == 1
|
||||
|
||||
settings.locale = userLocales and GetResourceKvpString('locale') or settings.default_locale
|
||||
settings.locale = userLocales and safeGetKvp(GetResourceKvpString, 'locale') or settings.default_locale
|
||||
|
||||
local function set(key, value)
|
||||
if settings[key] == value then return false end
|
||||
|
||||
Reference in New Issue
Block a user