add type validating of resource value

This commit is contained in:
Vologhat
2023-07-01 16:15:20 +03:00
parent 3629e71098
commit ff4a4094ff
3 changed files with 32 additions and 27 deletions
@@ -130,9 +130,9 @@ public class BinaryResourceValue implements SerializableResource {
*/
public static final int SIZE = 8;
private final int size;
private final Type type;
private final int data;
public int size;
public Type type;
public int data;
public static BinaryResourceValue create(ByteBuffer buffer) {
int size = (buffer.getShort() & 0xFFFF);
@@ -161,8 +161,7 @@ public class ThemesManager {
final var arscEntry = apk.getEntry("resources.arsc");
final var arscBis = new BufferedInputStream(apk.getInputStream(arscEntry));
final var arsc = BinaryResourceFile.fromInputStream(arscBis);
if (!ArscEditor.changeColors(arsc, ACCENT_COLORS, accentColor))
throw new IllegalStateException("Not all colors have been changed");
ArscEditor.changeColors(arsc, ACCENT_COLORS, accentColor);
try (
final var zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(modApk)))
@@ -4,34 +4,40 @@ import com.google.devrel.gmscore.tools.apk.arsc.*;
public class ArscEditor {
public static boolean changeColors(BinaryResourceFile arsc, int[] colorIds, int color /*int[] colors*/) {
int q = 0;
for (var chunk : arsc.getChunks()) {
final var resourceTableChunk = (ResourceTableChunk) chunk;
for (PackageChunk packageChunk : resourceTableChunk.getPackages()) {
for (TypeSpecChunk typeSpecChunk : packageChunk.getTypeSpecChunks()) {
for (TypeChunk typeChunk : packageChunk.getTypeChunks(typeSpecChunk.getId())) {
if (!"color".equals(typeChunk.getTypeName())) continue;
for (var entry : typeChunk.getEntries().entrySet()) {
if (entry.getValue().isComplex()) continue;
final var id = BinaryResourceIdentifier.create(packageChunk.getId(), typeSpecChunk.getId(), entry.getKey());
for (int colorId : colorIds) {
if (colorId != id.resId()) continue;
final var value = entry.getValue();
value.updateValue(new BinaryResourceValue(8, value.value().type(), color /*colors[i]*/));
++q;
public static void changeColors(BinaryResourceFile arsc,int[] colorIds,int color) {
for(var chunk:arsc.getChunks())
{
final var resTableChunk=(ResourceTableChunk)chunk;
for(var packageChunk:resTableChunk.getPackages())
{
for(var typeChunk:packageChunk.getTypeChunks("color"))
{
for(var entry:typeChunk.getEntries().entrySet())
{
final var value=entry.getValue();
if(value.isComplex())continue;
final var binValue=value.value();
if(!isColorResourceValue(binValue))continue;
final var resId=packageChunk.getId()<<24|typeChunk.getId()<<16|entry.getKey();
for(var colodId:colorIds)
{
if(resId==colodId)
{
binValue.data=color;
break;
}
}
}
}
}
}
}
return q == colorIds.length;
private static boolean isColorResourceValue(BinaryResourceValue value)
{
return value.type==BinaryResourceValue.Type.INT_COLOR_ARGB8
||value.type==BinaryResourceValue.Type.INT_COLOR_RGB8
||value.type==BinaryResourceValue.Type.INT_COLOR_ARGB4
||value.type==BinaryResourceValue.Type.INT_COLOR_RGB4;
}
}