37
37
import java .util .List ;
38
38
import java .util .Map ;
39
39
import java .util .Objects ;
40
+ import java .util .StringJoiner ;
40
41
import java .util .regex .Pattern ;
41
42
import java .util .stream .Stream ;
42
43
46
47
import static airsquared .blobsaver .app .Utils .getFirmwareList ;
47
48
import static airsquared .blobsaver .app .Utils .getSignedBetas ;
48
49
import static airsquared .blobsaver .app .Utils .getSignedFirmwares ;
50
+ import static airsquared .blobsaver .app .Utils .isNumeric ;
49
51
50
52
public class TSS extends Task <String > {
51
53
@@ -95,13 +97,14 @@ protected String call() throws TSSException {
95
97
System .out .println ("iosVersions = " + iosVersions );
96
98
ArrayList <String > args = constructArgs ();
97
99
98
- StringBuilder responseBuilder = new StringBuilder ("Successfully saved blobs in\n " ).append (savePath );
99
- if (manualIpswURL == null ) {
100
- responseBuilder .append (iosVersions .size () == 1 ? "\n \n For version " : "\n \n For versions " );
101
- }
102
-
103
- // can't use forEach() because exception won't be caught
100
+ var alreadySaved = new StringJoiner (", " );
101
+ var savedFor = new StringJoiner (", " );
104
102
for (Utils .IOSVersion iosVersion : iosVersions ) {
103
+ if (!Prefs .getAlwaysSaveNewBlobs () && checkAlreadySaved (iosVersion )) {
104
+ alreadySaved .add (iosVersion .versionString ());
105
+ continue ;
106
+ }
107
+
105
108
try {
106
109
saveFor (iosVersion , args );
107
110
} catch (TSSException e ) {
@@ -113,11 +116,22 @@ protected String call() throws TSSException {
113
116
}
114
117
115
118
if (iosVersion .versionString () != null ) {
116
- responseBuilder .append (iosVersion .versionString ());
117
- if (iosVersion != iosVersions .get (iosVersions .size () - 1 ))
118
- responseBuilder .append (", " );
119
+ savedFor .add (iosVersion .versionString ());
119
120
}
120
121
}
122
+ StringBuilder responseBuilder = new StringBuilder ();
123
+ if (manualIpswURL != null || savedFor .length () > 0 ) {
124
+ responseBuilder .append ("Successfully saved blobs in\n " ).append (savePath );
125
+ if (savedFor .length () > 0 ) {
126
+ responseBuilder .append ("\n \n For version" ).append (iosVersions .size () == 1 ? " " : "s " ).append (savedFor );
127
+ }
128
+ if (alreadySaved .length () > 0 ) {
129
+ responseBuilder .append ("\n \n " );
130
+ }
131
+ }
132
+ if (alreadySaved .length () > 0 ) {
133
+ responseBuilder .append ("Already saved for " ).append (alreadySaved );
134
+ }
121
135
122
136
if (saveToTSSSaver || saveToSHSHHost ) {
123
137
responseBuilder .append ("\n \n " );
@@ -133,6 +147,27 @@ protected String call() throws TSSException {
133
147
return responseBuilder .toString ();
134
148
}
135
149
150
+ private boolean checkAlreadySaved (Utils .IOSVersion ios ) {
151
+ if (ios .versionString () == null ) {
152
+ return false ;
153
+ }
154
+ var versionStringOnly = ios .versionString ().trim ().replaceFirst (" .*" , "" ); // strip out 'beta' labels
155
+ // https://github.com/1Conan/tsschecker/blob/0bc6174c3c2f77a0de525b71e7d8ec0987f07aa1/tsschecker/tsschecker.c#L1262
156
+ String fileName = "%s_%s_%s_%s-%s_%s.shsh2"
157
+ .formatted (parseECID (), deviceIdentifier , getBoardConfig (), versionStringOnly , ios .buildid (), apnonce );
158
+
159
+ if (Files .exists (Path .of (savePath , fileName ))) {
160
+ System .out .println ("Already Saved: " + fileName );
161
+ return true ;
162
+ }
163
+ return false ;
164
+ }
165
+
166
+ private long parseECID () {
167
+ return isNumeric (ecid ) ? Long .parseLong (ecid )
168
+ : Long .parseLong (ecid .startsWith ("0x" ) ? ecid .substring (2 ) : ecid , 16 );
169
+ }
170
+
136
171
137
172
private void saveFor (Utils .IOSVersion iosVersion , ArrayList <String > args ) throws TSSException {
138
173
final int urlIndex = args .size () - 1 ;
@@ -203,7 +238,7 @@ private List<Utils.IOSVersion> getIOSVersions() throws TSSException {
203
238
manualVersion .equals (iosVersion .versionString ())).findFirst ()
204
239
.orElseThrow (() -> new TSSException ("No versions found." , false )));
205
240
} else if (manualIpswURL != null ) {
206
- return Collections .singletonList (new Utils .IOSVersion (null , manualIpswURL , null ));
241
+ return Collections .singletonList (new Utils .IOSVersion (null , null , manualIpswURL , null ));
207
242
} else if (includeBetas ) {
208
243
return Stream .concat (getSignedFirmwares (deviceIdentifier ), getSignedBetas (deviceIdentifier )).toList ();
209
244
} else { // all signed firmwares
@@ -391,7 +426,7 @@ public void showErrorAlert() {
391
426
private void saveBlobsTSSSaver (StringBuilder responseBuilder ) {
392
427
Map <Object , Object > deviceParameters = new HashMap <>();
393
428
394
- deviceParameters .put ("ecid" , String .valueOf (Long . parseLong ( ecid . startsWith ( "0x" ) ? ecid . substring ( 2 ) : ecid , 16 )));
429
+ deviceParameters .put ("ecid" , String .valueOf (parseECID ( )));
395
430
deviceParameters .put ("deviceIdentifier" , deviceIdentifier );
396
431
deviceParameters .put ("boardConfig" , getBoardConfig ());
397
432
0 commit comments