@@ -40,6 +40,33 @@ private async void UserControl_Loaded(object sender, RoutedEventArgs e)
4040 InstallViewModel . Instance . InstalledGamesFilter = CollectionViewSource . GetDefaultView ( InstallViewModel . Instance . InstalledGames ) ;
4141 }
4242 }
43+ private List < Game > GetGamesFromOfflineCache ( Dictionary < int , string > games )
44+ {
45+ List < Game > offlineCacheGames = new List < Game > ( ) ;
46+ foreach ( KeyValuePair < int , string > entry in games )
47+ {
48+ string objectFromFile = Preferences . Get ( entry . Key . ToString ( ) , LoginManager . Instance . GetUserProfile ( ) . OfflineCache ) ;
49+ if ( objectFromFile != string . Empty )
50+ {
51+ try
52+ {
53+ string decompressedObject = StringCompressor . DecompressString ( objectFromFile ) ;
54+ Game ? deserializedObject = JsonSerializer . Deserialize < Game > ( decompressedObject ) ;
55+ if ( deserializedObject != null )
56+ {
57+ offlineCacheGames . Add ( deserializedObject ) ;
58+ }
59+ }
60+ catch ( FormatException exFormat ) { }
61+ }
62+ else
63+ {
64+ string gameTitle = Path . GetFileName ( entry . Value ) ;
65+ offlineCacheGames . Add ( new Game ( ) { ID = entry . Key , Title = gameTitle . Substring ( gameTitle . IndexOf ( ')' ) + 1 ) } ) ;
66+ }
67+ }
68+ return offlineCacheGames ;
69+ }
4370 public async Task RestoreInstalledGames ( bool fromCLI = false )
4471 {
4572 if ( gamesRestored )
@@ -98,33 +125,26 @@ public async Task RestoreInstalledGames(bool fromCLI = false)
98125 if ( LoginManager . Instance . IsLoggedIn ( ) )
99126 {
100127 string gameList = await WebHelper . GetAsync ( @$ "{ SettingsViewModel . Instance . ServerUrl } /api/games?filter.id=$in:{ gameIds } &limit=-1") ;
101- return JsonSerializer . Deserialize < PaginatedData < Game > > ( gameList ) . Data ;
102- }
103- else
104- {
105- List < Game > offlineCacheGames = new List < Game > ( ) ;
106- foreach ( KeyValuePair < int , string > entry in foundGames )
128+ Game [ ] serverGames = JsonSerializer . Deserialize < PaginatedData < Game > > ( gameList ) ! . Data ;
129+
130+ var serverGameIds = new HashSet < int > ( serverGames . Select ( g => g . ID ) ) ;
131+ var missingGames = foundGames . Where ( kvp => ! serverGameIds . Contains ( kvp . Key ) )
132+ . ToDictionary ( kvp => kvp . Key , kvp => kvp . Value ) ;
133+
134+ //Make sure to display installed games that are already deleted on the server
135+ if ( missingGames . Count > 0 )
107136 {
108- string objectFromFile = Preferences . Get ( entry . Key . ToString ( ) , LoginManager . Instance . GetUserProfile ( ) . OfflineCache ) ;
109- if ( objectFromFile != string . Empty )
110- {
111- try
112- {
113- string decompressedObject = StringCompressor . DecompressString ( objectFromFile ) ;
114- Game ? deserializedObject = JsonSerializer . Deserialize < Game > ( decompressedObject ) ;
115- if ( deserializedObject != null )
116- {
117- offlineCacheGames . Add ( deserializedObject ) ;
118- }
119- }
120- catch ( FormatException exFormat ) { }
121- }
122- else
137+ List < Game > offlineCacheGames = GetGamesFromOfflineCache ( foundGames ) ;
138+ if ( offlineCacheGames . Count > 0 )
123139 {
124- string gameTitle = Path . GetFileName ( entry . Value ) ;
125- offlineCacheGames . Add ( new Game ( ) { ID = entry . Key , Title = gameTitle . Substring ( gameTitle . IndexOf ( ')' ) + 1 ) } ) ;
140+ serverGames = serverGames . Concat ( offlineCacheGames ) . ToArray ( ) ;
126141 }
127142 }
143+ return serverGames ;
144+ }
145+ else
146+ {
147+ List < Game > offlineCacheGames = GetGamesFromOfflineCache ( foundGames ) ;
128148 return offlineCacheGames . ToArray ( ) ;
129149 }
130150 }
0 commit comments