added module config

master
rubin 2 years ago
parent 0f3e903906
commit 86530c8acd
  1. 76
      easy-cruise.lua

@ -16,6 +16,7 @@ cruise = false
function main()
repeat wait(0) until isSampAvailable()
lua_thread.create(script_update.main)
config.init()
while true do
wait(0)
local car = storeCarCharIsInNoSave(playerPed)
@ -57,6 +58,81 @@ function check_press_key(table)
return result, result_key_text
end
-->> CONFIG
config = {}
config.data = {}
config.default = {
status = true,
key = "VK_LSHIFT"
}
config.directory = string.format("%s\\moonloader\\config", getGameDirectory())
config.init = function()
if not doesDirectoryExist("moonloader\\config") then
createDirectory("moonloader\\config")
end
if not doesDirectoryExist(config.directory) then
createDirectory(config.directory)
end
config.address = string.format("%s\\%s.json", config.directory, thisScript().name)
if not doesFileExist(config.address) then
config.save(config.default)
end
config.read()
for k,v in pairs(config.default) do
if config.data[k] == nil then
config.data[k] = v
end
if type(v) == "table" then
for kk,vv in pairs(v) do
if config.data[k][kk] == nil then
config.data[k][kk] = vv
end
if type(vv) ~= type(config.data[k][kk]) then
config.data[k][kk] = vv
end
end
end
end
config.save(config.data)
end
config.save = function(data)
local file, error = io.open(config.address, "w")
if file == nil then
addChatMessage(error)
end
file:write(encodeJson(data))
file:flush()
io.close(file)
end
config.read = function()
local readJson = function()
local file, error = io.open(config.address, "r")
if file then
config.data = decodeJson(file:read("*a"))
io.close(file)
if config.data == nil then
addChatMessage("Ошибка чтения конфига! Сбрасываю конфиг!")
config.save(config.default)
end
end
end
local result = pcall(readJson)
if not result then
addChatMessage("Ошибка чтения конфига! Сбрасываю конфиг!")
config.save(config.default)
end
if config.data == nil then
config.error = true
addChatMessage("Ошибка чтения конфига! Пробую ещё раз прочесть")
config.read()
else
if config.error then
addChatMessage("Конфиг был успешно загружен!")
config.error = false
end
end
end
-->> UPDATE MODULE
function openURL(url, fpath)
local text = ""

Loading…
Cancel
Save