Skip to content

Commit

Permalink
Add custom string compression, move custom tips API to a new class, m…
Browse files Browse the repository at this point in the history
…inify the json message format

 - the string compression algorithm tries to replace duplicate words (space delimited strings) with short
   tokens. It is used to compress the power tips section

 - also most of the prints were disabled, stuff should print only if something's wrong
  • Loading branch information
avolny committed Apr 5, 2020
1 parent c4f2698 commit 4d65b2d
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 208 deletions.
21 changes: 15 additions & 6 deletions src/main/java/str_exporter/BackendBroadcaster.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedList;
import java.util.concurrent.locks.ReentrantLock;

public class BackendBroadcaster {

public static final Logger logger = LogManager.getLogger(BackendBroadcaster.class.getName());

private static final String EBS_URL = "https://localhost:8081";
// private static final String EBS_URL = "https://slaytherelics.xyz:8081";
// private static final String EBS_URL = "https://localhost:8081";
private static final String EBS_URL = "https://slaytherelics.xyz:8081";

private static final long CHECK_QUEUE_PERIOD_MILLIS = 100;
private static BackendBroadcaster instance = new BackendBroadcaster();

// public static long encodingDelay = 0;
private String message;
private long messageTimestamp;
private ReentrantLock queueLock;
Expand Down Expand Up @@ -91,6 +89,13 @@ private static String injectDelayToMessage(String msg, long delay) {
return "{\"d\":" + delay + "," + msg.substring(1);
}

private static String compressPowerTips(String msg) {
int index = msg.lastIndexOf("\"w\":\"");

String comp_powertips = StringCompression.compress(msg.substring(index + 5, msg.length() - 3));
return msg.substring(0, index + 5) + comp_powertips + "\"}}";
}

private void broadcastMessage(String msg, long msgTimestamp) {

try {
Expand All @@ -101,6 +106,7 @@ private void broadcastMessage(String msg, long msgTimestamp) {
con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);

msg = compressPowerTips(msg);
long msgDelay = msgTimestamp - System.currentTimeMillis() + SlayTheRelicsExporter.delay;
msg = injectDelayToMessage(msg, msgDelay);

Expand All @@ -115,11 +121,14 @@ private void broadcastMessage(String msg, long msgTimestamp) {
response.append(responseLine.trim());
}

logger.info(msg);
logger.info("broadcasted message, response: " + response.toString());
if (!response.toString().equals("Success"))
logger.info("message not broadcasted succesfully, response: " + response.toString());
// logger.info("broadcasted message, response: " + response.toString());

} catch (Exception e) {
e.printStackTrace();
} finally {
// logger.info(SlayTheRelicsExporter.removeSecret(msg));
}
}
}
135 changes: 135 additions & 0 deletions src/main/java/str_exporter/CustomTipsAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package str_exporter;

import basemod.BaseMod;
import basemod.interfaces.PostInitializeSubscriber;
import com.evacipated.cardcrawl.modthespire.Loader;
import com.evacipated.cardcrawl.modthespire.ModInfo;
import com.evacipated.cardcrawl.modthespire.Patcher;
import com.evacipated.cardcrawl.modthespire.lib.SpireInitializer;
import com.megacrit.cardcrawl.helpers.Hitbox;
import com.megacrit.cardcrawl.helpers.PowerTip;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Set;

