Skip to content

Commit

Permalink
Add FlashMap & FlashList, support collections in ObjectInspector (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
Punikekk authored Nov 4, 2023
1 parent b7e1b4e commit 8c3522e
Show file tree
Hide file tree
Showing 15 changed files with 1,693 additions and 402 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ default boolean readBoolean(long address) {
return readMemoryBoolean(address);
}

String readStringDirect(long address);

default String readStringDirect(long address, int... offsets) {
return readStringDirect(readLong(address, offsets));
}

@Override
default String readString(long address) {
return readMemoryString(address);
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ public String readMemoryStringFallback(long address, String fallback) {
@Override
public byte[] readMemory(long address, int length) {
if (!ByteUtils.isValidPtr(address)) return new byte[0];
return memory.readBytes(address, length);
synchronized (memory) {
return memory.readBytes(address, length);
}
}

@Override
Expand All @@ -360,7 +362,9 @@ public void readMemory(long address, byte[] buffer, int length) {
Arrays.fill(buffer, 0, length, (byte) 0);
return;
}
memory.readBytes(address, buffer, length);
synchronized (memory) {
memory.readBytes(address, buffer, length);
}
}

@Override
Expand Down Expand Up @@ -616,4 +620,12 @@ public void setQuality(GameAPI.Handler.GameQuality quality) {
public long lastInternetReadTime() {
return handler.lastInternetReadTime();
}

@Override
public String readStringDirect(long address) {
if (!ByteUtils.isValidPtr(address)) return FALLBACK_STRING;
String s = ByteUtils.readStringDirect(address);

return s == null ? FALLBACK_STRING : s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public interface NativeUpdatable {

long getAddress();

default void update() {}
default void update(long address) {}

default int modifyOffset(int offset) {
return offset;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.github.manolo8.darkbot.core.objects.swf;

import com.github.manolo8.darkbot.Main;
import com.github.manolo8.darkbot.core.itf.Updatable;
import com.github.manolo8.darkbot.core.utils.ByteUtils;

import java.util.function.BiFunction;
import java.util.function.LongFunction;

public enum AtomKind {
UNUSED(),
OBJECT(Long.class, atom -> atom & ByteUtils.ATOM_MASK, ByteUtils::getLong),
STRING(String.class, atom -> Main.API.readString(atom & ByteUtils.ATOM_MASK),
(b, i) -> Main.API.readString(ByteUtils.getLong(b, i))),
NAMESPACE(), // ?
SPECIAL(Float.class),// prob not supported
BOOLEAN(Boolean.class, atom -> atom == 0xD, (b, i) -> b[i] == 1),
INTEGER(Integer.class, atom -> (int) ((atom & ByteUtils.ATOM_MASK) >> 3), ByteUtils::getInt),
DOUBLE(Double.class, atom -> Double.longBitsToDouble(atom & ByteUtils.ATOM_MASK), ByteUtils::getDouble);

private static final AtomKind[] VALUES = values();

private final Class<?> javaType;
private final LongFunction<Object> readAtom;
private final BiFunction<byte[], Integer, Object> readBuffer;

AtomKind() {
this(null);
}

AtomKind(Class<?> javaType) {
this(javaType, null, null);
}

AtomKind(Class<?> javaType, LongFunction<Object> readAtom, BiFunction<byte[], Integer, Object> readBuffer) {
this.javaType = javaType;
this.readAtom = readAtom;
this.readBuffer = readBuffer;
}

@SuppressWarnings("unchecked")
public <T> T readAtom(long atom, boolean threadSafe) {
if (threadSafe && this == STRING)
return (T) Main.API.readStringDirect(atom & ByteUtils.ATOM_MASK);
return (T) readAtom.apply(atom);
}

@SuppressWarnings("unchecked")
public <T> T readBuffer(byte[] buffer, int offset) {
return (T) readBuffer.apply(buffer, offset);
}

public boolean isNotSupported() {
return readAtom == null;
}

public static AtomKind of(long atom) {
int kind = (int) (atom & ByteUtils.ATOM_KIND);
if (kind >= VALUES.length) return UNUSED;

return VALUES[kind];
}

public static AtomKind of(Class<?> type) {
if (Updatable.class.isAssignableFrom(type)) return OBJECT;
for (AtomKind kind : VALUES) {
if (kind.javaType == type)
return kind;
}
return UNUSED;
}

public static boolean isNullAtom(long atom) {
return atom <= 4;
}

public static boolean isString(long atom) {
return AtomKind.of(atom) == STRING;
}
}
Loading

0 comments on commit 8c3522e

Please sign in to comment.