77 */
88package net .wurstclient .hacks ;
99
10+ import java .io .IOException ;
11+ import java .nio .file .Files ;
12+ import java .nio .file .Path ;
1013import java .util .LinkedHashSet ;
1114import java .util .Set ;
1215
16+ import com .google .gson .JsonArray ;
17+
1318import net .wurstclient .Category ;
1419import net .wurstclient .DontBlock ;
1520import net .wurstclient .SearchTags ;
1621import net .wurstclient .hack .Hack ;
1722import net .wurstclient .hack .HackList ;
1823import net .wurstclient .settings .ButtonSetting ;
1924import net .wurstclient .util .ChatUtils ;
25+ import net .wurstclient .util .json .JsonException ;
26+ import net .wurstclient .util .json .JsonUtils ;
27+ import net .wurstclient .util .json .WsonArray ;
2028import net .wurstclient .util .text .WText ;
2129
2230@ SearchTags ({"legit" , "disable" })
2331@ DontBlock
2432public final class PanicHack extends Hack
2533{
2634 private final Set <String > savedHackNames = new LinkedHashSet <>();
35+ private final Path snapshotFile =
36+ WURST .getWurstFolder ().resolve ("panic-snapshot.json" );
37+ private boolean startupRestorePending ;
2738
2839 private final ButtonSetting restoreButton = new ButtonSetting (
2940 "Restore saved hacks" ,
@@ -37,6 +48,7 @@ public PanicHack()
3748 setCategory (Category .OTHER );
3849 addSetting (restoreButton );
3950 addPossibleKeybind ("panic restore" , "Restore hacks saved by Panic" );
51+ loadSnapshotFromDisk ();
4052 }
4153
4254 @ Override
@@ -62,6 +74,8 @@ private int snapshotEnabledHacks()
6274 if (hack .isEnabled () && hack != this )
6375 savedHackNames .add (hack .getName ());
6476
77+ persistSavedHacks ();
78+ startupRestorePending = false ;
6579 return savedHackNames .size ();
6680 }
6781
@@ -82,9 +96,10 @@ public void restoreSavedHacks()
8296
8397 HackList hax = WURST .getHax ();
8498 Set <String > missing = new LinkedHashSet <>();
99+ Set <String > blocked = new LinkedHashSet <>();
85100 int restored = 0 ;
86101
87- for (String name : savedHackNames )
102+ for (String name : new LinkedHashSet <>( savedHackNames ) )
88103 {
89104 Hack hack = hax .getHackByName (name );
90105 if (hack == null || hack == this )
@@ -95,18 +110,112 @@ public void restoreSavedHacks()
95110
96111 boolean wasEnabled = hack .isEnabled ();
97112 hack .setEnabled (true );
113+
114+ if (!hack .isEnabled ())
115+ {
116+ blocked .add (name );
117+ continue ;
118+ }
119+
98120 if (!wasEnabled && hack .isEnabled ())
99121 restored ++;
100122 }
101123
102- if (! missing . isEmpty ())
103- savedHackNames .removeAll ( missing );
124+ savedHackNames . clear ();
125+ savedHackNames .addAll ( blocked );
104126
105127 if (restored > 0 )
106128 ChatUtils .message ("Restored " + restored + " hack"
107129 + (restored == 1 ? "" : "s" ) + " from Panic." );
108- else
109- ChatUtils .message (
110- "All saved Panic hacks are already enabled or blocked." );
130+ else if (savedHackNames .isEmpty ())
131+ ChatUtils .message ("All saved Panic hacks are already enabled." );
132+
133+ if (!missing .isEmpty ())
134+ ChatUtils
135+ .warning ("Missing Panic hacks: " + String .join (", " , missing ));
136+
137+ if (!savedHackNames .isEmpty ())
138+ ChatUtils
139+ .warning ("Still blocked: " + String .join (", " , savedHackNames ));
140+
141+ persistSavedHacks ();
142+ }
143+
144+ public void handleStartupRestore ()
145+ {
146+ if (!startupRestorePending )
147+ return ;
148+
149+ startupRestorePending = false ;
150+
151+ if (savedHackNames .isEmpty ())
152+ {
153+ deleteSnapshotFile ();
154+ return ;
155+ }
156+
157+ restoreSavedHacks ();
158+ }
159+
160+ private void loadSnapshotFromDisk ()
161+ {
162+ startupRestorePending = false ;
163+ savedHackNames .clear ();
164+
165+ if (!Files .exists (snapshotFile ))
166+ return ;
167+
168+ try
169+ {
170+ WsonArray wson = JsonUtils .parseFileToArray (snapshotFile );
171+ savedHackNames .addAll (wson .getAllStrings ());
172+
173+ if (savedHackNames .isEmpty ())
174+ deleteSnapshotFile ();
175+ else
176+ startupRestorePending = true ;
177+
178+ }catch (IOException | JsonException e )
179+ {
180+ System .out .println ("Couldn't load panic snapshot" );
181+ e .printStackTrace ();
182+ deleteSnapshotFile ();
183+ }
184+ }
185+
186+ private void persistSavedHacks ()
187+ {
188+ if (savedHackNames .isEmpty ())
189+ {
190+ deleteSnapshotFile ();
191+ return ;
192+ }
193+
194+ JsonArray json = new JsonArray ();
195+ savedHackNames .forEach (json ::add );
196+
197+ try
198+ {
199+ Files .createDirectories (snapshotFile .getParent ());
200+ JsonUtils .toJson (json , snapshotFile );
201+
202+ }catch (IOException | JsonException e )
203+ {
204+ System .out .println ("Couldn't save panic snapshot" );
205+ e .printStackTrace ();
206+ }
207+ }
208+
209+ private void deleteSnapshotFile ()
210+ {
211+ try
212+ {
213+ Files .deleteIfExists (snapshotFile );
214+
215+ }catch (IOException e )
216+ {
217+ System .out .println ("Couldn't delete panic snapshot" );
218+ e .printStackTrace ();
219+ }
111220 }
112221}
0 commit comments