@@ -23,6 +23,7 @@ def games(self):
2323 Return a list of games in this library.
2424 """
2525 games = []
26+ image_dir = LibraryImageDir (Path (self .steam .path ).joinpath ('appcache' , 'librarycache' ))
2627 for appmanifest in Path (self .path ).joinpath ('steamapps' ).glob ('appmanifest_*.acf' ):
2728 try :
2829 manifest = VDF (appmanifest )
@@ -45,22 +46,44 @@ def games(self):
4546 path = Path (self .path ).joinpath (
4647 manifest ['AppState' ]['installdir' ]),
4748 id = manifest ['AppState' ]['appid' ],
48- image_dir = Path (self .steam .path ).joinpath (
49- 'appcache' , 'librarycache' ),
49+ image_dir = image_dir ,
5050 )
5151 )
5252 return games
5353
54+ class LibraryImageDir :
55+ """
56+ Caches the filesystem access of a library image directory
57+ """
58+
59+ def __init__ (self , image_dir : Union [str , Path ]):
60+ image_dir = Path (image_dir )
61+ self .grid = image_dir .name == 'grid'
62+ self ._files_cache = {}
63+ self ._iterdir = image_dir .iterdir ()
64+
65+ def get_image (self , id : str , type : str , sep = '_' ) -> Path :
66+
67+ prefix = f'{ id } { sep } { type } '
68+ if prefix in self ._files_cache :
69+ return self ._files_cache [prefix ]
70+ else :
71+ for file in self ._iterdir :
72+ haystack_prefix = file .name .split ("." , 1 )[0 ]
73+ self ._files_cache [haystack_prefix ] = file
74+ if prefix == haystack_prefix :
75+ return file
76+ return None
5477
5578class LibraryItem :
5679 """
5780 Base class for all library items.
5881 """
5982
60- def __init__ (self , name : str , path : Union [str , Path ], image_dir : Union [ str , Path ] , id : str = None ):
83+ def __init__ (self , name : str , path : Union [str , Path ], image_dir : LibraryImageDir , id : str = None ):
6184 self .name = name
6285 self .path = Path (path )
63- self .image_dir = Path ( image_dir )
86+ self .image_dir = image_dir
6487 self ._id = id
6588 self ._image_id = self .id
6689
@@ -88,10 +111,10 @@ def launch(self) -> None:
88111
89112 def get_image (self , type : str , sep = '_' ) -> Path :
90113 id = self .id
91- if Path ( self .image_dir ). name == ' grid' :
92- # Grid images use short ID format
114+ if self .image_dir . grid :
115+ # Grid images use the short ID
93116 id = self .short_id ()
94- return next ( Path ( self .image_dir ). glob ( f' { id } { sep } { type } .*' ), None )
117+ return self .image_dir . get_image ( id , type , sep )
95118
96119 @cached_property
97120 def icon (self ) -> Path :
0 commit comments