Skip to content

Commit

Permalink
[Modify] Update check
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick2562 committed Apr 7, 2021
1 parent 6397aa9 commit 1d58f6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
2 changes: 1 addition & 1 deletion meta.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<meta>
<info author="!Patrick" name="pAttach" type="script" version="1.1.3"/>
<min_mta_version client="1.5.8-0.20837" />
<min_mta_version client="1.5.8-0.20837" />
<oop>true</oop>

<script src="updatecheck.lua" type="server" />
Expand Down
69 changes: 30 additions & 39 deletions updatecheck.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
-- SETTINGS -------------------------------------------------------------------------------------------
-- SETTINGS - You can modify these variables ----------------------------------------------------------
local CHECK_FOR_UPDATES = true -- change this to 'false' if you don't want to check for new releases!
-------------------------------------------------------------------------------------------------------


-- DON'T MODIFY THESE VARIABLES ---------------
local VERSION = 113
-----------------------------------------------

if CHECK_FOR_UPDATES then
addEventHandler("onResourceStart", resourceRoot, function()
fetchRemote("https://api.github.com/repos/Patrick2562/mtasa-pAttach/releases/latest", function(data, status)
Expand All @@ -11,46 +15,33 @@ if CHECK_FOR_UPDATES then
data = fromJSON(data)

if data then
local latest_id = tonumber(data["id"])
local tag_name = tostring(data["tag_name"])

if latest_id then
local file = fileExists("release") and fileOpen("release") or fileCreate("release")

if file then
local current_id = tonumber(fileRead(file, fileGetSize(file)))

fileSetPos(file, 0)

if not current_id then
fileWrite(file, tostring(latest_id))

elseif latest_id > current_id then
local asset = data["assets"][1]

if asset then
local path = "releases/"..asset["name"]

if fileExists(path) then
print("[pAttach] New release ("..tag_name..") available on Github! It's already downloaded into 'releases' directory inside pAttach, just replace the old one!")

else
fetchRemote(asset["browser_download_url"], function(data, status)
assert(status == 0 and data, "[pAttach] Can't download latest release ("..tag_name..") from Github! (Status code: "..tostring(status)..")")

local zip = fileCreate(path)
if zip then
fileWrite(zip, data)
fileClose(zip)

print("[pAttach] New release ("..tag_name..") available on Github! Automatically downloaded into 'releases' directory inside pAttach, just replace the old one!")
end
end)
end
local tag_name = tostring(data["tag_name"])
local latest_version = tonumber( (tag_name:gsub("v",""):gsub("%.","")) )

if latest_version then
if latest_version > VERSION then
local asset = data["assets"][1]

if asset then
local path = "releases/"..asset["name"]

if fileExists(path) then
print("[pAttach] New release ("..tag_name..") available on Github! It's already downloaded into 'releases' directory inside pAttach, just replace the old one!")

else
fetchRemote(asset["browser_download_url"], function(data, status)
assert(status == 0 and data, "[pAttach] Can't download latest release ("..tag_name..") from Github! (Status code: "..tostring(status)..")")

local zip = fileCreate(path)
if zip then
fileWrite(zip, data)
fileClose(zip)

print("[pAttach] New release ("..tag_name..") available on Github! Automatically downloaded into 'releases' directory inside pAttach, just replace the old one!")
end
end)
end
end

fileClose(file)
end
end
end
Expand Down

0 comments on commit 1d58f6d

Please sign in to comment.