From 43e113afeb3ed09cf4f0c590639c769dc8647f3b Mon Sep 17 00:00:00 2001 From: rubin Date: Mon, 9 Jan 2023 22:59:50 +0300 Subject: [PATCH] replace autoupdate --- TruckHUD.lua | 313 +++++++++++++++++++++++++++------------------------ 1 file changed, 169 insertions(+), 144 deletions(-) diff --git a/TruckHUD.lua b/TruckHUD.lua index ea3520c..a764f1f 100644 --- a/TruckHUD.lua +++ b/TruckHUD.lua @@ -1,7 +1,7 @@ script_name("TruckHUD") script_author("Serhiy_Rubin") script_version("2209011") - +collectgarbage("stop") local inicfg = require "inicfg" local dlstatus = require("moonloader").download_status @@ -205,9 +205,6 @@ function main() end mon_time = inifiles.tmonitor.time - if inifiles.Settings.ad then - check_version(false) - end menu[3].run = inifiles.Settings.Report font = renderCreateFont(inifiles.Render.FontName, inifiles.Render.FontSize, inifiles.Render.FontFlag) @@ -216,6 +213,7 @@ function main() lua_thread.create(fastmap) lua_thread.create(renderTruckers) lua_thread.create(luChecker.checker) + lua_thread.create(script_update.main) @@ -831,7 +829,7 @@ function doDialog() end if str:find("Очистить неиспользуемую память скрипта") then local mem_do = string.format('%0.2f MB', (tonumber(gcinfo()) / 1000)) - collectgarbage() + ~() sampAddChatMessage('Памяти очищена. Было: '..mem_do..'. Стало: '..string.format('%0.2f MB', (tonumber(gcinfo()) / 1000)), -1) ShowDialog1(1) end @@ -2121,55 +2119,6 @@ function loadEvents() function sampev.onSendCommand(cmd) local command, params = string.match(cmd, "^%/([^ ]*)(.*)") if command ~= nil and params ~= nil and command:lower() == "truck" then - if params:lower() == " ad" then - inifiles.Settings.ad = not inifiles.Settings.ad - settings_save() - sampAddChatMessage( - string.format( - "Уведомления об обновлениях Truck HUD %s", - (inifiles.Settings.ad and "включены" or "выключены") - ), - 0xFF2f72f7 - ) - return false - end - if params:lower() == " version" then - lua_thread.create( - function() - local fpath = os.getenv("TEMP") .. "\\TruckHUD-up.txt" - downloadUrlToFile( - "http://thprivate.deadpoo.net/download/changelog", - fpath, - function(id, status, p1, p2) - if stop_downloading_3 then - stop_downloading_3 = false - return false - end - if status == dlstatus.STATUS_ENDDOWNLOADDATA then - local f = io.open(fpath, "r") - if f then - local text = f:read("*a") - if text ~= nil then - sampShowDialog(222, "Список обновлений TruckHUD", "{FFFFFF}Расшифровка версии: ГодМесяцДеньНомер\n/truck download [ver] - Загрузить нужную версию\n \n" ..text, "Закрыть", "", 2) - end - io.close(f) - os.remove(fpath) - end - end - end - ) - end - ) - return false - end - if params:lower() == " download" then - check_version(true) - return false - end - if params:lower():find(" download (%d+)") then - check_version(true, params:lower():match(" download (%d+)")) - return false - end if params:lower() == " menu" then ShowDialog1(1) return false @@ -2987,94 +2936,6 @@ function isTruckCar() end end -function check_version(update, downgrade_ver) - local fpath = os.getenv("TEMP") .. "\\truck-version.txt" - downloadUrlToFile( - "http://thprivate.deadpoo.net/download/version", - fpath, - function(id, status, p1, p2) - if status == dlstatus.STATUS_ENDDOWNLOADDATA then - local f = io.open(fpath, "r") - if f then - local data = decodeJson(f:read("*a")) - if data ~= nil then - if data.latest ~= tostring(thisScript().version) or downgrade_ver ~= nil then - if not update then - sampAddChatMessage( ">> Вышло обновление для TruckHUD, версия " .. data.latest .. ". Текущая версия " .. thisScript().version, 0xFF2f72f7) - sampAddChatMessage( ">> Посмотреть список изменений: /truck version. Включить/Выключить уведомления: /truck ad", 0xFF2f72f7) - sampAddChatMessage( ">> Скачать на официальнаой странице Truck HUD: https://vk.com/rubin.mods", 0xFF2f72f7) - sampAddChatMessage( ">> ", 0xFF2f72f7) - sampAddChatMessage( ">> Либо обновить онлайн командой: /truck download", 0xFF2f72f7) - sampAddChatMessage( ">> Не забывайте залетать ко мне в группу =)", 0xFF2f72f7) - else - if downgrade_ver == nil then - sampAddChatMessage( ">> Вышло обновление для TruckHUD, версия " .. data.latest .. ". Текущая версия " .. thisScript().version, 0xFF2f72f7) - sampAddChatMessage( ">> Пытаюсь обновиться!", 0xFF2f72f7) - lua_thread.create(function(data) - wait(1000) - downloadUrlToFile(data.updateurl, thisScript().path, - function(id3, status1, p13, p23) - if status1 == dlstatus.STATUS_DOWNLOADINGDATA then - sampAddChatMessage(string.format('Загружено %d из %d.', p13, p23), 0xFF2f72f7) - elseif status1 == dlstatus.STATUS_ENDDOWNLOADDATA then - goupdatestatus = true - sampAddChatMessage('Загрузка обновления завершена.', 0xFF2f72f7) - lua_thread.create(function() wait(500) thisScript():reload() end) - end - if status1 == dlstatus.STATUSEX_ENDDOWNLOAD then - if goupdatestatus == nil then - sampAddChatMessage('Обновление прошло неудачно. Запускаю устаревшую версию.', 0xFF2f72f7) - end - end - end - ) - end, data) - else - sampAddChatMessage( ">> Выполняю поиск версии " .. downgrade_ver .. ". Текущая версия " .. thisScript().version, 0xFF2f72f7) - lua_thread.create(function(data) - wait(1000) - fpath = os.getenv("TEMP").."\\truck-update.txt" - downloadUrlToFile("http://thprivate.deadpoo.net/download/"..downgrade_ver, fpath, - function(id3, status1, p13, p23) - if status1 == dlstatus.STATUS_ENDDOWNLOADDATA then - local f = io.open(fpath, "r") - if f then - local data = f:read("*a") - if data == "TRUCKHUD" then - sampAddChatMessage('Такой версии нет в базе.', 0xFF2f72f7) - else - sampAddChatMessage('Загрузка завершена!', 0xFF2f72f7) - local file, error = io.open(thisScript().path, "w") - if file ~= nil then - file:write(data) - file:flush() - io.close(file) - lua_thread.create(function() wait(500) thisScript():reload() end) - end - end - io.close(f) - end - os.remove(fpath) - end - end - ) - end, data) - end - end - else - if update then - sampAddChatMessage( ">> У Вас последняя версия TruckHUD!", 0xFF2f72f7) - end - end - end - io.close(f) - os.remove(fpath) - end - end - end - ) -end - function GetGruz() local gruz = 0 if isCharInModel(PLAYER_PED, 514) then @@ -3769,7 +3630,7 @@ function get_time() msk_timestamp = msk_timestamp + (os.time() - _time) _time = os.time() if inifiles.Settings.AutoClear then - collectgarbage() + --collectgarbage() end end end @@ -4429,4 +4290,168 @@ function samp_create_sync_data(sync_type, copy_from_player) end } return setmetatable({send = func_send}, mt) -end \ No newline at end of file +end + +-->> UPDATE MODULE + +function openURL(url, fpath) + local result = false + local file_download = false + local download_final = false + + + if doesFileExist(fpath) then + say("remove") + os.remove(fpath) + end + + downloadUrlToFile(url, fpath, function(id, status, p1, p2) + if status == dlstatus.STATUS_ENDDOWNLOADDATA then + file_download = true + end + if status == dlstatus.STATUSEX_ENDDOWNLOAD then + download_final = true + end + end + ) + + repeat + wait(1000) + until download_final or file_download + + if file_download then + local text = "" + local f = io.open(fpath, "r") + if f then + text = f:read("*a") + io.close(f) + end + os.remove(fpath) + else + addChatMessage("Не удалось скачать обновление по ссылке:") + addChatMessage(url) + end + return result, text +end + + +function addChatMessage(text) + local tag = string.format("{667dff}[%s]{FFFFFF} ", thisScript().name) + sampAddChatMessage(tag..text, 0xFFFFFFFF) +end + +script_update = { + check_update = false, + check_update_url = "http://gith.deadpoo.net/rubin/TruckHUD/raw/branch/master/version", + auto_update = false, + auto_update_url = "http://gith.deadpoo.net/rubin/TruckHUD/raw/branch/master/TruckHUD.lua", + address_ini = "rubin-mods-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 or script_update.auto_update then + local fpath = os.tmpname() + local result, text = openURL(script_update.check_update_url, fpath) + if result then + if text ~= "" and not string.find(text, thisScript().version) then + addChatMessage( string.format("Вышла новая версия '%s'. Текущая: '%s'", text, thisScript().version) ) + if script_update.auto_update then + addChatMessage( string.format("Авто-Обновление скрипта включено. Процесс запущен!") ) + script_update.command() + else + local command = thisScript().name:gsub(" ", "").."_update" + sampRegisterChatCommand(command, script_update.command) + addChatMessage( string.format("Авто-Обновление скрипта выключено. Обновить самому: %s", command) ) + end + end + end + end + script_update.menu.init() + end, + command = function() + lua_thread.create(function() + local fpath = os.tmpname() + local result, text = openURL(script_update.auto_update_url, fpath) + if result and 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("Обновление завершено, скрипт перезагружен!") + wait(500) + thisScript():reload() + end + end + end) + end, + menu = { + text = {}, + dialog = {}, + init = function() + if not sampIsChatCommandDefined("rubin-mods") then + sampRegisterChatCommand("rubin-mods",script_update.menu.show) + while true do + wait(0) + local result, button, list, input = sampHasDialogRespond(2160) + if result and button == 1 then + if script_update.menu.text[list+1] ~= nil and script_update.menu.dialog[list+1] ~= nil then + script_update.menu.dialog[list+1]() + end + end + end + else + say("Команда зарегана") + end + end, + show = function() + script_update.menu.text = {} + script_update.menu.dialog = {} + local ini = inicfg.load({}, script_update.address_ini) + for k,v in pairs(ini) do + script_update.menu.text[#script_update.menu.text+1] = string.format(" ") + script_update.menu.dialog[#script_update.menu.dialog+1] = function() + script_update.menu.show() + end + + script_update.menu.text[#script_update.menu.text+1] = string.format("> %s", k) + script_update.menu.dialog[#script_update.menu.dialog+1] = function() + script_update.menu.show() + end + script_update.menu.text[#script_update.menu.text+1] = string.format("\tauto update %s", (ini[k].auto_update and "{59fc30}ON" or "{ff0000}OFF")) + script_update.menu.dialog[#script_update.menu.dialog+1] = function() + ini[k].auto_update = not ini[k].auto_update + inicfg.save(ini, script_update.address_ini) + script_update.menu.show() + end + if not ini[k].auto_update then + script_update.menu.text[#script_update.menu.text+1] = string.format("\tcheck update %s", (ini[k].check_update and "{59fc30}ON" or "{ff0000}OFF")) + script_update.menu.dialog[#script_update.menu.dialog+1] = function() + ini[k].check_update = not ini[k].check_update + inicfg.save(ini, script_update.address_ini) + script_update.menu.show() + end + end + script_update.menu.text[#script_update.menu.text+1] = string.format("\tdelete update settings") + script_update.menu.dialog[#script_update.menu.dialog+1] = function() + ini[k] = nil + inicfg.save(ini, script_update.address_ini) + script_update.menu.show() + end + end + local text = "" + for i = 1, #script_update.menu.text do + text = text..script_update.menu.text[i].."\n" + end + sampShowDialog(2160,"rubin-mods update settings",text,"Enter","Exit",2) + end + } +}