Fix launcher crash on non-ASCII picked filenames (#325)

The Windows pickers shell out to PowerShell, which writes the chosen
path in the console's OEM codepage (CP437 on en-US): a file named
"Pokémon ...zip" came back as Pok\x82mon.  The import then failed and
the error notice carrying those bytes hard-crashed the mods panel's
UTF-8-validating text draw.

- All three Windows picker scripts (ROM, mod, save) now force
  [Console]::OutputEncoding to UTF-8, so returned paths and any notice
  built from them are valid.
- The mod picker also copies the pick to a plain-ASCII temp name and
  returns that, so a non-ASCII filename actually imports instead of
  failing the io.open (Windows io.open needs ANSI bytes).
This commit is contained in:
johnjohto
2026-07-27 22:08:57 -04:00
parent 10acfe0710
commit 9ec3ff26d8
2 changed files with 38 additions and 3 deletions
+16 -3
View File
@@ -338,7 +338,10 @@ local function chooseRom(promptName)
"$d=New-Object System.Windows.Forms.OpenFileDialog;", "$d=New-Object System.Windows.Forms.OpenFileDialog;",
"$d.Title='" .. prompt .. "';", "$d.Title='" .. prompt .. "';",
"$d.Filter='Game Boy ROM (*.gb)|*.gb|All files (*.*)|*.*';", "$d.Filter='Game Boy ROM (*.gb)|*.gb|All files (*.*)|*.*';",
"if($d.ShowDialog() -eq 'OK'){[Console]::Write($d.FileName)}", -- write the pick as UTF-8: the console's OEM codepage would mangle
-- non-ASCII names (Pokémon -> Pok\x82mon) and crash any text draw
-- that shows them (#325)
"if($d.ShowDialog() -eq 'OK'){[Console]::OutputEncoding=[Text.Encoding]::UTF8; [Console]::Write($d.FileName)}",
}) })
return commandOutput( return commandOutput(
'powershell -NoProfile -STA -Command "' .. script .. '"') 'powershell -NoProfile -STA -Command "' .. script .. '"')
@@ -369,7 +372,16 @@ local function chooseZip()
"$d=New-Object System.Windows.Forms.OpenFileDialog;", "$d=New-Object System.Windows.Forms.OpenFileDialog;",
"$d.Title='" .. prompt .. "';", "$d.Title='" .. prompt .. "';",
"$d.Filter='Mod archive (*.zip)|*.zip|All files (*.*)|*.*';", "$d.Filter='Mod archive (*.zip)|*.zip|All files (*.*)|*.*';",
"if($d.ShowDialog() -eq 'OK'){[Console]::Write($d.FileName)}", -- copy the pick to a plain-ASCII temp name and answer with that:
-- the console's OEM codepage would mangle a non-ASCII path
-- (Pokémon -> Pok\x82mon) and io.open on Windows needs ANSI bytes,
-- so returning the original name both crashed the notice draw and
-- could never have opened the file (#325)
"if($d.ShowDialog() -eq 'OK'){",
"$t=Join-Path $env:TEMP 'pokeport_mod_pick.zip';",
"Copy-Item -LiteralPath $d.FileName -Destination $t -Force;",
"[Console]::OutputEncoding=[Text.Encoding]::UTF8;",
"[Console]::Write($t)}",
}) })
return commandOutput( return commandOutput(
'powershell -NoProfile -STA -Command "' .. script .. '"') 'powershell -NoProfile -STA -Command "' .. script .. '"')
@@ -400,7 +412,8 @@ local function chooseSav()
"$d=New-Object System.Windows.Forms.OpenFileDialog;", "$d=New-Object System.Windows.Forms.OpenFileDialog;",
"$d.Title='" .. prompt .. "';", "$d.Title='" .. prompt .. "';",
"$d.Filter='Game Boy save (*.sav)|*.sav|All files (*.*)|*.*';", "$d.Filter='Game Boy save (*.sav)|*.sav|All files (*.*)|*.*';",
"if($d.ShowDialog() -eq 'OK'){[Console]::Write($d.FileName)}", -- UTF-8, like the ROM and mod pickers (#325)
"if($d.ShowDialog() -eq 'OK'){[Console]::OutputEncoding=[Text.Encoding]::UTF8; [Console]::Write($d.FileName)}",
}) })
return commandOutput( return commandOutput(
'powershell -NoProfile -STA -Command "' .. script .. '"') 'powershell -NoProfile -STA -Command "' .. script .. '"')
+22
View File
@@ -208,4 +208,26 @@ do
check(err ~= nil, "missing-mod uninstall carries a reason") check(err ~= nil, "missing-mod uninstall carries a reason")
end end
-- ------- issue #325: the Windows pickers must not hand back mangled paths
do
-- PowerShell writes the pick in the console's OEM codepage by default
-- (Pokémon -> Pok\x82mon), which broke the open AND crashed the mods
-- panel's UTF-8-validating text draw. Every Windows picker script must
-- force UTF-8 output, and the mod picker must return an ASCII temp copy
-- since io.open on Windows needs ANSI bytes to open the file at all.
local f = assert(io.open("src/import/RomImporter.lua", "rb"))
local src = f:read("*a")
f:close()
local utf8, copies = 0, 0
for _ in src:gmatch("OutputEncoding=%[Text%.Encoding%]::UTF8") do
utf8 = utf8 + 1
end
check(utf8 >= 3, "all three Windows pickers force UTF-8 output")
check(src:find("pokeport_mod_pick.zip", 1, true) ~= nil,
"the mod picker copies the pick to an ASCII temp name")
check(src:find("Copy%-Item %-LiteralPath") ~= nil,
"the copy uses the literal picked path")
end
T.finish("launcher_mods") T.finish("launcher_mods")