Skip to content

Commit fdc4734

Browse files
committed
Applied suggestions
1 parent 08518d2 commit fdc4734

File tree

8 files changed

+82
-67
lines changed

8 files changed

+82
-67
lines changed

src/main/java/com/github/manolo8/darkbot/core/api/DarkBoatAdapter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.github.manolo8.darkbot.core.api.util.AbstractDataReader;
44
import com.github.manolo8.darkbot.core.api.util.DataReader;
5-
import com.github.manolo8.darkbot.core.api.util.DefaultDataReader;
65
import com.github.manolo8.darkbot.core.utils.ByteUtils;
76
import com.github.manolo8.darkbot.utils.StartupParams;
87
import eu.darkbot.api.DarkBoat;
@@ -53,7 +52,7 @@ public void setMaxFps(int maxFps) {
5352
}
5453

5554
@Override
56-
protected AbstractDataReader createReader(int idx) {
55+
protected DataReader createReader(int idx) {
5756
if (window.getVersion() >= 9)
5857
return new DarkBoatDataReader(idx, memory, stringReader);
5958

src/main/java/com/github/manolo8/darkbot/core/api/DarkBoatHookAdapter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.manolo8.darkbot.core.api;
22

3-
import com.github.manolo8.darkbot.core.api.util.AbstractDataReader;
4-
import com.github.manolo8.darkbot.core.api.util.DefaultDataReader;
3+
import com.github.manolo8.darkbot.core.api.util.DataReader;
54
import com.github.manolo8.darkbot.core.manager.HookAdapter;
65
import com.github.manolo8.darkbot.core.utils.ByteUtils;
76
import com.github.manolo8.darkbot.utils.StartupParams;
@@ -55,7 +54,7 @@ public boolean hasCapability(GameAPI.Capability capability) {
5554
}
5655

5756
@Override
58-
protected AbstractDataReader createReader(int idx) {
57+
protected DataReader createReader(int idx) {
5958
if (window.getVersion() >= 9)
6059
return new DarkBoatAdapter.DarkBoatDataReader(idx, memory, stringReader);
6160

src/main/java/com/github/manolo8/darkbot/core/api/GameAPIImpl.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class GameAPIImpl<
5353

5454
protected long lastFailedLogin;
5555

56-
protected AbstractDataReader[] dataReaders = new AbstractDataReader[10];
56+
protected DataReader[] dataReaders = new DataReader[10];
5757

5858
public GameAPIImpl(StartupParams params,
5959
W window, H handler, M memory, S stringReader, I interaction, D direct,
@@ -289,8 +289,8 @@ public DataReader readData(long address, int length) throws RuntimeException {
289289
throw new ArrayIndexOutOfBoundsException("Length is <= 0 or exceeds max chunk size: " + DataReader.MAX_CHUNK_SIZE);
290290

291291
for (int i = 0; i < dataReaders.length; i++) {
292-
AbstractDataReader reader = dataReaders[i];
293-
if (reader == null) reader = dataReaders[i] = createReader(i);
292+
AbstractDataReader reader = (AbstractDataReader) dataReaders[i];
293+
if (reader == null) reader = (AbstractDataReader) (dataReaders[i] = createReader(i));
294294

295295
Boolean result = reader.read(address, length);
296296

@@ -302,7 +302,7 @@ public DataReader readData(long address, int length) throws RuntimeException {
302302
if (result) return reader;
303303
}
304304

305-
throw new RuntimeException("Every reader is busy");
305+
throw new RuntimeException("All DataReaders are in use. Some code is calling readData and not closing the resource!");
306306
}
307307

308308
@Override
@@ -315,7 +315,7 @@ public boolean readData(long address, int length, Consumer<DataReader> reader) {
315315
return true;
316316
}
317317

318-
protected AbstractDataReader createReader(int idx) {
318+
protected DataReader createReader(int idx) {
319319
return new DefaultDataReader(memory, stringReader);
320320
}
321321

src/main/java/com/github/manolo8/darkbot/core/api/util/AbstractDataReader.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,21 @@
33
import com.github.manolo8.darkbot.core.api.GameAPI;
44
import com.github.manolo8.darkbot.core.utils.ByteUtils;
55

6+
import java.nio.ByteBuffer;
67
import java.util.concurrent.atomic.AtomicBoolean;
78

89
public abstract class AbstractDataReader implements DataReader {
910

1011
private final GameAPI.StringReader reader;
1112

12-
protected AtomicBoolean inUse = new AtomicBoolean();
13+
protected final AtomicBoolean inUse = new AtomicBoolean();
1314

1415
public AbstractDataReader(GameAPI.StringReader reader) {
1516
this.reader = reader;
1617
}
1718

19+
public abstract ByteBuffer getByteBuffer();
20+
1821
// if returns null, something bad happen while reading the data
1922
public abstract Boolean read(long address, int length);
2023

@@ -100,7 +103,7 @@ public long getPointer() {
100103

101104
@Override
102105
public long getPointer(int idx) {
103-
return getByteBuffer().getLong(idx) & ByteUtils.ATOM_MASK;
106+
return getByteBuffer().getLong(idx) & ByteUtils.ATOM_MASK;
104107
}
105108

106109
@Override

src/main/java/com/github/manolo8/darkbot/core/api/util/DataReader.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
package com.github.manolo8.darkbot.core.api.util;
22

3-
import java.nio.ByteBuffer;
4-
53
public interface DataReader extends AutoCloseable {
64

75
int MAX_CHUNK_SIZE = 2048;
86

9-
ByteBuffer getByteBuffer();
10-
117
int getPosition();
128
void setPosition(int pos);
139

src/main/java/com/github/manolo8/darkbot/core/objects/swf/AtomKind.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.manolo8.darkbot.core.objects.swf;
22

33
import com.github.manolo8.darkbot.Main;
4+
import com.github.manolo8.darkbot.core.itf.Updatable;
45
import com.github.manolo8.darkbot.core.utils.ByteUtils;
56

67
public enum AtomKind {
@@ -47,6 +48,7 @@ public static AtomKind of(long atom) {
4748
}
4849

4950
public static AtomKind of(Class<?> type) {
51+
//if (Updatable.class.isAssignableFrom(type)) return OBJECT;
5052
for (AtomKind kind : values()) {
5153
if (kind.javaType == type)
5254
return kind;

src/main/java/com/github/manolo8/darkbot/core/objects/swf/FlashDictionary.java renamed to src/main/java/com/github/manolo8/darkbot/core/objects/swf/FlashMap.java

Lines changed: 63 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,72 @@
11
package com.github.manolo8.darkbot.core.objects.swf;
22

33
import com.github.manolo8.darkbot.core.api.util.DataReader;
4+
import com.github.manolo8.darkbot.core.itf.Updatable;
45
import com.github.manolo8.darkbot.core.utils.ByteUtils;
5-
import com.github.manolo8.darkbot.core.utils.Lazy;
66

7-
import java.lang.reflect.ParameterizedType;
8-
import java.lang.reflect.Type;
7+
import java.lang.reflect.Array;
98
import java.util.Arrays;
10-
import java.util.HashMap;
119
import java.util.Map;
12-
import java.util.function.Consumer;
1310

1411
import static com.github.manolo8.darkbot.Main.API;
1512

16-
public abstract class FlashDictionary<K, V> extends SwfPtrCollection {
13+
// TODO unfished, dont use it
14+
15+
// tableOffset = API.readInt(address, 0x10, 0x28, 236);
16+
// isDictionary = (API.readInt(address, 0x10, 0x28, 248) & (1 << 4)) != 0; // need to read heap hashtable
17+
public class FlashMap<K, V> extends SwfPtrCollection {
1718

1819
private final AtomKind keyKind;
1920
private final AtomKind valueKind;
2021

21-
private final Map<K, Lazy<V>> listeners = new HashMap<>();
22+
private final boolean keyUpdatable, valueUpdatable;
23+
private final Class<K> keyClazz;
24+
private final Class<V> valueClazz;
2225

2326
private int size;
24-
private boolean autoUpdatable, ignoreEmpty = true;
25-
26-
private Entry[] entries = new Entry[0];
27+
private boolean autoUpdatable;
2728

2829
@SuppressWarnings("unchecked")
29-
private FlashDictionary() {
30-
Type[] types = ((ParameterizedType) getClass()
31-
.getGenericSuperclass()).getActualTypeArguments();
30+
private Entry[] entries = (Entry[]) Array.newInstance(Entry.class, 0);
31+
32+
private FlashMap(Class<K> key, Class<V> value) {
33+
this.keyClazz = key;
34+
this.valueClazz = value;
3235

33-
keyKind = AtomKind.of((Class<K>) types[0]);
34-
valueKind = AtomKind.of((Class<V>) types[1]);
36+
this.keyUpdatable = Updatable.class.isAssignableFrom(keyClazz);
37+
this.valueUpdatable = Updatable.class.isAssignableFrom(valueClazz);
38+
39+
this.keyKind = AtomKind.of(keyClazz);
40+
this.valueKind = AtomKind.of(valueClazz);
3541

3642
if (keyKind == null || keyKind == AtomKind.UNUSED || valueKind == null || valueKind == AtomKind.UNUSED)
3743
throw new IllegalArgumentException("Provided types are not supported");
3844
}
3945

40-
public static <K, V> FlashDictionary<K, V> ofDictionary() {
41-
return new FlashDictionary<K, V>() {};
46+
public static <K, V> FlashMap<K, V> of(Class<K> key, Class<V> value) {
47+
return new FlashMap<K, V>(key, value);
4248
}
4349

44-
public FlashDictionary<K, V> setAutoUpdatable(boolean updatable) {
50+
public FlashMap<K, V> setAutoUpdatable(boolean updatable) {
4551
this.autoUpdatable = updatable;
4652
return this;
4753
}
4854

49-
public FlashDictionary<K, V> setIgnoreEmpty(boolean ignoreEmpty) {
50-
this.ignoreEmpty = ignoreEmpty;
51-
return this;
52-
}
53-
54-
public void addListener(K key, Consumer<V> consumer) {
55-
addListener(key, consumer, true);
56-
}
57-
58-
public void addListener(K key, Consumer<V> consumer, boolean cacheValue) {
59-
this.listeners.computeIfAbsent(key, k -> (cacheValue ? new Lazy<>() : new Lazy.NoCache<>())).add(consumer);
60-
}
61-
62-
@SuppressWarnings("unchecked")
6355
@Override
6456
public void update() {
65-
if (address == 0 || (ignoreEmpty && listeners.isEmpty())) return;
57+
if (address == 0) return;
58+
59+
// heap hashtable
60+
boolean isDictionary = (API.readInt(address, 0x10, 0x28, 248) & (1 << 4)) != 0;
61+
int tableOffset = API.readInt(address, 0x10, 0x28, 236);
62+
63+
long table = address + tableOffset;
64+
if (isDictionary) table = API.readLong(table) + 8;
6665

67-
long hashTable = API.readLong(address + 32); //todo get table offset from script object traits
68-
if (hashTable == 0) return;
66+
if (table == 0) return;
6967

70-
long atoms = API.readLong(hashTable + 8);
71-
long sizeAndExp = API.readLong(hashTable + 16);
68+
long atoms = API.readLong(table);
69+
long sizeAndExp = API.readLong(table + 8);
7270

7371
//boolean hasIterIndex = (atoms & 0x04) != 0; //unused here
7472

@@ -95,10 +93,10 @@ public void update() {
9593
AtomKind keyKind = AtomKind.of(key);
9694
if (keyKind != this.keyKind) continue;
9795

98-
Entry<K, V> entry = entries[idx];
99-
if (entry == null) entries[idx] = entry = new Entry<>(listeners);
96+
Entry entry = entries[idx];
97+
if (entry == null) entries[idx] = entry = new Entry();
10098

101-
entry.set((K) keyKind.read(key), (V) valueKind.read(r.getLong()));
99+
entry.set(key, r.getLong());
102100
idx++;
103101
}
104102
}
@@ -123,18 +121,23 @@ public long getPtr(int i) {
123121
if (entry != null && entry.value instanceof Long)
124122
return (long) entry.value;
125123
}
126-
127124
return 0;
128125
}
129126

130-
private static class Entry<K, V> {
131-
private final Map<K, Lazy<V>> listeners;
132127

128+
public class Entry implements Map.Entry<K, V> {
133129
private K key;
134130
private V value;
135131

136-
public Entry(Map<K, Lazy<V>> listeners) {
137-
this.listeners = listeners;
132+
private Entry() {
133+
try {
134+
if (keyUpdatable)
135+
key = keyClazz.newInstance();
136+
if (valueUpdatable)
137+
value = valueClazz.newInstance();
138+
} catch (InstantiationException | IllegalAccessException e) {
139+
throw new RuntimeException(e);
140+
}
138141
}
139142

140143
public K getKey() {
@@ -145,12 +148,22 @@ public V getValue() {
145148
return value;
146149
}
147150

148-
private void set(K key, V value) {
149-
this.key = key;
150-
this.value = value;
151+
@Override
152+
public V setValue(V value) {
153+
return null;
154+
}
151155

152-
Lazy<V> l = listeners.get(key);
153-
if (l != null) l.send(value);
156+
@SuppressWarnings("unchecked")
157+
private void set(long keyAtom, long valueAtom) {
158+
if (keyUpdatable) {
159+
((Updatable) key).update(keyAtom & ByteUtils.ATOM_MASK);
160+
if (autoUpdatable) ((Updatable) key).update();
161+
} else key = (K) keyKind.read(keyAtom);
162+
163+
if (valueUpdatable) {
164+
((Updatable) value).update(valueAtom & ByteUtils.ATOM_MASK);
165+
if (autoUpdatable) ((Updatable) value).update();
166+
} else value = (V) valueKind.read(valueAtom);
154167
}
155168

156169
@Override

src/main/java/com/github/manolo8/darkbot/utils/debug/SWFUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.manolo8.darkbot.utils.debug;
22

3+
import com.github.manolo8.darkbot.core.api.util.AbstractDataReader;
34
import com.github.manolo8.darkbot.core.api.util.DataReader;
45

56
import java.io.FileOutputStream;
@@ -19,7 +20,9 @@ public static void dumpMainSWF() {
1920

2021
try (FileOutputStream writer = new FileOutputStream("main.swf")) {
2122
for (int i = 0; i < size; i += DataReader.MAX_CHUNK_SIZE) {
22-
try (DataReader reader = API.readData(addr + i, Math.min(DataReader.MAX_CHUNK_SIZE, size - i))) {
23+
try (AbstractDataReader reader = (AbstractDataReader) API.readData(addr + i,
24+
Math.min(DataReader.MAX_CHUNK_SIZE, size - i))) {
25+
2326
writer.getChannel().write(reader.getByteBuffer(), i);
2427
}
2528
}

0 commit comments

Comments
 (0)