Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/1.19.4-fabric' into 1.20-fabric
Browse files Browse the repository at this point in the history
# Conflicts:
#	gradle.properties
  • Loading branch information
melontini committed Jul 19, 2023
2 parents b326a05 + 661e2f3 commit d999ca0
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 71 deletions.
15 changes: 6 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
## What's new:

### Content
### Analytics

* Fixed item groups not having a name by default. (1.19.3+)
* New analytics module! `analytics-crashes`
* * Adds event-like crash handling. Also, features a class to quickly upload logs to mclo.gs
* Analytics config moved to JSON + new option for Crashlytics.

### Danger
### Minecraft

* Fixed InstrumentationAccess failing to locate the agent jar.

### Misc

* Moved icons into `assets/dark-matter*`.
* Dummy no longer depends on MC version.
* Fixed matrices not getting popped after rendering tooltips. (1.20+)
41 changes: 4 additions & 37 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,11 @@

The invisible force that holds galaxies together.

[![](https://img.shields.io/modrinth/dt/qcz8IJ9L?label=modrinth)](https://modrinth.com/mod/dark-matter)
[![CurseForge](https://cf.way2muchnoise.eu/title/839908.svg)](https://www.curseforge.com/minecraft/mc-mods/dark-matter)
[![](https://jitpack.io/v/me.melontini/dark-matter.svg)](https://jitpack.io/#me.melontini/dark-matter)
![GitHub](https://img.shields.io/github/license/melontini/dark-matter)

### Want to use this in your project?

` for some reason `

First, add JitPack to your repositories.

```groovy
repositories {
maven {
url 'https://jitpack.io'
}
}
```

and then

Since `0.3.0` Dark Matter uses modules.

```groovy
dependencies {
//other dependencies...
//dark-matter-base is required by all modules.
modImplementation include("me.melontini.dark-matter:dark-matter-base:${project.dark_matter}")
//or com.github.melontini.dark-matter
//if you need dark-matter-recipe-book, you also have to include its dependencies.
//such as dark-matter-enums
modImplementation include("me.melontini.dark-matter:dark-matter-recipe-book:${project.dark_matter}")
modImplementation include("me.melontini.dark-matter:dark-matter-enums:${project.dark_matter}")
//alternatively, you can include all modules like this:
modImplementation include("me.melontini.dark-matter:dark-matter:${project.dark_matter}")
//other dependencies...
}
```
See [the wiki](https://github.com/melontini/dark-matter/wiki) for a detailed explanation of each module.

You can find all the tags and modules here: https://jitpack.io/#me.melontini/dark-matter
3 changes: 3 additions & 0 deletions dark-matter-analytics-crashes/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dependencies {
api(project(path: ':dark-matter-analytics', configuration: 'namedElements'))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.melontini.dark_matter.analytics.crashes;

import me.melontini.dark_matter.util.classes.Tuple;
import net.fabricmc.api.EnvType;
import net.minecraft.util.crash.CrashReport;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class Crashlytics {
private static final Map<String, Tuple<Decider, Handler>> HANDLERS = new HashMap<>();
public static void addHandler(String id, Decider decider, Handler handler) {
HANDLERS.putIfAbsent(id, new Tuple<>(decider, handler));
}

public static void removeHandler(String id) {
HANDLERS.remove(id);
}

public static Collection<Tuple<Decider, Handler>> getHandlers() {
return Collections.unmodifiableCollection(HANDLERS.values());
}

@FunctionalInterface
public interface Decider {
boolean shouldHandle(CrashReport report, Throwable cause, @Nullable String latestLog, EnvType envType);
}

@FunctionalInterface
public interface Handler {
void handle(CrashReport report, Throwable cause, @Nullable String latestLog, EnvType envType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.melontini.dark_matter.analytics.crashes;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.melontini.dark_matter.DarkMatterLog;

import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;

public class Uploader {
private static final HttpClient CLIENT = HttpClient.newHttpClient();
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
private static final String MCLO_GS_API = "https://api.mclo.gs/1/log";

public static String uploadToMclo_gs(String log) {
try {
HttpResponse<String> response = CLIENT.send(HttpRequest.newBuilder()
.uri(URI.create(MCLO_GS_API))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString("content=" + URLEncoder.encode(log, StandardCharsets.UTF_8)))
.build(), HttpResponse.BodyHandlers.ofString());

JsonObject jResponse = JsonParser.parseString(response.body()).getAsJsonObject();
boolean success = jResponse.get("success").getAsBoolean();

if (success) return jResponse.get("url").getAsString();
else throw new RuntimeException(jResponse.get("error").getAsString());
} catch (IOException | InterruptedException e) {
DarkMatterLog.error("Failed to upload log to mclo.gs!", e);
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package me.melontini.dark_matter.analytics.crashes.mixin;

import me.melontini.dark_matter.analytics.Analytics;
import me.melontini.dark_matter.analytics.crashes.Crashlytics;
import me.melontini.dark_matter.util.classes.Tuple;
import net.fabricmc.api.EnvType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.util.crash.CrashReport;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@Mixin(CrashReport.class)
public abstract class CrashReportMixin {
@Shadow @Final private Throwable cause;

@Inject(at = @At("RETURN"), method = "writeToFile")
private void dark_matter$handleCrash(File file, CallbackInfoReturnable<Boolean> cir) {
if (Analytics.handleCrashes()) {
EnvType envType = FabricLoader.getInstance().getEnvironmentType();
String latestLog = null;
try {
latestLog = Files.readString(FabricLoader.getInstance().getGameDir().resolve("logs/latest.log"));
} catch (IOException ignored) {}

for (Tuple<Crashlytics.Decider, Crashlytics.Handler> tuple : Crashlytics.getHandlers()) {
if (tuple.left().shouldHandle((CrashReport) (Object) this, this.cause, latestLog, envType)) {
tuple.right().handle((CrashReport) (Object) this, this.cause, latestLog, envType);
}
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"required": true,
"minVersion": "0.8",
"package": "me.melontini.dark_matter.analytics.crashes.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"CrashReportMixin"
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}
31 changes: 31 additions & 0 deletions dark-matter-analytics-crashes/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"schemaVersion": 1,
"id": "dark-matter-analytics-crashes",
"version": "${version}",
"name": "Dark Matter Analytics Crashes",
"description": "Handling crashes (real)",
"authors": [
"melontini"
],
"contact": {},
"license": "MIT",
"icon": "assets/dark-matter-analytics-crashes/icon.png",
"environment": "*",
"entrypoints": {
},
"mixins": [
"dark-matter-analytics-crashes.mixin.json"
],
"depends": {
"fabricloader": "*",
"minecraft": "*",
"dark-matter-base": "*",
"dark-matter-analytics": "*"
},
"custom": {
"modmenu": {
"parent": "dark-matter",
"badges": [ "library" ]
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package me.melontini.dark_matter.analytics;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.melontini.dark_matter.DarkMatterLog;
import net.fabricmc.loader.api.FabricLoader;

Expand All @@ -9,57 +11,90 @@
import java.util.Properties;
import java.util.UUID;

@SuppressWarnings("unused")

public class Analytics {
private static final UUID nullID = new UUID(0,0);
private static UUID userUUID = nullID;
private static boolean enabled = true;
private static final Config CONFIG;
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

private static final Path CU_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("cracker-util/analytics.properties");
private static final Path OLD_CONFIG_PATH = FabricLoader.getInstance().getConfigDir().resolve("dark-matter/analytics.properties");

static {
Properties properties = new Properties();
Path oldConfig = FabricLoader.getInstance().getConfigDir().resolve("cracker-util/analytics.properties");
Path config = FabricLoader.getInstance().getConfigDir().resolve("dark-matter/analytics.properties");
if (Files.exists(oldConfig)) {
moveConfigIfExists();
CONFIG = loadConfig();
}

private static void moveConfigIfExists() {
if (Files.exists(CU_CONFIG_PATH)) {
try {
if (!Files.exists(config.getParent())) Files.createDirectories(config.getParent());
if (!Files.exists(OLD_CONFIG_PATH.getParent())) Files.createDirectories(OLD_CONFIG_PATH.getParent());
DarkMatterLog.info("Found old config at config/cracker_analytics.properties, moving to config/dark-matter/analytics.properties");
Files.move(oldConfig, config);
Files.move(CU_CONFIG_PATH, OLD_CONFIG_PATH);
} catch (IOException e) {
DarkMatterLog.error("Couldn't move old config!", e);
}
}
}

if (Files.exists(config)) {
private static void upgradeToJson(Config config) {
if (Files.exists(OLD_CONFIG_PATH)) {
Properties properties = new Properties();
try {
properties.load(Files.newInputStream(config));
enabled = Boolean.parseBoolean(properties.getProperty("enabled"));
if (isEnabled()) userUUID = UUID.fromString(properties.getProperty("user_id"));
properties.load(Files.newInputStream(OLD_CONFIG_PATH));
config.enabled = Boolean.parseBoolean(properties.getProperty("enabled"));
config.userUUID = UUID.fromString(properties.getProperty("user_id"));

Files.deleteIfExists(OLD_CONFIG_PATH);
} catch (IOException e) {
DarkMatterLog.error("Could not read analytics properties", e);
}
}
}

private static Config loadConfig() {
Config config = new Config();
upgradeToJson(config);
Path configPath = FabricLoader.getInstance().getConfigDir().resolve("dark-matter/analytics.json");
if (Files.exists(configPath)) {
try {
config = GSON.fromJson(Files.newBufferedReader(configPath), Config.class);
Files.write(configPath, GSON.toJson(config).getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
enabled = true;
properties.setProperty("enabled", "true");
userUUID = UUID.randomUUID();
properties.setProperty("user_id", userUUID.toString());
if (!Files.exists(config.getParent())) Files.createDirectories(config.getParent());
properties.store(Files.newOutputStream(config), "Dark Matter analytics properties");
Files.createDirectories(configPath.getParent());
Files.createFile(configPath);
Files.write(configPath, GSON.toJson(config).getBytes());
} catch (IOException e) {
DarkMatterLog.error("Could not write analytics properties", e);
throw new RuntimeException(e);
}
}
if (!config.enabled) config.userUUID = nullID;
return config;
}

public static UUID getUUID() {
return userUUID;
return CONFIG.userUUID;
}

public static String getUUIDString() {
return userUUID.toString();
return CONFIG.userUUID.toString();
}

public static boolean isEnabled() {
return enabled;
return CONFIG.enabled;
}

public static boolean handleCrashes() {
return CONFIG.crashesEnabled;
}

public static class Config {
public boolean enabled = true;
public boolean crashesEnabled = true;
public UUID userUUID = nullID;
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ minecraft_version=1.20
yarn_mappings=1.20+build.1
loader_version=0.14.19
# Mod Properties
mod_version=0.6.2-1.20
mod_version=0.7.0-1.20
maven_group=me.melontini
archives_base_name=dark-matter
# Dependencies
# check this on https://modmuss50.me/fabric.html
mixin_extras_version=0.2.0-beta.8
mixin_extras_version=0.2.0-beta.9
byte_buddy_agent_version=1.14.2
mixpanel_version=1.5.1
org_json_version=20230227
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ rootProject.name = "dark-matter"

include("${rootProject.name}-base")
include("${rootProject.name}-analytics")
include("${rootProject.name}-analytics-crashes")
include("${rootProject.name}-analytics-mixpanel")
include("${rootProject.name}-content")
include("${rootProject.name}-danger")
Expand Down

0 comments on commit d999ca0

Please sign in to comment.