Client-side:
local checking_updates = false
addEvent ( "update",true)
addEventHandler ( "update", getRootElement(getThisResource()),
function()
sx,sy = guiGetScreenSize()
jupdateWindow = guiCreateWindow((sx/2)-109,(sy/2)-74,218,148,"J UPDATE",false)
jupdateVersionLabel = guiCreateLabel(11,23,196,18,"J Login Version x.x.x",false,jupdateWindow)
guiLabelSetHorizontalAlign(jupdateVersionLabel,"center",true)
jUpdateButton = guiCreateButton(41,79,136,25,"Check for Updates",false,jupdateWindow)
jUpdateLabel = guiCreateLabel(11,45,196,33,"Update Status: unknown",false,jupdateWindow)
guiLabelSetHorizontalAlign(jUpdateLabel,"center",true)
jUpdateCheckbox = guiCreateCheckBox(9,116,200,18,"Check for updates automaticly",false,false,jupdateWindow)
addEventHandler("onClientGUIClick",jupdateWindow,function(button,state)
if button == "left" and state == "up" then
if source ~= jUpdateButton and source ~= jUpdateCheckbox then
hideWindow()
checking_updates = false
elseif source == jUpdateButton then
checking_updates = true
triggerServerEvent("checkUpdateStatus",getLocalPlayer(),true)
guiSetText(jUpdateLabel,"Checking...")
end
end
guiWindowSetSizable(jupdateWindow,false)
setElementData(jupdateWindow,"cant_edit",true)
setElementData(jupdateVersionLabel,"cant_edit",true)
setElementData(jUpdateButton,"cant_edit",true)
setElementData(jUpdateLabel,"cant_edit",true)
setElementData(jUpdateCheckbox,"cant_edit",true)
setElementData(jupdateVersionLabel,"cant_highlight",true)
setElementData(jUpdateLabel,"cant_highlight",true)
setElementData(jUpdateCheckbox,"cant_highlight",true)
guiSetVisible(jupdateWindow,false)
end)
end)
function hideWindow()
guiSetVisible(jupdateWindow,false)
settings.jupdate_update_check.value = guiCheckBoxGetSelected(jUpdateCheckbox)
saveSettingsFile()
end
addEvent("receiveUpdateCheck",true)
addEventHandler("receiveUpdateCheck",getRootElement(),function(update,newVersion,oldVersion,manual)
if checking_updates then
if update then
guiSetText(jUpdateLabel,"An update is available! (v. "..tostring(newVersion)..")")
if manual == false then
if not jupdate_version then jupdate_version = oldVersion end
showWindow()
end
elseif update == false then
guiSetText(jUpdateLabel,"No updates available")
else
guiSetText(jUpdateLabel,"Error: Check ACL permissions and MTA website availability.")
end
end
end)
function showWindow()
guiSetText(jUpdateLabel,"GUI Editor Version "..tostring(jupdate_version))
guiCheckBoxSetSelected(jUpdateCheckbox,settings.jupdate_update_check.value)
guiSetVisible(jupdateWindow,true)
guiBringToFront(jupdateWindow)
end
Server-side
function checkForUpdates(manual)
local player = client
local called = callRemote("https://community.multitheftauto.com/mta/resources.php",function(n,v) updateResult(n,v,player,manual) end,"version",string.lower(getResourceName(getThisResource())))
if not called then
-- outputChatBox("Error: Check ACL permissions and website availability.")
triggerClientEvent(client,"receiveUpdateCheck",client,nil)
end
end
addEvent("checkUpdateStatus",true)
addEventHandler("checkUpdateStatus",root,checkForUpdates)
function updateResult(name,version,player,manual)
local update = false
if string.lower(name):find("error") or version == 0 then update = nil end
if update == false then
local v1,v2,v3 = parseVersion(tostring(version))
local cv1,cv2,cv3 = parseVersion(tostring(getResourceInfo(getThisResource(),"version")))
if v1 and cv1 then
if v1 > cv1 then update = true
elseif v2 > cv2 then update = true
elseif v3 > cv3 then update = true
end
end
end
triggerClientEvent(player,"receiveUpdateCheck",player,update,version,tostring(getResourceInfo(getThisResource(),"version")),manual)
end
function parseVersion(version)
local parts = split(version,string.byte("."))
return parts[1],parts[2],parts[3]
end
Settings File:
settings = {}
settings.gui = {}
settings.jupdate_update_check = {value = false, type = "boolean"}
function saveSettingsFile()
local file = xmlLoadFile("settings.xml")
if not file then
file = xmlCreateFile("settings.xml","settings")
if file then
outputDebugString("Created GUI Editor settings file successfully.")
else
outputDebugString("Could not create GUI Editor settings file.")
return
end
end
for name,setting in pairs(settings) do
if setting and setting ~= settings.gui then
local node = getChild(file,tostring(name),0)
if node then
xmlNodeSetValue(node,tostring(setting.value))
else
outputDebugString("Failed to save GUI Editor '"..tostring(name).."' setting.")
end
end
end
xmlSaveFile(file)
xmlUnloadFile(file)
return
end
function loadSettingsFile()
local file = xmlLoadFile("settings.xml")
if not file then
outputDebugString("Couldnt find GUI Editor settings file. Creating...")
saveSettingsFile(false)
return
end
for i,node in ipairs(xmlNodeGetChildren(file)) do
local value = xmlNodeGetValue(node)
if value then
local name = xmlNodeGetName(node)
-- silly side effect of having :~ settings to begin with
-- redirect deprecated names onto their new counterparts
name = checkOutdatedNodes(node)
local formatted = formatValue(value,name)
if formatted ~= nil then
settings[name].value = formatted
-- outputDebugString("Loaded GUI Editor '"..name.."' setting. ("..tostring(formatted)..")")
else
outputDebugString("Failed to load GUI Editor '"..name.."' setting. Using default...")
end
end
end
xmlSaveFile(file)
xmlUnloadFile(file)
return
end
function checkOutdatedNodes(node)
local name = xmlNodeGetName(node)
if name == "updatecheck" then
xmlDestroyNode(node)
outputDebugString("Destroyed deprecated J Update settings node '"..name.."'.")
return "jupdate_update_check"
end
return name
end