Skip to content

Commit 9400b18

Browse files
authored
Merge pull request #42 from masterpi314/icon_performance
Cache & hash image dir listing for performance
2 parents 920dc7e + cadc194 commit 9400b18

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Name": "Steam Search",
55
"Description": "Search and launch your Steam Game library",
66
"Author": "Garulf",
7-
"Version": "7.2.0",
7+
"Version": "8.0.0",
88
"Language": "executable",
99
"Website": "https://github.com/Garulf/Steam-Search",
1010
"IcoPath": "run.exe",

plugin/library.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

5578
class 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:

plugin/loginusers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import UserList
66

77
from .vdfs import VDF
8-
from .library import LibraryItem
8+
from .library import LibraryItem, LibraryImageDir
99
if TYPE_CHECKING:
1010
from steam import Steam
1111

@@ -75,12 +75,13 @@ def shortcuts(self) -> list:
7575
split = _shortcuts.split(b'AppName\x00')[1:]
7676
if len(split) == 0:
7777
return _list
78+
image_dir = LibraryImageDir(self.grid_path)
7879
for shortcut in _shortcuts.split(b'AppName\x00')[1:]:
7980
_list.append(
8081
LibraryItem(
8182
name=shortcut.split(b'\x00')[0].decode('utf-8'),
8283
path=shortcut.split(b'\x00')[2].decode('utf-8'),
83-
image_dir=self.grid_path
84+
image_dir=image_dir
8485
)
8586
)
8687
return _list

0 commit comments

Comments
 (0)