Skip to content

Commit

Permalink
refactor(settings): safeGetKvp
Browse files Browse the repository at this point in the history
If the kvp has an unexpected type for whatever reason, clear it.
  • Loading branch information
thelindat committed Apr 22, 2024
1 parent a8df465 commit 55d9ed1
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions resource/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55d9ed1

Please sign in to comment.