diff --git a/launcher/gamefiles.py b/launcher/gamefiles.py index bf3998a0..f37568aa 100644 --- a/launcher/gamefiles.py +++ b/launcher/gamefiles.py @@ -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 diff --git a/launcher/ui/app.py b/launcher/ui/app.py index 6b73ce8e..130effa1 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -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}') diff --git a/launcher/winsys.py b/launcher/winsys.py index 04789f62..e0cd91e5 100644 --- a/launcher/winsys.py +++ b/launcher/winsys.py @@ -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': diff --git a/tests/launcher_test.py b/tests/launcher_test.py index a839f02a..92837ed8 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -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