@SpireInitializer
public class CustomTipsAPI implements PostInitializeSubscriber {

private static final String CUSTOM_TIP_HITBOX_NAME = "slayTheRelicsHitboxes";
private static final String CUSTOM_TIP_POWERTIPS_NAME = "slayTheRelicsPowerTips";

private ArrayList<Field> externalHitboxFields = new ArrayList<>();
private ArrayList<Field> externalPowerTipsFields = new ArrayList<>();

private static Logger logger = SlayTheRelicsExporter.logger;

public static CustomTipsAPI instance;

private CustomTipsAPI() {
BaseMod.subscribe(this);
}

public static void initialize() {
instance = new CustomTipsAPI();
}

@Override
public void receivePostInitialize() {
findCustomTipImplementingClasses();
}

private void findCustomTipImplementingClasses() {
logger.info("CHECKING FOR OTHER MODS THAT IMPLEMENT CUSTOM TIPS API");

ClassLoader loader = SlayTheRelicsExporter.class.getClassLoader();

for (ModInfo info : Loader.MODINFOS) {
if (Patcher.annotationDBMap.containsKey(info.jarURL)) {
Set<String> initializers = Patcher.annotationDBMap.get(info.jarURL).getAnnotationIndex().get(SpireInitializer.class.getName());
if (initializers != null) {
System.out.println(" - " + info.Name);
for (String initializer : initializers) {
System.out.println(" - " + initializer);
try {
long startTime = System.nanoTime();

Class<?> c = loader.loadClass(initializer);

try {
Field hitboxes = c.getField(CUSTOM_TIP_HITBOX_NAME);
Field powerTips = c.getField(CUSTOM_TIP_POWERTIPS_NAME);

if (hitboxes.getType() == ArrayList.class && powerTips.getType() == ArrayList.class) {
externalHitboxFields.add(hitboxes);
externalPowerTipsFields.add(powerTips);
logger.info("Fields found for class " + c.getCanonicalName());
}
} catch (NoSuchFieldException e) {
logger.info("No fields found for class " + c.getCanonicalName());
}

long endTime = System.nanoTime();
long duration = endTime - startTime;
logger.info(" - " + (duration / 1000000) + "ms");
} catch (ClassNotFoundException e) {
logger.info("WARNING: Unable to find method initialize() on class marked @SpireInitializer: " + initializer);
}
}
}
} else {
System.err.println(info.jarURL + " Not in DB map. Something is very wrong");
}
}
}

public Object[] getTipsFromMods() {

LinkedList<Hitbox> hitboxes = new LinkedList<>();
LinkedList<ArrayList<PowerTip>> tip_lists = new LinkedList<>();

for (int i = 0; i < externalHitboxFields.size(); i++) {

try {

// logger.info("class: " + hitbox_fields.get(i).getDeclaringClass().getSimpleName());

ArrayList<Hitbox> mod_hitboxes = (ArrayList<Hitbox>) externalHitboxFields.get(i).get(null);
ArrayList<ArrayList<PowerTip>> mod_tip_lists = (ArrayList<ArrayList<PowerTip>>) externalPowerTipsFields.get(i).get(null);

if (mod_hitboxes.size() == mod_tip_lists.size()) {
// logger.info("found fields, adding " + mod_hitboxes.size() + " entries");
hitboxes.addAll(mod_hitboxes);
tip_lists.addAll(removeImagesFromPowerTipsLists(mod_tip_lists));
} else {
// logger.info("hitboxes and powertip list don't have the same size for class: " + hitbox_fields.get(i).getDeclaringClass().getSimpleName());
}

} catch (IllegalAccessException e) {
e.printStackTrace();
}
}

return new Object[]{hitboxes, tip_lists};
}



private static ArrayList<ArrayList<PowerTip>> removeImagesFromPowerTipsLists(ArrayList<ArrayList<PowerTip>> tip_lists) {
// removes any images
ArrayList<ArrayList<PowerTip>> new_tip_lists = new ArrayList<>(tip_lists.size());

for (int i = 0; i < tip_lists.size(); i++) {
ArrayList<PowerTip> list = tip_lists.get(i);

new_tip_lists.add(new ArrayList<>(list.size()));
for (int j = 0; j < list.size(); j++) {
new_tip_lists.get(i).add(new PowerTip(list.get(j).header, list.get(j).body));
}
}

return new_tip_lists;
}
}
Loading

0 comments on commit 4d65b2d

Please sign in to comment.