-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: ShowMsgAt: find id from dex code
- Loading branch information
Showing
4 changed files
with
357 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
app/src/main/java/io/github/qauxv/util/dexkit/HostMainDexHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/* | ||
* QAuxiliary - An Xposed module for QQ/TIM | ||
* Copyright (C) 2019-2023 QAuxiliary developers | ||
* https://github.com/cinit/QAuxiliary | ||
* | ||
* This software is non-free but opensource software: you can redistribute it | ||
* and/or modify it under the terms of the GNU Affero General Public License | ||
* as published by the Free Software Foundation; either | ||
* version 3 of the License, or any later version and our eula as published | ||
* by QAuxiliary contributors. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* and eula along with this software. If not, see | ||
* <https://www.gnu.org/licenses/> | ||
* <https://github.com/cinit/QAuxiliary/blob/master/LICENSE.md>. | ||
*/ | ||
|
||
package io.github.qauxv.util.dexkit; | ||
|
||
import android.app.Application; | ||
import androidx.annotation.Nullable; | ||
import io.github.qauxv.util.HostInfo; | ||
import io.github.qauxv.util.IoUtils; | ||
import java.io.IOException; | ||
import java.lang.ref.WeakReference; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Objects; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipFile; | ||
|
||
public class HostMainDexHelper { | ||
|
||
private HostMainDexHelper() { | ||
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); | ||
} | ||
|
||
private static HashMap<Integer, WeakReference<byte[]>> sCachedDex = new HashMap<>(32); | ||
|
||
@Nullable | ||
private static byte[] extractDexFromHost(int index) { | ||
Application app = HostInfo.getHostInfo().getApplication(); | ||
// get path of base.apk | ||
String apkPath = app.getApplicationInfo().sourceDir; | ||
String dexName = getNameForIndex(index); | ||
// FIXME 2023-07-25: on very old QQ/TIM versions, some dex is in assets | ||
try (ZipFile zipFile = new ZipFile(apkPath)) { | ||
ZipEntry entry = zipFile.getEntry(dexName); | ||
if (entry == null) { | ||
return null; | ||
} | ||
return IoUtils.readFully(zipFile.getInputStream(entry)); | ||
} catch (IOException e) { | ||
IoUtils.unsafeThrow(e); | ||
// unreachable | ||
return null; | ||
} | ||
} | ||
|
||
public static boolean hasDexIndex(int i) { | ||
Application app = HostInfo.getHostInfo().getApplication(); | ||
// get path of base.apk | ||
String apkPath = app.getApplicationInfo().sourceDir; | ||
String dexName = getNameForIndex(i); | ||
// FIXME 2023-07-25: on very old QQ/TIM versions, some dex is in assets | ||
try (ZipFile zipFile = new ZipFile(apkPath)) { | ||
ZipEntry entry = zipFile.getEntry(dexName); | ||
return entry != null; | ||
} catch (IOException e) { | ||
IoUtils.unsafeThrow(e); | ||
// unreachable | ||
return false; | ||
} | ||
} | ||
|
||
private static String getNameForIndex(int i) { | ||
if (i <= 1) { | ||
return "classes.dex"; | ||
} else { | ||
return "classes" + i + ".dex"; | ||
} | ||
} | ||
|
||
@Nullable | ||
public static byte[] getHostDexIndex(int i) { | ||
if (i <= 0) { | ||
return null; | ||
} | ||
// load from cache | ||
WeakReference<byte[]> weakReference = sCachedDex.get(i); | ||
if (weakReference != null) { | ||
byte[] bytes = weakReference.get(); | ||
if (bytes != null) { | ||
return bytes; | ||
} | ||
} | ||
// load from host | ||
byte[] bytes = extractDexFromHost(i); | ||
if (bytes != null) { | ||
sCachedDex.put(i, new WeakReference<>(bytes)); | ||
} | ||
return bytes; | ||
} | ||
|
||
|
||
public static class DexIterator implements Iterator<byte[]> { | ||
|
||
private int nextIndex = 1; | ||
|
||
private DexIterator() { | ||
} | ||
|
||
public boolean hasNext() { | ||
return HostMainDexHelper.hasDexIndex(this.nextIndex); | ||
} | ||
|
||
public byte[] next() { | ||
byte[] hostDexIndex = HostMainDexHelper.getHostDexIndex(this.nextIndex); | ||
if (hostDexIndex != null) { | ||
this.nextIndex++; | ||
} | ||
return hostDexIndex; | ||
} | ||
|
||
public int getLastIndex() { | ||
return this.nextIndex - 1; | ||
} | ||
|
||
} | ||
|
||
public static Iterator<byte[]> getDexIterator() { | ||
return new DexIterator(); | ||
} | ||
|
||
public static Iterable<byte[]> asIterable() { | ||
return DexIterator::new; | ||
} | ||
|
||
@Nullable | ||
public static byte[] findDexWithClass(String name) { | ||
if (name == null || name.isEmpty()) { | ||
throw new IllegalArgumentException("name == null || name.isEmpty()"); | ||
} | ||
for (byte[] dex : asIterable()) { | ||
if (DexFlow.hasClassInDex(dex, name)) { | ||
return dex; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Nullable | ||
public static byte[] findDexWithClass(Class<?> klass) { | ||
Objects.requireNonNull(klass, "klass == null"); | ||
return findDexWithClass(klass.getName()); | ||
} | ||
|
||
} |
Oops, something went wrong.