From 827db5443087d7647c886530eea82e9a39164f6c Mon Sep 17 00:00:00 2001 From: maj00r <73280030+maj00r@users.noreply.github.com> Date: Tue, 24 Nov 2026 19:28:54 +0100 Subject: [PATCH] fix(python): the files a screen opens itself are found on Linux when named in another case (#449) #362 made the simulator find the data named in another case of its letters, through FileOnDisk(), the script of a screen included. What the script opens itself goes past it: Image.open(), ImageFont.truetype(), open() and the openimage() of the screens go straight to the system. The data is made on Windows, so on Linux a screen that asks for "WS_on.png" where the disk has "ws_on.png" doesn't start at all, e.g. the left screens of the EN57AL/AKM (WS_gotowosc.png), EN57AKL and EN71AKS (WN_off.png, M1M2_on.png and more). abstractscreenrenderer.py, run in __main__ before any screen, now finds those files as FileCased() does: the name given when it's on the disk, otherwise the one differing from it only in case, of the file or of its folders, a folder listed again for a name missing at most once a second. builtins.open() (which Image.open() goes through) and ImageFont.truetype() take the name so found, and openimage() looks for it. A file not there yet goes into its folder as named on the disk, so a screen writing its state for the first time doesn't make a second folder. A name with no such file is passed on as given, so a file missing fails as before. On Windows, which finds the names in any case itself, nothing is wrapped and nothing changes. No change to the game data is needed. Checked on a case-sensitive folder (a Windows folder with the case-sensitive attribute) with the scripts as they are in the data: with the file of master the EN57AL, EN57AKL and EN71AKS screens fail to start, with this one they start and render; plus the cases of the lookup (folders, ./, .., backslashes, a new file, a missing one, the module run twice, a screen's global named like the module's own). Co-authored-by: Claude Opus 5.5 --- .../python/local/abstractscreenrenderer.py | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/staticFiles/python/local/abstractscreenrenderer.py b/staticFiles/python/local/abstractscreenrenderer.py index 3ebf20813..59341c045 100644 --- a/staticFiles/python/local/abstractscreenrenderer.py +++ b/staticFiles/python/local/abstractscreenrenderer.py @@ -1,6 +1,73 @@ from PIL import Image, ImageDraw, ImageFont from math import ceil, floor, cos, sin, radians +import builtins import os +import sys +import time + +# Linux tells the case of the letters in a file name apart, Windows doesn't, and the data is made on Windows: a screen asks for +# "WS_on.png" where the disk has "ws_on.png", or names a folder in another case. The simulator opens such files of its own through +# FileOnDisk(); the files a screen opens itself (images, fonts, text) are found here the same way. The name given when it's on the +# disk, otherwise the one differing from it only in case, of the file or of its folders. On Windows nothing changes. +_case_sensitive = not sys.platform.startswith('win') +_cased_listings = {} # folder -> (when listed, {name in lower case: name on the disk}) + +def _cased_name(folder, name): + """the name in the folder differing from the one given only in case, as on the disk; None when there's none""" + listed = _cased_listings.get(folder) + # a name missing gets the folder listed again, at most once a second, as FileCased() does + if listed is None or (name.lower() not in listed[1] and time.monotonic() - listed[0] >= 1.0): + names = {} + try: + for entry in os.listdir(folder or '.'): + names.setdefault(entry.lower(), entry) + except OSError: + pass + listed = (time.monotonic(), names) + _cased_listings[folder] = listed + return listed[1].get(name.lower()) + +def file_on_disk(path): + """the file to open: as it's named on the disk, the path given when there's none (and on Windows, which finds it itself)""" + if not _case_sensitive or not isinstance(path, str) or os.path.exists(path): + return path + text = path.replace('\\', '/') + parts = [part for part in text.split('/') if part] + cased = '/' if text.startswith('/') else '' + for index, part in enumerate(parts): + last = index == len(parts) - 1 + found = part + if part not in ('.', '..') and not os.path.exists(cased + part): + found = _cased_name(cased, part) + if found is None: + if not last: + return path + # a file not there yet goes into its folder as named on the disk: one written for the first time. The folder + # gets listed anew next time, the file being there by then + _cased_listings.pop(cased, None) + return cased + part + cased += found if last else found + '/' + return cased + +def _open_files_on_disk(): + # Image.open() opens the file through builtins.open(); the fonts are opened by FreeType, from the name. What the wrappers + # call is held here, not looked up in __main__, which the screens share and could name the same + on_disk = file_on_disk + open_given = builtins.open + truetype_given = ImageFont.truetype + + def open_on_disk(file, *args, **kwargs): + return open_given(on_disk(file), *args, **kwargs) + + def truetype_on_disk(font=None, *args, **kwargs): + return truetype_given(on_disk(font), *args, **kwargs) + + open_on_disk.file_on_disk = True + builtins.open = open_on_disk + ImageFont.truetype = truetype_on_disk + +if _case_sensitive and not getattr(builtins.open, 'file_on_disk', False): + _open_files_on_disk() # Pillow 10 removed ImageFont.getsize(), ImageDraw.textsize() and Image.ANTIALIAS, which the screen scripts use; for # Python 3.14 only a newer Pillow is available, so they are put back, on top of getbbox() / textbbox() and LANCZOS @@ -30,7 +97,7 @@ class abstractscreenrenderer(): def openimage(self, name): exts = [ ".png", ".tga", ".dds" ] for e in exts: - path = name + e + path = file_on_disk(name + e) if os.path.isfile(path): if e == ".dds": return Image.open(path).transpose(Image.FLIP_TOP_BOTTOM)