22using Newtonsoft . Json . Linq ;
33using System . Collections . Concurrent ;
44using System . Security . Cryptography ;
5+ using System . Text ;
56using System . Threading ;
67
78namespace Wauncher . Utils
89{
10+ internal static class PatchManifestCache
11+ {
12+ private static string CachePath => Path . Combine (
13+ Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) ,
14+ "ClassicCounter" , "Wauncher" , "patch_manifest.hash" ) ;
15+
16+ public static string ? Load ( )
17+ {
18+ try { return File . ReadAllText ( CachePath ) . Trim ( ) ; }
19+ catch { return null ; }
20+ }
21+
22+ public static void Save ( string hash )
23+ {
24+ try
25+ {
26+ Directory . CreateDirectory ( Path . GetDirectoryName ( CachePath ) ! ) ;
27+ File . WriteAllText ( CachePath , hash ) ;
28+ }
29+ catch { }
30+ }
31+
32+ public static void Clear ( )
33+ {
34+ try { File . Delete ( CachePath ) ; }
35+ catch { }
36+ }
37+
38+ public static string ComputeHash ( string input )
39+ {
40+ var bytes = MD5 . HashData ( Encoding . UTF8 . GetBytes ( input ) ) ;
41+ return Convert . ToHexString ( bytes ) . ToLowerInvariant ( ) ;
42+ }
43+ }
44+
945 public class Patch
1046 {
1147 [ JsonProperty ( PropertyName = "file" ) ]
@@ -40,23 +76,42 @@ private static string GetOriginalFileName(string fileName)
4076 return fileName . EndsWith ( ".7z" ) ? fileName [ ..^ 3 ] : fileName ;
4177 }
4278
43- private static async Task < List < Patch > > GetPatches ( bool validateAll = false )
79+ public static async Task < string > FetchPatchJsonAsync ( )
4480 {
45- List < Patch > patches = new List < Patch > ( ) ;
46-
47- string responseString ;
4881 try
4982 {
50- responseString = await Api . ClassicCounter . GetPatches ( ) ;
83+ return await Api . ClassicCounter . GetPatches ( ) ;
5184 }
5285 catch ( Exception ex )
5386 {
54- // Network failure / timeout / DNS — surface it instead of swallowing,
55- // so the UI can say "Can't connect to update server" rather than hang.
5687 if ( Debug . Enabled ( ) )
57- Terminal . Debug ( $ "Couldn't reach { ( validateAll ? "full game" : " patch" ) } API: { ex . Message } ") ;
88+ Terminal . Debug ( $ "Couldn't reach patch API: { ex . Message } ") ;
5889 throw new UpdateServerUnreachableException ( "Can't connect to update server" , ex ) ;
5990 }
91+ }
92+
93+ private static async Task < List < Patch > > GetPatches ( string ? rawJson = null )
94+ {
95+ List < Patch > patches = new List < Patch > ( ) ;
96+
97+ string responseString ;
98+ if ( rawJson != null )
99+ {
100+ responseString = rawJson ;
101+ }
102+ else
103+ {
104+ try
105+ {
106+ responseString = await Api . ClassicCounter . GetPatches ( ) ;
107+ }
108+ catch ( Exception ex )
109+ {
110+ if ( Debug . Enabled ( ) )
111+ Terminal . Debug ( $ "Couldn't reach patch API: { ex . Message } ") ;
112+ throw new UpdateServerUnreachableException ( "Can't connect to update server" , ex ) ;
113+ }
114+ }
60115
61116 try
62117 {
@@ -67,7 +122,6 @@ private static async Task<List<Patch>> GetPatches(bool validateAll = false)
67122 }
68123 catch ( Exception ex )
69124 {
70- // Reachable but returned something we can't parse — treat as a server problem.
71125 if ( Debug . Enabled ( ) )
72126 Terminal . Debug ( $ "Update server returned invalid data: { ex . Message } ") ;
73127 throw new UpdateServerUnreachableException ( "Update server returned invalid data" , ex ) ;
@@ -84,9 +138,9 @@ private static async Task<string> GetHash(string filePath)
84138 return BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLowerInvariant ( ) ;
85139 }
86140
87- public static async Task < Patches > ValidatePatches ( bool validateAll = false , bool deleteOutdatedFiles = true , Action < int , int > ? onProgress = null )
141+ public static async Task < Patches > ValidatePatches ( string ? rawJson = null , bool validateAll = false , bool deleteOutdatedFiles = true , Action < int , int > ? onProgress = null )
88142 {
89- List < Patch > patches = await GetPatches ( validateAll ) ;
143+ List < Patch > patches = await GetPatches ( rawJson ) ;
90144 List < Patch > missing = new ( ) ;
91145 List < Patch > outdated = new ( ) ;
92146 Patch ? dirPatch = null ;
0 commit comments