Skip to content

Commit

Permalink
Add one new mod bookkeeper
Browse files Browse the repository at this point in the history
  • Loading branch information
yihleego committed Dec 8, 2023
1 parent 81b754c commit e8ce4ed
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 1 deletion.
2 changes: 2 additions & 0 deletions bookkeeper/bookkeeper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
endpoint: https://api.ball.one/balances
filename: ./bookkeeper.txt
99 changes: 99 additions & 0 deletions bookkeeper/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.spiralstudio.mod</groupId>
<artifactId>root</artifactId>
<version>1.0.0</version>
</parent>

<artifactId>bookkeeper</artifactId>
<version>1.0.0</version>
<name>Bookkeeper</name>

<properties>
<mainClass>com.spiralstudio.mod.bookkeeper.Main</mainClass>
</properties>

<dependencies>
<dependency>
<groupId>com.threerings</groupId>
<artifactId>projectx-pcode</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/../lib/projectx-pcode.jar</systemPath>
</dependency>
<dependency>
<groupId>com.spiralstudio.mod</groupId>
<artifactId>core</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
<manifestEntries>
<Name>${project.name}</Name>
<Version>${project.version}</Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>${mainClass}</mainClass>
</manifest>
<manifestEntries>
<Name>${project.name}</Name>
<Version>${project.version}</Version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.spiralstudio.mod.bookkeeper;

import com.spiralstudio.mod.core.FileUtils;
import com.spiralstudio.mod.core.HttpUtils;
import com.spiralstudio.mod.core.JsonUtils;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author Leego Yih
*/
public class BookkeeperHandler {
public static String endpoint = "";
public static String filename = "";
public static final ExecutorService executor = Executors.newSingleThreadExecutor();

public static void dump(com.threerings.projectx.util.A ctx, String type, int value) {
String name = getKnightName(ctx);
if (name != null) {
executor.submit(() -> {
callAPI(name, type, value);
writeFile(name, type, value);
});
}
}

public static void callAPI(String name, String type, int value) {
String body = null;
try {
if (endpoint == null || endpoint.isEmpty()) {
return;
}
Map<String, Object> obj = new HashMap<>();
obj.put("knight", name);
obj.put("type", type);
obj.put("value", value);
obj.put("timestamp", System.currentTimeMillis());
body = JsonUtils.toString(obj);
HttpUtils.post(endpoint, Collections.emptyMap(), body);
} catch (Exception e) {
System.out.println("[Bookkeeper] Failed to call api: " + endpoint + ", data: " + body);
e.printStackTrace();
}
}

public static void writeFile(String name, String type, int value) {
String body = null;
try {
if (filename == null || filename.isEmpty()) {
return;
}
body = "[" + LocalDateTime.now() + "] " + name + ": " + type + "=" + value + "\n";
FileUtils.write(filename, body + "\n", true);
} catch (Exception e) {
System.out.println("[Bookkeeper] Failed to write file: " + filename + ", data: " + body);
e.printStackTrace();
}
}

private static String getKnightName(com.threerings.projectx.util.A ctx) {
Object name = null;
try {
Field field = Class.forName("com.threerings.util.Name").getDeclaredField("_name");
field.setAccessible(true);
name = field.get(ctx.uk().knight);
} catch (Exception e) {
System.out.println("[Bookkeeper] Failed to get knight name");
}
if (name != null) {
return (String) name;
} else {
return null;
}
}
}
71 changes: 71 additions & 0 deletions bookkeeper/src/main/java/com/spiralstudio/mod/bookkeeper/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.spiralstudio.mod.bookkeeper;

import com.spiralstudio.mod.core.ClassPool;
import com.spiralstudio.mod.core.Configs;
import com.spiralstudio.mod.core.MethodModifier;
import com.spiralstudio.mod.core.Registers;
import lombok.Data;

/**
* @author Leego Yih
* @see com.threerings.projectx.client.aq EnergyPanel
* @see com.threerings.projectx.item.client.g BaseItemListPanel
*/
public class Main {
private static boolean mounted = false;

static {
Registers.add(Main.class);
}

public static void mount() {
if (mounted) {
return;
}
mounted = true;
Config config = getConfig();
initConfig(config);
redefineEnergyPanel();
redefineBaseItemListPanel();
}

static void initConfig(Config config) {
if (config == null) {
return;
}
BookkeeperHandler.endpoint = config.endpoint;
BookkeeperHandler.filename = config.filename;
}

static void redefineEnergyPanel() {
ClassPool.from("com.threerings.projectx.client.aq")
.modifyMethod(new MethodModifier()
.methodName("uv")
.insertAfter("com.spiralstudio.mod.bookkeeper.BookkeeperHandler.dump(this._ctx, \"energy\", this._ctx.uk().energy);"));
}

static void redefineBaseItemListPanel() {
ClassPool.from("com.threerings.projectx.item.client.g")
.modifyMethod(new MethodModifier()
.methodName("updateCrowns")
.insertAfter("com.spiralstudio.mod.bookkeeper.BookkeeperHandler.dump(this._ctx, \"crowns\", this._ctx.uk().crowns);"));
}

static Config getConfig() {
try {
return Configs.readYaml("bookkeeper.yml", Config.class);
} catch (Exception e) {
System.out.println("Failed to read config: " + e.getMessage());
return null;
}
}

@Data
public static class Config {
private String endpoint;
private String filename;
}

public static void main(String[] args) {
}
}
9 changes: 9 additions & 0 deletions bookkeeper/src/main/resources/mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"mod": {
"name": "Bookkeeper",
"description": "",
"author": "Leego",
"version": "1.0.0",
"compatibility": "Yes"
}
}
6 changes: 5 additions & 1 deletion core/src/main/java/com/spiralstudio/mod/core/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
public final class FileUtils {

public static void write(String filename, String content) throws IOException {
write(filename, content, false);
}

public static void write(String filename, String content, boolean append) throws IOException {
File file = new File(filename);
File dir = file.getParentFile();
if (!dir.exists()) {
Expand All @@ -18,7 +22,7 @@ public static void write(String filename, String content) throws IOException {
throw new IOException("Failed to create dir: " + dir.getAbsolutePath());
}
}
try (FileWriter writer = new FileWriter(file)) {
try (FileWriter writer = new FileWriter(file, append)) {
writer.write(content);
writer.flush();
}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<module>simplifiedchinese</module>
<module>traditionalchinese</module>
<module>hidename</module>
<module>bookkeeper</module>
<module>demo</module>
<module>test</module>
</modules>
Expand Down

0 comments on commit e8ce4ed

Please sign in to comment.