Skip to content

Instantly share code, notes, and snippets.

@Rubylium
Last active July 18, 2023 10:02
Show Gist options
  • Save Rubylium/2a785fa2da8a1b0a7dd99f3521324aaf to your computer and use it in GitHub Desktop.
Save Rubylium/2a785fa2da8a1b0a7dd99f3521324aaf to your computer and use it in GitHub Desktop.
JSON_BUILDER FiveM
JSON_BUILDER = {}
JSON_BUILDER.BasePath = "./server/generated/"
JSON_BUILDER.PathCache = {}
function JSON_BUILDER.LoadFile(name)
local data = {}
local loadFile = LoadResourceFile(GetCurrentResourceName(), JSON_BUILDER.BasePath..name)
if loadFile ~= nil then
data = json.decode(loadFile)
else
data = {}
SaveResourceFile(GetCurrentResourceName(), JSON_BUILDER.BasePath..name, json.encode(data), -1)
end
JSON_BUILDER.PathCache[name] = {
data = data,
needSave = false,
}
return data
end
function JSON_BUILDER.SaveFile(name)
if JSON_BUILDER.PathCache[name] ~= nil then
SaveResourceFile(GetCurrentResourceName(), JSON_BUILDER.BasePath..name, json.encode(JSON_BUILDER.PathCache[name].data), -1)
print("JSON_BUILDER.SaveFile: "..name.." saved")
else
CONSOLE.CriticalPrint("JSON_BUILDER.SaveFile: "..name.." not found in cache")
end
end
function JSON_BUILDER.MarkFileAsNeedingSave(name)
if JSON_BUILDER.PathCache[name] ~= nil then
JSON_BUILDER.PathCache[name].needSave = true
else
CONSOLE.CriticalPrint("JSON_BUILDER.MarkFileAsNeedingSave: "..name.." not found in cache")
end
end
function JSON_BUILDER.GetData(name)
if JSON_BUILDER.PathCache[name] ~= nil then
return JSON_BUILDER.PathCache[name].data
else
CONSOLE.CriticalPrint("JSON_BUILDER.GetData: "..name.." not found in cache")
end
end
function JSON_BUILDER.SetData(name, data)
if JSON_BUILDER.PathCache[name] ~= nil then
JSON_BUILDER.PathCache[name].data = data
else
CONSOLE.CriticalPrint("JSON_BUILDER.SetData: "..name.." not found in cache")
end
end
Citizen.CreateThread(function()
while true do
for k,v in pairs(JSON_BUILDER.PathCache) do
if v.needSave then
JSON_BUILDER.SaveFile(k)
v.needSave = false
end
end
Wait(10*1000)
end
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment