From 080b08e625742192e179a36d67e80adb1c829eb3 Mon Sep 17 00:00:00 2001 From: rubin Date: Thu, 27 Oct 2022 10:51:45 +0300 Subject: [PATCH] rewritten auto update module --- easy-cruise.lua | 123 +++++++++++++++++++++++------------------------- 1 file changed, 58 insertions(+), 65 deletions(-) diff --git a/easy-cruise.lua b/easy-cruise.lua index 371ee64..bbd2626 100644 --- a/easy-cruise.lua +++ b/easy-cruise.lua @@ -1,24 +1,22 @@ script_name("easy-cruise") script_author("rubin") -script_version("22.10.26.1") +script_version("23.10.26.1") -encoding = require "encoding" -encoding.default = "CP1251" -u8 = encoding.UTF8 +inicfg = require "inicfg" +dlstatus = require("moonloader").download_status +vkeys = require "vkeys" +encoding = require "encoding" +encoding.default = "CP1251" +u8 = encoding.UTF8 -dlstatus = require("moonloader").download_status - -vkeys = require("vkeys") - -main_key_start = { "VK_LSHIFT" } -additional_key_start = { "VK_W", "VK_S", "VK_SPACE" } -cruise = false +main_key_start = { "VK_LSHIFT" } +additional_key_start = { "VK_W", "VK_S", "VK_SPACE" } +cruise = false function main() repeat wait(0) until isSampAvailable() - lua_thread.create(update_check) -->> Удалить чтобы скрипт не проверял обновления - + lua_thread.create(script_update.main) -->> Удалить чтобы скрипт не проверял обновления while true do wait(0) doCruise() @@ -66,7 +64,53 @@ function check_press_key(table) return result, result_key_text end -function download(url, fpath) +script_update = { + check_update = false, + check_update_url = "https://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/version", + auto_update = false, + auto_update_url = "https://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/easy-cruise.lua", + address_ini = "rubin-scripts-updates.ini", + main = function() + local name = thisScript().name + local ini = inicfg.load({ + [name] = { + check_update = true, + auto_update = true + } + }, script_update.address_ini) + script_update.check_update = ini[name].check_update + script_update.auto_update = ini[name].auto_update + inicfg.save(ini, script_update.address_ini) + if script_update.check_update then + local fpath = os.tmpname() + local text = openURL(script_update.check_update_url, fpath) + if text ~= "" and not string.find(text, thisScript().version) then + addChatMessage( string.format("Вышла новая версия '%s'. Текущая: '%s'", text, thisScript().version) ) + local command = thisScript().name:gsub(" ", "").."_update" + addChatMessage( string.format("Чтобы обновиться введите /%s'", command) ) + sampRegisterChatCommand(command, script_update.command) + end + end + end, + command = function() + lua_thread.create(function() + local fpath = os.tmpname() + local text = openURL(script_update.auto_update_url, fpath) + if text ~= "" and text:find(thisScript().name:gsub("%-", "%%-")) then + local file, error = io.open(thisScript().path, "w") + if file ~= nil then + file:write(text) + file:flush() + io.close(file) + addChatMessage("Обновление завершено, скрипт перезагружен!") + lua_thread.create(function() wait(500) thisScript():reload() end) + end + end + end) + end +} + +function openURL(url, fpath) local final_download = false downloadUrlToFile(url, fpath, function(id, status, p1, p2) if status == dlstatus.STATUS_ENDDOWNLOADDATA then @@ -77,9 +121,7 @@ function download(url, fpath) repeat wait(1000) until final_download -end -function read(fpath) local text = "" local f = io.open(fpath, "r") if f then @@ -90,55 +132,6 @@ function read(fpath) return text end -function update_check() - local fpath = os.tmpname() - download("https://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/version", fpath) - local text = read(fpath) - if text ~= "" and not string.find(text, thisScript().version) then - addChatMessage( string.format("Вышла новая версия '%s'. Текущая: '%s'", text, thisScript().version) ) - local command = thisScript().name:gsub(" ", "").."_update" - addChatMessage( string.format("Чтобы обновиться введите /%s'", command) ) - sampRegisterChatCommand(command, cmd) - end -end - -function update() - local fpath = os.tmpname() - local final_download = false - downloadUrlToFile("https://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/easy-cruise.lua", fpath, - function(id, status, p13, p23) - addChatMessage(status) - if status == dlstatus.STATUS_ENDDOWNLOADDATA then - final_download = true - end - end - ) - - repeat - wait(0) - until final_download - wait(1000) - - local f = io.open(fpath, "r") - if f then - local data = f:read("*a") - local file, error = io.open(thisScript().path, "w") - if file ~= nil then - file:write(data) - file:flush() - io.close(file) - addChatMessage("Обновление завершено, скрипт перезагружен!") - lua_thread.create(function() wait(500) thisScript():reload() end) - end - io.close(f) - end - os.remove(fpath) -end - -function cmd() - lua_thread.create(update) -end - function addChatMessage(text) local tag = string.format("{667dff}[%s]{FFFFFF} ", thisScript().name) sampAddChatMessage(tag..u8:decode(text), 0xFFFFFFFF)