update module autoupdate & fix cruise

master
rubin 2 years ago
parent e660513ac6
commit 0f3e903906
  1. 325
      easy-cruise.lua

@ -1,6 +1,6 @@
script_name("easy-cruise") script_name("easy-cruise")
script_author("rubin") script_author("rubin")
script_version("22.10.26.1") script_version("18.02.2023")
inicfg = require "inicfg" inicfg = require "inicfg"
dlstatus = require("moonloader").download_status dlstatus = require("moonloader").download_status
@ -10,43 +10,37 @@ encoding.default = "CP1251"
u8 = encoding.UTF8 u8 = encoding.UTF8
main_key_start = { "VK_LSHIFT" } main_key_start = { "VK_LSHIFT" }
additional_key_start = { "VK_W", "VK_S", "VK_SPACE" } additional_key_start = { "VK_W", "VK_S" }
cruise = false cruise = false
function main() function main()
repeat wait(0) until isSampAvailable() repeat wait(0) until isSampAvailable()
lua_thread.create(script_update.main) -->> Удалить чтобы скрипт не проверял обновления lua_thread.create(script_update.main)
while true do while true do
wait(0) wait(0)
doCruise() local car = storeCarCharIsInNoSave(playerPed)
end if not isCharInAnyCar(playerPed) or not isCarEngineOn(car) then
end if cruise then
cruise = false
function doCruise() printStringNow('~R~cruise control - OFF', 1500)
local car = storeCarCharIsInNoSave(playerPed) end
if not isCharInAnyCar(playerPed) or not isCarEngineOn(car) then return
if cruise then
cruise = false
printStringNow('~R~cruise control - OFF', 1500)
end end
return if not sampIsChatInputActive() and not sampIsDialogActive() and not sampIsCursorActive() then
end if not cruise and select(1, check_press_key(additional_key_start)) and select(1, check_press_key(main_key_start)) then
if not sampIsChatInputActive() and not sampIsDialogActive() and not sampIsCursorActive() then cruise = true
if not cruise and select(1, check_press_key(additional_key_start)) and select(1, check_press_key(main_key_start)) then printStringNow('~G~cruise control - ON', 1500)
repeat repeat
wait(100) wait(100)
local result, text = check_press_key(additional_key_start) until not isKeyDown(vkeys["VK_W"])
printStringNow("~G~don't press "..text, 100) elseif cruise and not isKeyDown(vkeys["VK_LSHIFT"]) and select(1, check_press_key(additional_key_start)) then
until not result cruise = false
cruise = true printStringNow('~R~cruise control - OFF', 1500)
printStringNow('~G~cruise control - ON', 1500) end
elseif cruise and not isKeyDown(vkeys["VK_LSHIFT"]) and select(1, check_press_key(additional_key_start)) then end
cruise = false if cruise then
printStringNow('~R~cruise control - OFF', 1500) setGameKeyState(16, 255)
end end
end
if cruise then
setGameKeyState(16, 255)
end end
end end
@ -63,140 +57,227 @@ function check_press_key(table)
return result, result_key_text return result, result_key_text
end end
-->> UPDATE MODULE
function openURL(url, fpath)
local text = ""
local file_download = false
local download_final = false
if doesFileExist(fpath) then
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 f = io.open(fpath, "r")
if f then
text = f:read("*a")
io.close(f)
end
os.remove(fpath)
end
if (text:find("Not found") and not text:find('"Not found"')) or text == "" then
text = ""
addChatMessage("Не удалось скачать обновление по ссылке:")
addChatMessage(url)
end
return text
end
function addChatMessage(text)
local tag = string.format("{667dff}[%s]{FFFFFF} ", thisScript().name)
sampAddChatMessage(tag..text, 0xFFFFFFFF)
end
script_update = { script_update = {
check_update = false, version_url = "http://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/version",
check_update_url = "https://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/version", script_url = "http://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/easy-cruise.lua",
auto_update = false, changelog_url = "http://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/changelog",
auto_update_url = "https://git.deadpoo.net/rubin/easy-cruise/raw/branch/master/easy-cruise.lua", address_ini = string.format("rubin-mods-updates\\%s.ini", thisScript().name),
address_ini = "rubin-mods-updates.ini",
main = function() main = function()
local name = thisScript().name if not doesDirectoryExist("moonloader\\config\\rubin-mods-updates") then
createDirectory("moonloader\\config\\rubin-mods-updates")
end
local ini = inicfg.load({ local ini = inicfg.load({
[name] = { settings = {
check_update = true, check_update = true,
auto_update = true auto_update = true,
server_version = ""
} }
}, script_update.address_ini) }, script_update.address_ini)
script_update.check_update = ini[name].check_update ini.settings.version_url = script_update.version_url
script_update.auto_update = ini[name].auto_update ini.settings.script_url = script_update.script_url
inicfg.save(ini, script_update.address_ini) ini.settings.changelog_url = script_update.changelog_url
if script_update.check_update or script_update.auto_update then ini.settings.version = thisScript().version
ini.settings.script_name = thisScript().name
local command = (thisScript().name:gsub(" ", "").."-update"):lower()
sampRegisterChatCommand(command, script_update.command)
if ini.settings.check_update or ini.settings.auto_update then
local fpath = os.tmpname() local fpath = os.tmpname()
local text = openURL(script_update.check_update_url, fpath) local result, text = pcall(openURL, script_update.version_url, fpath)
if text ~= "" and not string.find(text, thisScript().version) then if result then
addChatMessage( string.format("Вышла новая версия '%s'. Текущая: '%s'", text, thisScript().version) ) ini.settings.server_version = text
if script_update.auto_update then if text ~= "" and text ~= thisScript().version then
addChatMessage( string.format("Авто-Обновление скрипта включено. Процесс запущен!") ) addChatMessage( string.format("Вышла новая версия '%s'. Текущая: '%s'", text, thisScript().version) )
script_update.command() if ini.settings.auto_update then
else addChatMessage( string.format("Автообновление скрипта включено. Процесс запущен!") )
local command = thisScript().name:gsub(" ", "").."_update" script_update.command()
sampRegisterChatCommand(command, script_update.command) else
addChatMessage( string.format("Авто-Обновление скрипта выключено. Обновить самому: %s", command) ) addChatMessage( string.format("Автообновление скрипта выключено. Обновить самому: /%s", command) )
end
end end
end end
end end
inicfg.save(ini, script_update.address_ini)
script_update.menu.init() script_update.menu.init()
end, end,
command = function() command = function()
lua_thread.create(function() lua_thread.create(function()
local fpath = os.tmpname() local fpath = os.tmpname()
local text = openURL(script_update.auto_update_url, fpath) local result, text = pcall(openURL, script_update.version_url, fpath)
if text ~= "" and text:find(thisScript().name:gsub("%-", "%%-")) then if result then
local file, error = io.open(thisScript().path, "w") if text ~= "" and text ~= thisScript().version then
if file ~= nil then addChatMessage( string.format("Вышла новая версия '%s'. Текущая: '%s'", text, thisScript().version) )
file:write(text) local fpath = os.tmpname()
file:flush() local result, text = pcall(openURL, script_update.script_url, fpath)
io.close(file) if result and text ~= "" and text:find(thisScript().name:gsub("%-", "%%-")) then
addChatMessage("Обновление завершено, скрипт перезагружен!") local file, error = io.open(thisScript().path, "w")
lua_thread.create(function() wait(500) thisScript():reload() end) if file ~= nil then
file:write(text)
file:flush()
io.close(file)
addChatMessage("Обновление завершено, скрипт перезагружен!")
wait(500)
thisScript():reload()
end
end
else
addChatMessage("У Вас установлена последняя версия!")
end end
end end
end) end)
end, end,
menu = { menu = {
text = {},
dialog = {}, dialog = {},
ini = {},
init = function() init = function()
if not sampIsChatCommandDefined("rubin-mods") then if not sampIsChatCommandDefined("rubin-mods") then
sampAddChatMessage("{667dff}[RUBIN MODS]{FFFFFF} Управление обновлениями скриптов: /rubin-mods", 0xFFFFFFFF)
sampRegisterChatCommand("rubin-mods",script_update.menu.show) sampRegisterChatCommand("rubin-mods",script_update.menu.show)
while true do while true do
wait(0) wait(0)
local result, button, list, input = sampHasDialogRespond(2160) local result, button, list, input = sampHasDialogRespond(2160)
if result and button == 1 then if result and button == 1 then
if script_update.menu.text[list+1] ~= nil and script_update.menu.dialog[list+1] ~= nil then if script_update.menu.ini[list+1] ~= nil and script_update.menu.dialog[list+1] ~= nil then
script_update.menu.dialog[list+1]() script_update.menu.dialog[list+1](script_update.menu.ini[list+1])
end end
end end
local result, button, list, input = sampHasDialogRespond(2162)
if result then
if button == 1 then
if script_update.menu2.text[list+1] ~= nil and script_update.menu2.dialog[list+1] ~= nil then
script_update.menu2.dialog[list+1]()
end
else
script_update.menu.show()
end
end
local result, button, list, input = sampHasDialogRespond(2161)
if result then
script_update.menu2.show(script_update.menu2.data)
end
end end
end end
end, end,
show = function() show = function()
script_update.menu.text = {}
script_update.menu.dialog = {} script_update.menu.dialog = {}
local ini = inicfg.load({}, script_update.address_ini) script_update.menu.ini = {}
for k,v in pairs(ini) do local text = ""
script_update.menu.text[#script_update.menu.text+1] = string.format(" ") if doesDirectoryExist("moonloader\\config\\rubin-mods-updates") then
script_update.menu.dialog[#script_update.menu.dialog+1] = function() local FileHandle, FileName = findFirstFile("moonloader\\config\\rubin-mods-updates\\*")
script_update.menu.show() while FileName ~= nil do
end if FileName ~= nil and FileName ~= ".." and FileName ~= "." and FileName:find("%.ini") then
local address = string.format("moonloader\\config\\rubin-mods-updates\\%s", FileName)
script_update.menu.text[#script_update.menu.text+1] = string.format("> %s", k) if doesFileExist(address) then
script_update.menu.dialog[#script_update.menu.dialog+1] = function() local ini = inicfg.load({}, address)
script_update.menu.show() script_update.menu.ini[#script_update.menu.ini+1] = address
text = string.format("%s%s\n", text, string.format("%s\t%s%s", ini.settings.script_name, (ini.settings.version == ini.settings.server_version and "{59fc30}" or "{ff0000}"),ini.settings.version))
script_update.menu.dialog[#script_update.menu.dialog+1] = function(data)
script_update.menu2.show(data)
end
end
end
FileName = findNextFile(FileHandle)
end 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")) findClose(FileHandle)
script_update.menu.dialog[#script_update.menu.dialog+1] = function() else
ini[k].auto_update = not ini[k].auto_update text = "Не найдена директория:\t\n moonloader\\config\\rubin-mods-updates\t"
inicfg.save(ini, script_update.address_ini) end
script_update.menu.show() sampShowDialog(2160,"Обновление скриптов: Rubin Mods","Скрипт\tВерсия\n"..text,"Выбрать","Закрыть",5)
end
},
menu2 = {
data = {},
text = {},
dialog = {},
show = function(data)
script_update.menu2.data = data
script_update.menu2.text = {}
script_update.menu2.dialog = {}
if doesFileExist(data) then
local ini = inicfg.load({}, data)
script_update.menu2.text[#script_update.menu2.text+1] = string.format("Автообновление %s", (ini.settings.auto_update and "{59fc30}ON" or "{ff0000}OFF"))
script_update.menu2.dialog[#script_update.menu2.dialog+1] = function()
ini.settings.auto_update = not ini.settings.auto_update
inicfg.save(ini, data)
script_update.menu2.show(data)
end end
if not ini[k].auto_update then if not ini.settings.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.menu2.text[#script_update.menu2.text+1] = string.format("Проверять обновления %s", (ini.settings.check_update and "{59fc30}ON" or "{ff0000}OFF"))
script_update.menu.dialog[#script_update.menu.dialog+1] = function() script_update.menu2.dialog[#script_update.menu2.dialog+1] = function()
ini[k].check_update = not ini[k].check_update ini.settings.check_update = not ini.settings.check_update
inicfg.save(ini, script_update.address_ini) inicfg.save(ini, data)
script_update.menu.show() script_update.menu2.show(data)
end end
end end
script_update.menu.text[#script_update.menu.text+1] = string.format("\tdelete update settings") script_update.menu2.text[#script_update.menu2.text+1] = string.format("Последние изменения")
script_update.menu.dialog[#script_update.menu.dialog+1] = function() script_update.menu2.dialog[#script_update.menu2.dialog+1] = function()
ini[k] = nil script_update.changelog(ini.settings.changelog_url, ini.settings.script_name)
inicfg.save(ini, script_update.address_ini) end
script_update.menu2.text[#script_update.menu2.text+1] = string.format("Удалить из списка")
script_update.menu2.dialog[#script_update.menu2.dialog+1] = function()
os.remove(data)
script_update.menu.show() script_update.menu.show()
end end
local text = ""
for i = 1, #script_update.menu2.text do
text = text..script_update.menu2.text[i].."\n"
end
sampShowDialog(2162,"Настройки обновления для "..ini.settings.script_name,text,"Выбрать","Назад",2)
end end
local text = ""
for i = 1, #script_update.menu.text do
text = text..u8:decode(script_update.menu.text[i]).."\n"
end
sampShowDialog(2160,"rubin-mods update settings",text,"Enter","Exit",2)
end end
} },
} changelog = function(url, name)
local fpath = os.tmpname()
function openURL(url, fpath) local result, text = pcall(openURL, url, fpath)
local final_download = false if result then
downloadUrlToFile(url, fpath, function(id, status, p1, p2) sampShowDialog(2161,"Changelog - "..name,text,"Выбрать","Назад",4)
if status == dlstatus.STATUS_ENDDOWNLOADDATA then
final_download = true
end end
end)
repeat
wait(1000)
until final_download
local text = ""
local f = io.open(fpath, "r")
if f then
text = f:read("*a")
io.close(f)
end end
os.remove(fpath) }
return text
end
function addChatMessage(text)
local tag = string.format("{667dff}[%s]{FFFFFF} ", thisScript().name)
sampAddChatMessage(tag..u8:decode(text), 0xFFFFFFFF)
end

Loading…
Cancel
Save