diff --git a/TruckHUD.lua b/TruckHUD.lua index d7cd333..492a368 100644 --- a/TruckHUD.lua +++ b/TruckHUD.lua @@ -7,9 +7,9 @@ local dlstatus = require("moonloader").download_status local vkeys = require "lib.vkeys" local ffi = require("ffi") -local encoding = require "encoding" -encoding.default = "CP1251" -local u8 = encoding.UTF8 +-- local encoding = require "encoding" +-- encoding.default = "CP1251" +-- local u8 = encoding.UTF8 ffi.cdef [[ bool SetCursorPos(int X, int Y); ]] @@ -2786,11 +2786,11 @@ function loadEvents() end end, Cid) end - utf8_function({ - { "sampev", "onShowDialog" }, - { "sampev", "onServerMessage" }, - { "sampev", "onCreate3DText" } - }, AnsiToUtf8, AnsiToUtf8) + -- utf8_function({ + -- { "sampev", "onShowDialog" }, + -- { "sampev", "onServerMessage" }, + -- { "sampev", "onCreate3DText" } + -- }, "AnsiToUtf8", "AnsiToUtf8") end function isTruck3dTextDefined(id) local result = false @@ -4574,44 +4574,102 @@ script_update = { end } + -->> SCRIPT UTF-8 -function utf8_function(utf8_func, in_encoding, out_encoding) - for i = 1, #utf8_func do - if _G[utf8_func[i][1]] ~= nil then - utf8_function_create = function(...) - local pack = table.pack(...) - for i = 1, pack.n do - if type(pack[i]) == "string" then - pack[i] = in_encoding(pack[i]) - end - end - local result = {utf8_func[i][1000](table.unpack(pack))} - for i = 1, #result do - if type(result[i]) == "string" then - result[i] = out_encoding(result[i]) - end - end - return table.unpack(result) - end - local text = "_G" - for s = 1, #utf8_func[i] do - text = string.format('%s["%s"]', text, utf8_func[i][s]) - end - text = string.format('%s = _G["utf8_function_create"];', text) - utf8_func[i][1000] = _G[utf8_func[i][1]] - load(text)() - _G["utf8_function_create"] = nil - end - end +utf8 = load([=[ + return { + module = function(utf8_func, in_encoding, out_encoding); + + + + if _G["encoding"] == nil then; + _G["encoding"] = require("encoding"); + _G["encoding"]["default"] = "CP1251"; + _G["u8"] = _G["encoding"]["UTF8"] + end + + if _G["AnsiToUtf8"] == nil or _G["Utf8ToAnsi"] == nil then + _G["AnsiToUtf8"] = function(text) + return _G["u8"](text) + end + _G["Utf8ToAnsi"] = function(text) + return _G["u8"]:decode(text) + end + end + + for i = 1, #utf8_func do; + if _G[utf8_func[i][1]] ~= nil then; + utf8_function_create = function(...); + local pack = table.pack(...); + for i = 1, pack.n do; + if type(pack[i]) == "string" then; + pack[i] = _G[in_encoding](pack[i]); + end; + end; + local result = {utf8_func[i][1000](table.unpack(pack))}; + for i = 1, #result do; + if type(result[i]) == "string" then; + result[i] = _G[out_encoding](result[i]); + end; + end; + return table.unpack(result); + end; + local text = "_G"; + for s = 1, #utf8_func[i] do; + text = string.format('%s["%s"]', text, utf8_func[i][s]); end; + text = string.format('%s = _G["utf8_function_create"];', text); + utf8_func[i][1000] = _G[utf8_func[i][1]]; + load(text)(); + _G["utf8_function_create"] = nil; + end; + end; + end + }]=]) + +function utf8_function(...) --> utf8_function(table with function names, encoding of transmitted param, encoding of received param) + pcall(utf8().module, table.unpack(table.pack(...))) end + + + +-- function utf8_function(utf8_func, in_encoding, out_encoding) +-- for i = 1, #utf8_func do +-- if _G[utf8_func[i][1]] ~= nil then +-- utf8_function_create = function(...) +-- local pack = table.pack(...) +-- for i = 1, pack.n do +-- if type(pack[i]) == "string" then +-- pack[i] = in_encoding(pack[i]) +-- end +-- end +-- local result = {utf8_func[i][1000](table.unpack(pack))} +-- for i = 1, #result do +-- if type(result[i]) == "string" then +-- result[i] = out_encoding(result[i]) +-- end +-- end +-- return table.unpack(result) +-- end +-- local text = "_G" +-- for s = 1, #utf8_func[i] do +-- text = string.format('%s["%s"]', text, utf8_func[i][s]) +-- end +-- text = string.format('%s = _G["utf8_function_create"];', text) +-- utf8_func[i][1000] = _G[utf8_func[i][1]] +-- load(text)() +-- _G["utf8_function_create"] = nil +-- end +-- end +-- end + --- decode = Utf8ToAnsi / encode = AnsiToUtf8 -function AnsiToUtf8(text) - return u8(text) -end -function Utf8ToAnsi(text) --> ANSI to UTF-8 - return u8:decode(text) -end +-- function AnsiToUtf8(text) +-- return u8(text) +-- end +-- function Utf8ToAnsi(text) --> ANSI to UTF-8 +-- return u8:decode(text) +-- end utf8_function({ { "sampShowDialog" }, @@ -4624,8 +4682,8 @@ utf8_function({ { "sampCreate3dTextEx" }, { "sampSet3dTextString" }, { "sampCreate3dText" } -}, Utf8ToAnsi, AnsiToUtf8) +}, "Utf8ToAnsi", "AnsiToUtf8") utf8_function({ { "sampGetDialogCaption" }, { "sampHasDialogRespond" } -}, AnsiToUtf8, AnsiToUtf8) \ No newline at end of file +}, "AnsiToUtf8", "AnsiToUtf8") \ No newline at end of file