mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 04:59:57 -04:00
Let the launcher replace a cached font file it has registered.
A font file registered with Windows stays open, so writing it failed with Permission denied. When the cached title font was registered but its family could not be used, every start tried to decode it again, failed on the locked file and kept the system font. The registration is now given up before the fonts are decoded again, and an unchanged file is not rewritten. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -326,7 +326,9 @@ def extract_fonts(layout, game_dir='', dump=''):
|
||||
fam = font_family(otf) if otf else None
|
||||
if not otf or not fam:
|
||||
continue
|
||||
(d / f'{role}.otf').write_bytes(otf)
|
||||
p = d / f'{role}.otf'
|
||||
if not (p.is_file() and p.read_bytes() == otf): # a file this process registered as a font stays as it is
|
||||
p.write_bytes(otf)
|
||||
(d / f'{role}.family').write_text(fam, encoding='utf-8')
|
||||
out[role] = (d / f'{role}.otf', fam)
|
||||
return out
|
||||
|
||||
@@ -315,6 +315,11 @@ class App:
|
||||
got = heard = {}
|
||||
try:
|
||||
if fonts:
|
||||
# The cached files are registered (load_fonts) and were not usable, or this would not run:
|
||||
# registered, Windows keeps them open, and decoding them again failed with "Permission denied"
|
||||
# at every start, so a cache file that had gone bad was never replaced.
|
||||
for path, _family in gamefiles.cached_fonts(self.layout).values():
|
||||
winsys.unregister_font(path)
|
||||
got = gamefiles.extract_fonts(self.layout, game_dir, dump)
|
||||
except Exception as e:
|
||||
self.ctx.log(f'fonts: {e}')
|
||||
|
||||
@@ -141,6 +141,17 @@ def register_font(path):
|
||||
return False
|
||||
|
||||
|
||||
def unregister_font(path):
|
||||
'''Give up a font file register_font added, so the file can be written again: Windows keeps a registered font
|
||||
file open, and writing it then fails with "Permission denied".'''
|
||||
if os.name != 'nt':
|
||||
return
|
||||
try:
|
||||
ctypes.windll.gdi32.RemoveFontResourceExW(ctypes.c_wchar_p(str(path)), 0x10, None) # FR_PRIVATE
|
||||
except (AttributeError, OSError):
|
||||
pass
|
||||
|
||||
|
||||
def open_path(path):
|
||||
path = str(path)
|
||||
if os.name == 'nt':
|
||||
|
||||
@@ -1019,6 +1019,14 @@ class FontTest(Tmp):
|
||||
self.assertEqual(got['title'][1], 'DejaVu Sans')
|
||||
self.assertEqual(Path(got['title'][0]).read_bytes(), otf)
|
||||
self.assertEqual(gamefiles.cached_fonts(self.layout)['title'][1], 'DejaVu Sans')
|
||||
# Registered as a font, Windows keeps the file open: decoding it again must not fail on the unchanged file,
|
||||
# and once the registration is given up a file that went bad is replaced.
|
||||
path = Path(got['title'][0])
|
||||
winsys.register_font(path)
|
||||
self.assertEqual(gamefiles.extract_fonts(self.layout, self.tmp / 'game')['title'][1], 'DejaVu Sans')
|
||||
winsys.unregister_font(path)
|
||||
path.write_bytes(b'not a font')
|
||||
self.assertEqual(Path(gamefiles.extract_fonts(self.layout, self.tmp / 'game')['title'][0]).read_bytes(), otf)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- the button art of a release
|
||||
|
||||
Reference in New Issue
Block a user