forked from DarkEnergyProcessor/livesim2_async
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_open_wide.lua
80 lines (65 loc) · 1.73 KB
/
io_open_wide.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-- io.open patch to allow UTF-8 filenames in Windows
-- Copyright (c) 2020 Miku AuahDark
-- You can use portion of this code or as a whole without my permission.
local ffi = require("ffi")
if ffi.os ~= "Windows" then
-- Not Windows
return
end
-- Safety measures from redefining
do
local function index(t, k)
return t[k]
end
if pcall(index, ffi.C, "GetACP") then
-- Has been patched!
return
end
end
ffi.cdef[[
uint32_t __stdcall GetACP();
]]
if ffi.C.GetACP() == 65001 then
-- "Use Unicode UTF-8 for worldwide language support" is ticked.
-- Don't bother patching it.
return
end
ffi.cdef[[
int32_t __stdcall MultiByteToWideChar(
uint32_t CodePage,
uint32_t dwFlags,
const char *lpMultiByteStr,
int32_t cbMultiByte,
wchar_t *lpWideCharStr,
int32_t cchWideChar
);
void *_wfreopen(wchar_t *path, wchar_t *mode, void *file);
]]
local MB_ERR_INVALID_CHARS = 0x8
local function toWideChar(ch)
local size = ffi.C.MultiByteToWideChar(65001, MB_ERR_INVALID_CHARS, ch, #ch, nil, 0)
if size == 0 then
error("invalid character sequence")
end
local buf = ffi.new("wchar_t[?]", size + 1)
if ffi.C.MultiByteToWideChar(65001, MB_ERR_INVALID_CHARS, ch, #ch, buf, size) == 0 then
error("char conversion error")
end
return buf
end
local open = io.open
---@param filename string
---@param mode? openmode
---@return file*?
---@return string? errmsg
---@nodiscard
function io.open(filename, mode)
local pathw = toWideChar(filename)
local modew = toWideChar(mode)
local file = assert(open("nul", "rb"))
if ffi.C._wfreopen(pathw, modew, file) == nil then
local msg, errno = select(2, file:close())
return nil, filename..": "..msg, errno
end
return file
end