utf-8 module rewritten to loadstring

test-utf-8
rubin 2 years ago
parent 38e63b9ece
commit b51fd958d0
  1. 146
      TruckHUD.lua

@ -7,9 +7,9 @@ local dlstatus = require("moonloader").download_status
local vkeys = require "lib.vkeys" local vkeys = require "lib.vkeys"
local ffi = require("ffi") local ffi = require("ffi")
local encoding = require "encoding" -- local encoding = require "encoding"
encoding.default = "CP1251" -- encoding.default = "CP1251"
local u8 = encoding.UTF8 -- local u8 = encoding.UTF8
ffi.cdef [[ bool SetCursorPos(int X, int Y); ]] ffi.cdef [[ bool SetCursorPos(int X, int Y); ]]
@ -2786,11 +2786,11 @@ function loadEvents()
end end
end, Cid) end, Cid)
end end
utf8_function({ -- utf8_function({
{ "sampev", "onShowDialog" }, -- { "sampev", "onShowDialog" },
{ "sampev", "onServerMessage" }, -- { "sampev", "onServerMessage" },
{ "sampev", "onCreate3DText" } -- { "sampev", "onCreate3DText" }
}, AnsiToUtf8, AnsiToUtf8) -- }, "AnsiToUtf8", "AnsiToUtf8")
end end
function isTruck3dTextDefined(id) function isTruck3dTextDefined(id)
local result = false local result = false
@ -4574,44 +4574,102 @@ script_update = {
end end
} }
-->> SCRIPT UTF-8 -->> SCRIPT UTF-8
function utf8_function(utf8_func, in_encoding, out_encoding) utf8 = load([=[
for i = 1, #utf8_func do return {
if _G[utf8_func[i][1]] ~= nil then module = function(utf8_func, in_encoding, out_encoding);
utf8_function_create = function(...)
local pack = table.pack(...)
for i = 1, pack.n do
if type(pack[i]) == "string" then if _G["encoding"] == nil then;
pack[i] = in_encoding(pack[i]) _G["encoding"] = require("encoding");
end _G["encoding"]["default"] = "CP1251";
end _G["u8"] = _G["encoding"]["UTF8"]
local result = {utf8_func[i][1000](table.unpack(pack))} end
for i = 1, #result do
if type(result[i]) == "string" then if _G["AnsiToUtf8"] == nil or _G["Utf8ToAnsi"] == nil then
result[i] = out_encoding(result[i]) _G["AnsiToUtf8"] = function(text)
end return _G["u8"](text)
end end
return table.unpack(result) _G["Utf8ToAnsi"] = function(text)
end return _G["u8"]:decode(text)
local text = "_G" end
for s = 1, #utf8_func[i] do end
text = string.format('%s["%s"]', text, utf8_func[i][s])
end for i = 1, #utf8_func do;
text = string.format('%s = _G["utf8_function_create"];', text) if _G[utf8_func[i][1]] ~= nil then;
utf8_func[i][1000] = _G[utf8_func[i][1]] utf8_function_create = function(...);
load(text)() local pack = table.pack(...);
_G["utf8_function_create"] = nil for i = 1, pack.n do;
end if type(pack[i]) == "string" then;
end 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 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 --- decode = Utf8ToAnsi / encode = AnsiToUtf8
function AnsiToUtf8(text) -- function AnsiToUtf8(text)
return u8(text) -- return u8(text)
end -- end
function Utf8ToAnsi(text) --> ANSI to UTF-8 -- function Utf8ToAnsi(text) --> ANSI to UTF-8
return u8:decode(text) -- return u8:decode(text)
end -- end
utf8_function({ utf8_function({
{ "sampShowDialog" }, { "sampShowDialog" },
@ -4624,8 +4682,8 @@ utf8_function({
{ "sampCreate3dTextEx" }, { "sampCreate3dTextEx" },
{ "sampSet3dTextString" }, { "sampSet3dTextString" },
{ "sampCreate3dText" } { "sampCreate3dText" }
}, Utf8ToAnsi, AnsiToUtf8) }, "Utf8ToAnsi", "AnsiToUtf8")
utf8_function({ utf8_function({
{ "sampGetDialogCaption" }, { "sampGetDialogCaption" },
{ "sampHasDialogRespond" } { "sampHasDialogRespond" }
}, AnsiToUtf8, AnsiToUtf8) }, "AnsiToUtf8", "AnsiToUtf8")
Loading…
Cancel
Save