Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Runemoro committed Jan 14, 2019
1 parent e665e0f commit 8112ebe
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 78 deletions.
13 changes: 1 addition & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ buildscript {
maven { url = 'https://maven.fabricmc.net/' }
}
dependencies {
classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
classpath "net.fabricmc:fabric-loom:0.2.0-SNAPSHOT"
}
}

apply plugin: "java"
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: net.fabricmc.loom.LoomGradlePlugin

sourceCompatibility = 1.8
Expand All @@ -26,21 +24,13 @@ dependencies {
def travisBuildNumber = System.getenv("TRAVIS_BUILD_NUMBER")
def versionSuffix = travisBuildNumber != null ? travisBuildNumber : "SNAPSHOT"

version "1.0-$versionSuffix"
version "1.0"
group "org.dimdev.toomanycrashes"
archivesBaseName = "TooManyCrashes"

sourceCompatibility = 1.8
targetCompatibility = 1.8

jar {
classifier "thin"
}

shadowJar {
classifier ""
}

processResources {
filesMatching("fabric.mod.json") {
expand "version": project.version
Expand All @@ -56,6 +46,5 @@ task sourcesJar(type: Jar, dependsOn: classes) {

artifacts {
archives jar
archives shadowJar
archives sourcesJar
}
13 changes: 7 additions & 6 deletions src/main/java/org/dimdev/toomanycrashes/ModConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.dimdev.toomanycrashes;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.fabricmc.loader.FabricLoader;

import java.io.*;

public class ModConfig {
public static final File CONFIG_FILE = new File(FabricLoader.INSTANCE.getConfigDirectory(), "toomanycrashes.json");
public static final Gson GSON = new Gson();
private static ModConfig instance = new ModConfig();
private static final File CONFIG_FILE = new File(FabricLoader.INSTANCE.getConfigDirectory(), "toomanycrashes.json");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static ModConfig instance = null;

public String hasteURL = "https://paste.dimdev.org";
public boolean disableReturnToMainMenu = false;
Expand All @@ -28,10 +29,10 @@ public static ModConfig instance() {

instance = new ModConfig();

try {
GSON.toJson(instance, new FileWriter(CONFIG_FILE));
try (FileWriter writer = new FileWriter(CONFIG_FILE)) {
GSON.toJson(instance, writer);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}

return instance;
Expand Down
83 changes: 38 additions & 45 deletions src/main/java/org/dimdev/toomanycrashes/StacktraceDeobfuscator.java
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
package org.dimdev.toomanycrashes;

import java.io.File;
import net.fabricmc.tinyremapper.TinyUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.io.InputStreamReader;
import java.util.*;

public final class StacktraceDeobfuscator {
private static final String MAPPINGS_URL = "https://gist.githubusercontent.com/Runemoro/cc6ad843f5403b870214ae34baaa4b60/raw/c7be9a0d5be49eaa365f915fd6326c1ddd419bbd/yarn-mappings.csv";
private static final boolean DEBUG_IN_DEV = false; // Makes this deobf -> obf for testing in dev. Don't forget to set to false when done!
private static HashMap<String, String> mappings = null;

/**
* If the file does not exits, downloads latest method mappings and saves them to it.
* Initializes a HashMap between obfuscated and deobfuscated names from that file.
*/
public static void init(File mappingsFile) {
if (mappings != null) return;

// Download the file if necessary
if (!mappingsFile.exists()) {
try {
try (InputStream is = new URL(MAPPINGS_URL).openStream()) {
Files.copy(is, mappingsFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

// Read the mapping
HashMap<String, String> mappings = new HashMap<>();
try (Scanner scanner = new Scanner(mappingsFile)) {
scanner.nextLine(); // Skip CSV header
while (scanner.hasNext()) {
String[] mappingLine = scanner.nextLine().split(",");
String obfName = mappingLine[0];
String deobfName = mappingLine[1];
public static final String MAPPINGS = "mappings/mappings.tiny";
private static Map<String, String> mappings = null;

if (!DEBUG_IN_DEV) {
mappings.put(obfName, deobfName);
} else {
mappings.put(deobfName, obfName);
}
}
public static void init() {
Map<String, String> mappings = new HashMap<>();
try (BufferedReader mappingReader = new BufferedReader(new InputStreamReader(StacktraceDeobfuscator.class.getClassLoader().getResourceAsStream(MAPPINGS)))) {
TinyUtils.read(
mappingReader,
"intermediary",
"named",
(key, value) -> mappings.put(key.replace('/', '.'), value.replace('/', '.')),
(intermediary, named) -> mappings.put(intermediary.name, named.name),
(intermediary, named) -> mappings.put(intermediary.name, named.name)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -71,18 +47,35 @@ public static StackTraceElement[] deobfuscateStacktrace(StackTraceElement[] stac

int index = 0;
for (StackTraceElement el : stackTrace) {
String remappedClass = mappings.get(el.getClassName());
String remappedMethod = mappings.get(el.getMethodName());
stackTrace[index++] = new StackTraceElement(
mappings.getOrDefault(el.getClassName(), el.getClassName()),
mappings.getOrDefault(el.getMethodName(), el.getMethodName()),
el.getFileName(),
remappedClass != null ? remappedClass : el.getClassName(),
remappedMethod != null ? remappedMethod : el.getMethodName(),
remappedClass != null ? getFileName(remappedClass) : el.getFileName(),
el.getLineNumber()
);
}
return stackTrace;
}

public static String getFileName(String className) {
String remappedFile = className;
int lastDot = className.lastIndexOf('.');
if (lastDot != -1) {
remappedFile = remappedFile.substring(lastDot + 1);
}

int firstDollar = className.indexOf('$');
if (firstDollar != -1) {
remappedFile = remappedFile.substring(0, firstDollar);
}

return remappedFile;
}

public static void main(String[] args) {
init(new File("mappings.csv"));
init();
for (Map.Entry<String, String> entry : mappings.entrySet()) {
System.out.println(entry.getKey() + " <=> " + entry.getValue());
}
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/org/dimdev/toomanycrashes/TooManyCrashes.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.dimdev.toomanycrashes;

import net.fabricmc.loader.FabricLoader;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.utils.SSLUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
Expand All @@ -15,7 +13,6 @@

public class TooManyCrashes {
private static final Logger LOGGER = LogManager.getLogger("TooManyCrashes");
private static final long MAPPINGS_CACHE_DURATION = 2 * 24 * 60 * 60 * 1000;

public static void init() {
ModConfig.instance();
Expand All @@ -35,20 +32,11 @@ private static void trustIdenTrust() {
}

private static void initStacktraceDeobfuscator() {
File modDir = new File(FabricLoader.INSTANCE.getConfigDirectory(), "toomanycrashes");
modDir.mkdirs();

LOGGER.info("Initializing StacktraceDeobfuscator");
try {
File mappings = new File(modDir, "mappings-" + System.currentTimeMillis() / MAPPINGS_CACHE_DURATION + ".csv");
if (mappings.exists()) {
LOGGER.info("Found mappings: " + mappings.getName());
} else {
LOGGER.info("Downloading latest mappings to: " + mappings.getName());
}
StacktraceDeobfuscator.init(mappings);
StacktraceDeobfuscator.init();
} catch (Exception e) {
LOGGER.error("Failed to get mappings!", e);
LOGGER.error("Failed to load mappings!", e);
}
LOGGER.info("Done initializing StacktraceDeobfuscator");

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mixins.toomanycrashes.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"package": "org.dimdev.toomanycrashes.mixins",
"required": true,
"refmap": "mixins.toomanycrashes.refmap.json",
"refmap": "TooManyCrashes-refmap.json",
"target": "@env(DEFAULT)",
"minVersion": "0.6",
"compatibilityLevel": "JAVA_8",
Expand Down

0 comments on commit 8112ebe

Please sign in to comment.