From 607a1572dce030216ccaf31998d70997f966a510 Mon Sep 17 00:00:00 2001 From: rubin Date: Mon, 9 Jan 2023 20:44:11 +0300 Subject: [PATCH] fix restore config & fix default values binder --- TruckHUD.lua | 72 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/TruckHUD.lua b/TruckHUD.lua index 7b78e44..51eaa54 100644 --- a/TruckHUD.lua +++ b/TruckHUD.lua @@ -244,6 +244,7 @@ function main() end function settings_load() + wait(0) local x1, y1 = convertGameScreenCoordsToWindowScreenCoords(14.992679595947, 274.75) local x2, y2 = convertGameScreenCoordsToWindowScreenCoords(146.17861938477, 345.91665649414) local x3, y3 = convertGameScreenCoordsToWindowScreenCoords(529.42901611328, 158.08332824707) @@ -333,24 +334,51 @@ function settings_load() end end - try( - function() - local file, error = io.open(AdressJson, "r") - if file then - inifiles = decodeJson(file:read("*a")) - io.close(file) - end - end, - function(e) - sampAddChatMessage("[TruckHUD] Ошибка чтения конфига! Сбрасываю конфиг!", 0xff0000) - local file, error = io.open(AdressJson, "w") - if file ~= nil then - file:write(encodeJson(table_std)) - file:flush() - io.close(file) + -- try( + -- function() + -- local file, error = io.open(AdressJson, "r") + -- if file then + -- inifiles = decodeJson(file:read("*a")) + -- io.close(file) + -- end + -- end, + -- function(e) + -- sampAddChatMessage("[TruckHUD] Ошибка чтения конфига! Сбрасываю конфиг!", 0xff0000) + -- local file, error = io.open(AdressJson, "w") + -- if file ~= nil then + -- file:write(encodeJson(table_std)) + -- file:flush() + -- io.close(file) + -- end + -- end + -- ) + + local readJson = function() + local file, error = io.open(AdressJson, "r") + if file then + inifiles = decodeJson(file:read("*a")) + if inifiles == nil then + sampAddChatMessage("[TruckHUD] Ошибка чтения конфига! Сбрасываю конфиг!", 0xff0000) + local file, error = io.open(AdressJson, "w") + if file ~= nil then + file:write(encodeJson(table_std)) + file:flush() + io.close(file) + end end + io.close(file) end - ) + end + local result = pcall(readJson) + if not result then + sampAddChatMessage("[TruckHUD] Ошибка чтения конфига! Сбрасываю конфиг!", 0xff0000) + local file, error = io.open(AdressJson, "w") + if file ~= nil then + file:write(encodeJson(table_std)) + file:flush() + io.close(file) + end + end if inifiles ~= nil then @@ -362,9 +390,11 @@ function settings_load() if inifiles[k] == nil then inifiles[k] = v end - for i, s in pairs(v) do - if inifiles[k][i] == nil then - inifiles[k][i] = s + if k ~= "binder" and k ~= "binder_sms" then + for i, s in pairs(v) do + if inifiles[k][i] == nil then + inifiles[k][i] = s + end end end end @@ -1479,9 +1509,9 @@ function doRenderBind() end if drawClickableText("{ff0000}х", (X + renderGetFontDrawTextLength(font, string .. " ")), Y) then if not binder_mode_sms then - inifiles.binder[k] = nil + table.remove(inifiles.binder, k) else - inifiles.binder_sms[k] = nil + table.remove(inifiles.binder_sms, k) end settings_save() end