1
1
package io .github .xausky .unitymodmanager .utils ;
2
2
3
+ import android .app .backup .SharedPreferencesBackupHelper ;
4
+ import android .content .Context ;
5
+ import android .content .SharedPreferences ;
3
6
import android .database .Cursor ;
4
7
import android .database .sqlite .SQLiteDatabase ;
5
8
9
+ import com .google .android .gms .common .util .SharedPreferencesUtils ;
6
10
import com .topjohnwu .superuser .Shell ;
7
11
8
12
import org .apache .commons .io .FileUtils ;
9
- import org .apache .commons .io .IOUtils ;
10
- import org .json .JSONException ;
11
13
import org .json .JSONObject ;
12
14
13
- import java .io .ByteArrayOutputStream ;
14
15
import java .io .File ;
15
- import java .io .IOException ;
16
- import java .io .OutputStream ;
17
- import java .util .HashMap ;
18
- import java .util .LinkedList ;
19
- import java .util .List ;
20
- import java .util .Map ;
21
- import java .util .StringJoiner ;
22
- import java .util .zip .Deflater ;
23
- import java .util .zip .DeflaterOutputStream ;
24
- import java .util .zip .Inflater ;
25
16
26
- public class CompressUtil {
27
- public static byte [] compress (byte [] data ) {
28
- byte [] output = new byte [0 ];
29
- Deflater compresser = new Deflater ();
30
- compresser .reset ();
31
- compresser .setInput (data );
32
- compresser .finish ();
33
- ByteArrayOutputStream bos = new ByteArrayOutputStream (data .length );
34
- try {
35
- byte [] buf = new byte [1024 ];
36
- while (!compresser .finished ()) {
37
- int i = compresser .deflate (buf );
38
- bos .write (buf , 0 , i );
39
- }
40
- output = bos .toByteArray ();
41
- } catch (Exception e ) {
42
- output = data ;
43
- e .printStackTrace ();
44
- } finally {
45
- try {
46
- bos .close ();
47
- } catch (IOException e ) {
48
- e .printStackTrace ();
49
- }
50
- }
51
- compresser .end ();
52
- return output ;
53
- }
54
-
55
- //解压缩 字节数组
56
- public static byte [] decompress (byte [] data ) {
57
- byte [] output = new byte [0 ];
58
-
59
- Inflater decompresser = new Inflater ();
60
- decompresser .reset ();
61
- decompresser .setInput (data );
17
+ import static android .content .Context .MODE_PRIVATE ;
62
18
63
- ByteArrayOutputStream o = new ByteArrayOutputStream (data .length );
64
- try {
65
- byte [] buf = new byte [1024 ];
66
- while (!decompresser .finished ()) {
67
- int i = decompresser .inflate (buf );
68
- o .write (buf , 0 , i );
69
- }
70
- output = o .toByteArray ();
71
- } catch (Exception e ) {
72
- output = data ;
73
- e .printStackTrace ();
74
- } finally {
75
- try {
76
- o .close ();
77
- } catch (IOException e ) {
78
- e .printStackTrace ();
79
- }
80
- }
81
-
82
- decompresser .end ();
83
- return output ;
84
- }
19
+ public class BackupUtil {
85
20
86
- public static byte [] backupKuroGame (String packageName ) {
87
- String accountsDatabaseFile = "/data/data/" + packageName + "/databases/ zz_sdk_db" ;
88
- String deviceIdFile = "/data/data/" + packageName + "/shared_prefs/devicesyn.xml" ;
21
+ public static byte [] backupKuroGame (Context context ) {
22
+ String accountsDatabaseFile = context . getDatabasePath ( " zz_sdk_db"). getAbsolutePath () ;
23
+ String deviceIdFile = context . getFilesDir (). getAbsolutePath () + "/shared_prefs/devicesyn.xml" ;
89
24
try {
90
25
unprotectFilesWithRoot (accountsDatabaseFile , deviceIdFile );
91
26
JSONObject root = new JSONObject ();
@@ -105,7 +40,9 @@ public static byte[] backupKuroGame(String packageName) {
105
40
}
106
41
}
107
42
root .put ("accounts" , builder .toString ());
108
- root .put ("device" , FileUtils .readFileToString (new File (deviceIdFile )));
43
+ SharedPreferences sharedPreferences = context .getSharedPreferences ("devicesyn" , MODE_PRIVATE );
44
+ String deviceId = sharedPreferences .getString ("device_id" , null );
45
+ root .put ("device_id" , deviceId );
109
46
return root .toString ().getBytes ();
110
47
} catch (Exception e ) {
111
48
throw new RuntimeException (e );
@@ -114,9 +51,9 @@ public static byte[] backupKuroGame(String packageName) {
114
51
}
115
52
}
116
53
117
- public static void restoreKuroGame (String packageName , byte [] data ) {
118
- String accountsDatabaseFile = "/data/data/" + packageName + "/databases/ zz_sdk_db" ;
119
- String deviceIdFile = "/data/data/" + packageName + "/shared_prefs/devicesyn.xml" ;
54
+ public static void restoreKuroGame (Context context , byte [] data ) {
55
+ String accountsDatabaseFile = context . getDatabasePath ( " zz_sdk_db"). getAbsolutePath () ;
56
+ String deviceIdFile = context . getFilesDir (). getAbsolutePath () + "/shared_prefs/devicesyn.xml" ;
120
57
try {
121
58
unprotectFilesWithRoot (accountsDatabaseFile , deviceIdFile );
122
59
JSONObject root = new JSONObject (new String (data ));
@@ -126,15 +63,16 @@ public static void restoreKuroGame(String packageName, byte[] data) {
126
63
db .execSQL ("INSERT INTO sdkuser(user_id,login_id,login_name,password,auto_login,last_login_time,login_type,local_login_count,user_type) values (?, ?, ?, ?, ?, ?, ?, ?, ?)" , account .split ("," ));
127
64
}
128
65
}
129
- FileUtils .writeStringToFile (new File (deviceIdFile ), root .getString ("device" ));
66
+ SharedPreferences sharedPreferences = context .getSharedPreferences ("devicesyn" , MODE_PRIVATE );
67
+ sharedPreferences .edit ().putString ("fake_device_id" , root .getString ("device_id" )).commit ();
130
68
} catch (Exception e ) {
131
69
throw new RuntimeException (e );
132
70
} finally {
133
71
protectFilesWithRoot (accountsDatabaseFile , deviceIdFile );
134
72
}
135
73
}
136
74
137
- public static void unprotectFilesWithRoot (String ...files ){
75
+ private static void unprotectFilesWithRoot (String ...files ){
138
76
StringBuilder builder = new StringBuilder ();
139
77
for (int i = 1 ; i <= files .length ; i ++){
140
78
builder .append (files [i - 1 ]);
@@ -145,7 +83,7 @@ public static void unprotectFilesWithRoot(String ...files){
145
83
Shell .su ("setenforce 0" , "chmod 666 " + builder .toString ()).exec ();
146
84
}
147
85
148
- public static void protectFilesWithRoot (String ...files ){
86
+ private static void protectFilesWithRoot (String ...files ){
149
87
StringBuilder builder = new StringBuilder ();
150
88
for (int i = 1 ; i <= files .length ; i ++){
151
89
builder .append (files [i - 1 ]);
0 commit comments