Skip to content

Commit b8a2a70

Browse files
zaoWires77
andauthored
Fix fallout from SimpleGraphic upgrade with wider Unicode support (#372)
* fix: enable Unicode separators and caret motions As the runtime is going to support Unicode installation locations and build directories, some UTF-8 text is going to reach the Lua side of the project. This includes the script path, the user path, any paths yielded from file searches and also imported character names from accounts. Care needs to be taken in many places where string operations are performed as no longer does a byte necessarily correspond to a single character and anything that truncates, reverses or otherwise slices strings could need an audit. This change fixes cursor movement in `EditControl`s with the arrow keys as those historically used string matching and byte offsets. It also ensures that the use of arbitrary Unicode codepoints as decimal and thousands separators works correctly as the previous code used unaware reversing and slicing. * fix: turn update paths relative for wide installs The updater is a fixed piece of older code that uses a Lua runtime that only handles paths that are representable in the user's text codepage. As the software may be installed in a location that cannot be expressed in that way, to mitigate the problem we turn all the paths in the update op-files into relative paths. That way as long as we never use exotic codepoints in our own paths it should be able to apply them cleanly and restart Path of Building afterward with a relative path. The updater executable can ironically enough not be updated at all with the related type of runtime hacks we introduced in SimpleGraphic as the updater deadlocks in updating itself. We have to work around its shortcomings in how we produce the op-files and possibly the update application script that runs under that limited runtime. * Add luautf8 to Dockerfile --------- Co-authored-by: Wires77 <Wires77@users.noreply.github.com>
1 parent df3773d commit b8a2a70

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ RUN --mount=type=cache,from=luarocks,source=/opt,target=/opt make -C /opt/luaroc
3131
# Install here to install lua rocks pkgs in pararell with compilation of emmylua and luajit
3232
RUN luarocks install busted 2.2.0-1;\
3333
luarocks install cluacov 0.1.2-1;\
34-
luarocks install luacov-coveralls 0.2.3-1
34+
luarocks install luacov-coveralls 0.2.3-1;\
35+
luarocks install luautf8 0.1.6-1
3536

3637
RUN --mount=type=cache,from=emmyluadebugger,source=/opt,target=/opt make -C /opt/EmmyLuaDebugger/build/ install
3738
RUN --mount=type=cache,from=luajit,source=/opt,target=/opt make -C /opt/LuaJIT/ install

src/Classes/EditControl.lua

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local m_max = math.max
77
local m_min = math.min
88
local m_floor = math.floor
99
local protected_replace = "*"
10+
local utf8 = require('lua-utf8')
1011

1112
local function lastLine(str)
1213
local lastLineIndex = 1
@@ -541,18 +542,10 @@ function EditClass:OnKeyDown(key, doubleClick)
541542
if self.caret > 1 then
542543
if ctrl then
543544
-- Skip leading space, then jump word
544-
while self.buf:sub(self.caret-1, self.caret-1):match("[%s%p]") do
545-
if self.caret > 1 then
546-
self.caret = self.caret - 1
547-
end
548-
end
549-
while self.buf:sub(self.caret-1, self.caret-1):match("%w") do
550-
if self.caret > 1 then
551-
self.caret = self.caret - 1
552-
end
553-
end
545+
self.caret = self.caret - #utf8.match(self.buf:sub(1, self.caret-1), "[%s%p]*$")
546+
self.caret = self.caret - #utf8.match(self.buf:sub(1, self.caret-1), "%w*$")
554547
else
555-
self.caret = self.caret - 1
548+
self.caret = utf8.next(self.buf, self.caret, -1) or 0
556549
end
557550
self.lastUndoState.caret = self.caret
558551
self:ScrollCaretIntoView()
@@ -562,19 +555,11 @@ function EditClass:OnKeyDown(key, doubleClick)
562555
self.sel = shift and (self.sel or self.caret) or nil
563556
if self.caret <= #self.buf then
564557
if ctrl then
565-
-- Jump word, then skip trailing space,
566-
while self.buf:sub(self.caret, self.caret):match("%w") do
567-
if self.caret <= #self.buf then
568-
self.caret = self.caret + 1
569-
end
570-
end
571-
while self.buf:sub(self.caret, self.caret):match("[%s%p]") do
572-
if self.caret <= #self.buf then
573-
self.caret = self.caret + 1
574-
end
575-
end
558+
-- Jump word, then skip trailing space,
559+
self.caret = self.caret + #utf8.match(self.buf:sub(self.caret), "^%w*")
560+
self.caret = self.caret + #utf8.match(self.buf:sub(self.caret), "^[%s%p]*")
576561
else
577-
self.caret = self.caret + 1
562+
self.caret = utf8.next(self.buf, self.caret, 1) or #self.buf + 1
578563
end
579564
self.lastUndoState.caret = self.caret
580565
self:ScrollCaretIntoView()

src/Modules/Common.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ common.curl = require("lcurl.safe")
2626
common.xml = require("xml")
2727
common.base64 = require("base64")
2828
common.sha1 = require("sha1")
29+
local utf8 = require('lua-utf8')
2930

3031
-- Try to load a library return nil if failed. https://stackoverflow.com/questions/34965863/lua-require-fallback-error-handling
3132
function prerequire(...)
@@ -723,20 +724,21 @@ function formatNumSep(str)
723724
end
724725
local x, y, minus, integer, fraction = str:find("(-?)(%d+)(%.?%d*)")
725726
if main.showThousandsSeparators then
726-
integer = integer:reverse():gsub("(%d%d%d)", "%1"..main.thousandsSeparator):reverse()
727+
rev1kSep = utf8.reverse(main.thousandsSeparator)
728+
integer = utf8.reverse(utf8.gsub(utf8.reverse(integer), "(%d%d%d)", "%1"..rev1kSep))
727729
-- There will be leading separators if the number of digits are divisible by 3
728730
-- This checks for their presence and removes them
729731
-- Don't use patterns here because thousandsSeparator can be a pattern control character, and will crash if used
730732
if main.thousandsSeparator ~= "" then
731-
local thousandsSeparator = string.find(integer, main.thousandsSeparator, 1, 2)
733+
local thousandsSeparator = utf8.find(integer, rev1kSep, 1, 2)
732734
if thousandsSeparator and thousandsSeparator == 1 then
733-
integer = integer:sub(2)
735+
integer = utf8.sub(integer, 2)
734736
end
735737
end
736738
else
737-
integer = integer:reverse():gsub("(%d%d%d)", "%1"):reverse()
739+
integer = utf8.reverse(utf8.gsub(utf8.reverse(integer), "(%d%d%d)", "%1"))
738740
end
739-
return colour..minus..integer..fraction:gsub("%.", main.decimalSeparator)
741+
return colour..minus..integer..utf8.gsub(fraction, "%.", main.decimalSeparator)
740742
end)
741743
end
742744

src/UpdateCheck.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ end
7878

7979
ConPrintf("Checking for update...")
8080

81-
local scriptPath = GetScriptPath()
82-
local runtimePath = GetRuntimePath()
81+
local scriptPath = "."
82+
local runtimePath = "."
8383

8484
-- Load and process local manifest
8585
local localVer

0 commit comments

Comments
 (0)