diff --git a/easy-cruise.lua b/easy-cruise.lua index b113bf7..371ee64 100644 --- a/easy-cruise.lua +++ b/easy-cruise.lua @@ -2,14 +2,23 @@ script_name("easy-cruise") script_author("rubin") script_version("22.10.26.1") +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 function main() repeat wait(0) until isSampAvailable() - cruise = false + + lua_thread.create(update_check) -->> Удалить чтобы скрипт не проверял обновления + while true do wait(0) doCruise() @@ -55,4 +64,82 @@ function check_press_key(table) end end return result, result_key_text +end + +function download(url, fpath) + local final_download = false + downloadUrlToFile(url, fpath, function(id, status, p1, p2) + if status == dlstatus.STATUS_ENDDOWNLOADDATA then + final_download = true + end + end) + + repeat + wait(1000) + until final_download +end + +function read(fpath) + local text = "" + local f = io.open(fpath, "r") + if f then + text = f:read("*a") + io.close(f) + end + os.remove(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) end \ No newline at end of file