Skip to content

Commit 5debdb8

Browse files
committed
Caches and loads steam background images to disk instead of downloading them every time
1 parent a5254d3 commit 5debdb8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

CompactGUI/Helper.vb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,20 @@ Module Helper
5151
End Function
5252

5353

54+
Function LoadImageFromDisk(imagePath As String) As BitmapImage
55+
Dim bImg As New BitmapImage(New Uri(imagePath))
56+
Return bImg
57+
End Function
58+
59+
Function LoadImageFromMemoryStream(imageData As Byte()) As BitmapImage
60+
Dim bImg As New BitmapImage()
61+
Using ms As New MemoryStream(imageData)
62+
bImg.BeginInit()
63+
bImg.CacheOption = BitmapCacheOption.OnLoad
64+
bImg.StreamSource = ms
65+
bImg.EndInit()
66+
End Using
67+
Return bImg
68+
End Function
69+
5470
End Module

CompactGUI/ViewModels/MainViewModel.vb

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,30 @@ Public Class MainViewModel : Inherits ObservableObject
6262
End Function
6363

6464

65+
6566
Private Async Function GetSteamHeaderAsync() As Task
6667

67-
Dim url As String = $"https://steamcdn-a.akamaihd.net/steam/apps/{ActiveFolder.SteamAppID}/page_bg_generated_v6b.jpg"
68+
If ActiveFolder.SteamAppID = 0 Then Return
69+
70+
Dim EnvironmentPath = Environment.GetEnvironmentVariable("IridiumIO", EnvironmentVariableTarget.User)
71+
Dim imagePath = Path.Combine(EnvironmentPath, "CompactGUI", "SteamCache", $"{ActiveFolder.SteamAppID}.jpg")
6872

73+
If Not Path.Exists(Path.GetDirectoryName(imagePath)) Then Directory.CreateDirectory(Path.GetDirectoryName(imagePath))
74+
75+
If File.Exists(imagePath) Then
76+
SteamBGImage = Helper.LoadImageFromDisk(imagePath)
77+
Debug.WriteLine("Loaded Steam header image from disk")
78+
Return
79+
End If
80+
81+
Dim url As String = $"https://steamcdn-a.akamaihd.net/steam/apps/{ActiveFolder.SteamAppID}/page_bg_generated_v6b.jpg"
6982
If SteamBGImage?.UriSource IsNot Nothing AndAlso SteamBGImage.UriSource.ToString() = url Then Return
7083

7184
Try
7285
Using client As New HttpClient()
73-
7486
Dim imageData As Byte() = Await client.GetByteArrayAsync(url)
75-
76-
Dim bImg As New BitmapImage()
77-
Using ms As New MemoryStream(imageData)
78-
bImg.BeginInit()
79-
bImg.CacheOption = BitmapCacheOption.OnLoad
80-
bImg.StreamSource = ms
81-
bImg.EndInit()
82-
End Using
83-
84-
SteamBGImage = bImg
87+
SteamBGImage = LoadImageFromMemoryStream(imageData)
88+
Await File.WriteAllBytesAsync(imagePath, imageData)
8589
End Using
8690
Catch ex As Exception
8791
Debug.WriteLine($"Failed to load Steam header image: {ex.Message}")

0 commit comments

Comments
 (0)