Last active
June 30, 2016 06:39
-
-
Save Jire/211edad18320ef4f22e02d732ad3fa7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import com.sun.jna.Native; | |
| import com.sun.jna.Pointer; | |
| import org.jire.kotmem.NativeBuffer; | |
| import org.jire.kotmem.Process; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| public final class KotmemJava { | |
| private static final ThreadLocal<Pointer> pointer = ThreadLocal.withInitial(() -> new Pointer(0)); | |
| private static final ThreadLocal<Map<Integer, NativeBuffer>> bufferByBytes = ThreadLocal.withInitial(HashMap::new); | |
| private KotmemJava() { | |
| throw new UnsupportedOperationException(); | |
| } | |
| public static Pointer cachedPointer(long address) { | |
| Pointer tlPointer = pointer.get(); | |
| Pointer.nativeValue(tlPointer, address); | |
| return tlPointer; | |
| } | |
| public static NativeBuffer cachedBuffer(int bytes) { | |
| Map<Integer, NativeBuffer> map = bufferByBytes.get(); | |
| NativeBuffer buffer = map.get(bytes); | |
| if (buffer == null) map.put(bytes, buffer = new NativeBuffer(bytes, Native.malloc(bytes))); | |
| return buffer; | |
| } | |
| public static byte getByte(Process process, long address) { | |
| return process.get(address, 1).getByte(0); | |
| } | |
| public static short getShort(Process process, long address) { | |
| return process.get(address, 2).getShort(0); | |
| } | |
| public static int getInt(Process process, long address) { | |
| return process.get(address, 4).getInt(0); | |
| } | |
| public static long getLong(Process process, long address) { | |
| return process.get(address, 8).getLong(0); | |
| } | |
| public static float getFloat(Process process, long address) { | |
| return process.get(address, 4).getFloat(0); | |
| } | |
| public static double getDouble(Process process, long address) { | |
| return process.get(address, 8).getDouble(0); | |
| } | |
| public static boolean getBoolean(Process process, long address) { | |
| return getByte(process, address) > 0; | |
| } | |
| public static void setByte(Process process, long address, int value) { | |
| NativeBuffer buffer = cachedBuffer(1); | |
| buffer.setByte(0, (byte) value); | |
| process.write(cachedPointer(address), buffer, 1); | |
| } | |
| public static void setShort(Process process, long address, int value) { | |
| NativeBuffer buffer = cachedBuffer(2); | |
| buffer.setShort(0, (short) value); | |
| process.write(cachedPointer(address), buffer, 2); | |
| } | |
| public static void setInt(Process process, long address, int value) { | |
| NativeBuffer buffer = cachedBuffer(4); | |
| buffer.setInt(0, value); | |
| process.write(cachedPointer(address), buffer, 4); | |
| } | |
| public static void setLong(Process process, long address, long value) { | |
| NativeBuffer buffer = cachedBuffer(8); | |
| buffer.setLong(0, value); | |
| process.write(cachedPointer(address), buffer, 8); | |
| } | |
| public static void setFloat(Process process, long address, float value) { | |
| NativeBuffer buffer = cachedBuffer(4); | |
| buffer.setFloat(0, value); | |
| process.write(cachedPointer(address), buffer, 4); | |
| } | |
| public static void setDouble(Process process, long address, double value) { | |
| NativeBuffer buffer = cachedBuffer(8); | |
| buffer.setDouble(0, value); | |
| process.write(cachedPointer(address), buffer, 8); | |
| } | |
| public static void setBoolean(Process process, long address, boolean value) { | |
| setByte(process, address, value ? 1 : 0); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment