11package io .github .xausky .unitymodmanager .utils ;
22
3+ import android .app .backup .SharedPreferencesBackupHelper ;
4+ import android .content .Context ;
5+ import android .content .SharedPreferences ;
36import android .database .Cursor ;
47import android .database .sqlite .SQLiteDatabase ;
58
9+ import com .google .android .gms .common .util .SharedPreferencesUtils ;
610import com .topjohnwu .superuser .Shell ;
711
812import org .apache .commons .io .FileUtils ;
9- import org .apache .commons .io .IOUtils ;
10- import org .json .JSONException ;
1113import org .json .JSONObject ;
1214
13- import java .io .ByteArrayOutputStream ;
1415import 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 ;
2516
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 ;
6218
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 {
8520
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" ;
8924 try {
9025 unprotectFilesWithRoot (accountsDatabaseFile , deviceIdFile );
9126 JSONObject root = new JSONObject ();
@@ -105,7 +40,9 @@ public static byte[] backupKuroGame(String packageName) {
10540 }
10641 }
10742 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 );
10946 return root .toString ().getBytes ();
11047 } catch (Exception e ) {
11148 throw new RuntimeException (e );
@@ -114,9 +51,9 @@ public static byte[] backupKuroGame(String packageName) {
11451 }
11552 }
11653
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" ;
12057 try {
12158 unprotectFilesWithRoot (accountsDatabaseFile , deviceIdFile );
12259 JSONObject root = new JSONObject (new String (data ));
@@ -126,15 +63,16 @@ public static void restoreKuroGame(String packageName, byte[] data) {
12663 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 ("," ));
12764 }
12865 }
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 ();
13068 } catch (Exception e ) {
13169 throw new RuntimeException (e );
13270 } finally {
13371 protectFilesWithRoot (accountsDatabaseFile , deviceIdFile );
13472 }
13573 }
13674
137- public static void unprotectFilesWithRoot (String ...files ){
75+ private static void unprotectFilesWithRoot (String ...files ){
13876 StringBuilder builder = new StringBuilder ();
13977 for (int i = 1 ; i <= files .length ; i ++){
14078 builder .append (files [i - 1 ]);
@@ -145,7 +83,7 @@ public static void unprotectFilesWithRoot(String ...files){
14583 Shell .su ("setenforce 0" , "chmod 666 " + builder .toString ()).exec ();
14684 }
14785
148- public static void protectFilesWithRoot (String ...files ){
86+ private static void protectFilesWithRoot (String ...files ){
14987 StringBuilder builder = new StringBuilder ();
15088 for (int i = 1 ; i <= files .length ; i ++){
15189 builder .append (files [i - 1 ]);
0 commit comments