Skip to content

Add LastStack #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions src/main/java/org/mangorage/mangobot/core/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@
package org.mangorage.mangobot.core;


import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.utils.TimeUtil;
import org.mangorage.basicutils.LogHelper;
import org.mangorage.mangobot.loader.CoreMain;
import org.mangorage.mangobotapi.core.commands.Arguments;
import org.mangorage.mangobotapi.core.commands.CommandPrefix;
import org.mangorage.mangobotapi.core.events.BasicCommandEvent;
import org.mangorage.mangobotapi.core.plugin.extra.JDAPlugin;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
Expand All @@ -49,6 +38,21 @@
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.temporal.TemporalAccessor;
import java.util.List;

import org.mangorage.basicutils.LogHelper;
import org.mangorage.mangobot.loader.CoreMain;
import org.mangorage.mangobot.modules.logs.LogAnalyser;
import org.mangorage.mangobot.modules.logs.LogAnalyserModule;
import org.mangorage.mangobotapi.core.commands.Arguments;
import org.mangorage.mangobotapi.core.commands.CommandPrefix;
import org.mangorage.mangobotapi.core.events.BasicCommandEvent;
import org.mangorage.mangobotapi.core.plugin.extra.JDAPlugin;

import net.dv8tion.jda.api.entities.ISnowflake;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.utils.TimeUtil;

public class Util {

Expand All @@ -70,7 +74,6 @@ public static MessageType handleMessage(JDAPlugin plugin, MessageReceivedEvent e
Message message = event.getMessage();
String rawMessage = message.getContentRaw();


boolean silent = rawMessage.startsWith("s");
if (silent)
rawMessage = rawMessage.replaceFirst("s", "");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.mangorage.mangobot.modules.logs;

import java.util.List;

import org.mangorage.mangobot.modules.logs.modules.BrokenDrivers;
import org.mangorage.mangobot.modules.logs.modules.EarlyWindow;
import org.mangorage.mangobot.modules.logs.modules.Java22;
import org.mangorage.mangobot.modules.logs.modules.LastStack;
import org.mangorage.mangobot.modules.logs.modules.MissingDeps;

import java.util.List;
import org.mangorage.mangobot.modules.logs.modules.RenewableLogAnalyser;

public interface LogAnalyserModule {
String LS = System.getProperty("line.separator");
Expand All @@ -15,6 +17,7 @@ public interface LogAnalyserModule {
new EarlyWindow(),
new Java22(),
new MissingDeps(),
new RenewableLogAnalyser(LastStack::new),
LogAnalyser.createModule(
(s, m) -> {
m.append("\n").append("This is a common issue on Modrinth Theseus. Modrinth's launcher has been known to be problematic in some cases with Forge. If you need to download a Modrinth format modpack you can use Prism Launcher, GDLauncher, ATLauncher, or others which are far more reliable.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.mangorage.mangobot.modules.logs.modules;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class LastStack extends StackTraceReader {


public String[] package_denylist = {
"java.",
"jdk.",
"sun.",
"com.sun."
};


@Override
public void analyse(String log, StringBuilder message) {
message.append("Analysing last").append(nl);
List<String> traces = getTraces(log);
if (traces.isEmpty()) {
return;
}

String lastTrace = traces.get(traces.size() - 1);

List<String> filteredLines = Stream.of(lastTrace.split(nl))
.filter(line -> !isLineDenylisted(line.strip()))
.collect(Collectors.toList());

for (String line : filteredLines) {
message.append(line).append(nl);
}
}

private boolean isLineDenylisted(String line) {
if (line.startsWith("at")) {
String packageCandidate = extractPackageCandidate(line);

for (String packagePrefix : package_denylist) {
if (packageCandidate.startsWith(packagePrefix)) {
return true;
}
}
}
return false;
}

private String extractPackageCandidate(String line) {
int startIdx = line.indexOf(' ') + 1;
int firstDotIdx = line.indexOf('.', startIdx);

if (firstDotIdx == -1) {
return "";
}

int endIdx = line.indexOf(' ', firstDotIdx);
if (endIdx == -1) {
endIdx = line.indexOf('(', firstDotIdx);
}
if (endIdx == -1) {
endIdx = line.length();
}

String candidate = line.substring(startIdx, endIdx).trim();

int lastSlashIdx = candidate.lastIndexOf('/');
if (lastSlashIdx != -1) {
candidate = candidate.substring(lastSlashIdx + 1);
}

int lastDotIdx = candidate.lastIndexOf('.');
if (lastDotIdx == -1) {
return "";
}

return candidate.substring(0, lastDotIdx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.dv8tion.jda.api.entities.Message;
import org.mangorage.mangobot.modules.logs.LogAnalyserModule;

public class StackTraceReader {
public class StackTraceReader implements LogAnalyserModule{

// 正则表达式用于匹配Java异常堆栈跟踪
private static final Pattern STACK_TRACE_PATTERN = Pattern.compile("(?m)(^\\S.*(?:\\r?\\n[ \\t]+at\\s+.*)+)");
Expand Down Expand Up @@ -42,20 +41,44 @@ public class StackTraceReader {
List<String> bad_jar = new ArrayList<String>();
List<String> bad_modid = new ArrayList<String>();
List<String> bad_package = new ArrayList<String>();
StringBuilder build = new StringBuilder();



public void analyse(String log, Message message) {
public String[] package_denylist = {
"java.",
"net.minecraft",
"net.minecraftforge",
"org.spongepowered",
"it.unimi",
"com.mojang.",
"cpw.",
"featurecreep.",
"jdk.",
"sun.",
"com.sun.",
"org.lwjgl.",
"org.apache.",
"io.netty",
"org.prismlauncher",
"io.github.zekerzhayard",
"org.multimc",
"org.polymc",
"org.tlauncher",

};




public void analyse(String log, StringBuilder build) {
int lvl = 0;
for (String trace : getFatalTraces(log).reversed()) {// Las ultimas son las más importante
lvl++;
this.processTrace(trace, true, lvl);
this.processTrace(build,trace, true, lvl);
}

for (String trace : getTraces(log).reversed()) {// Las ultimas son las más importante
lvl++;
this.processTrace(trace, false, lvl);
this.processTrace(build,trace, false, lvl);
}

List<String> jar_names = new ArrayList<String>();
Expand Down Expand Up @@ -142,14 +165,9 @@ public void analyse(String log, Message message) {
}
}

if (!build.toString().isEmpty()) {
message.reply(build.toString()).setSuppressEmbeds(true).mentionRepliedUser(true).queue();
;
}

}

public void processTrace(String trace, boolean fatal, int lvl) {
public void processTrace(StringBuilder build, String trace, boolean fatal, int lvl) {

List<String> jsonFiles = findJsonFilesInMixinExceptions(trace);

Expand Down Expand Up @@ -271,28 +289,8 @@ private boolean isModIDDenylisted(String modid) {

private boolean packIsDenyListed(String pack) {
// TODO Auto-generated method stub
String[] prefixes = {
"java.",
"net.minecraft.",
"net.minecraftforge.",
"com.mojang.",
"cpw.",
"featurecreep.",
"jdk.",
"sun.",
"com.sun.",
"org.lwjgl.",
"org.apache.",
"io.netty",
"org.prismlauncher",
"io.github.zekerzhayard",
"org.multimc",
"org.polymc",
"org.tlauncher",

};

for(String prefix:prefixes) {

for(String prefix:package_denylist) {
if(pack.startsWith(prefix)) {
return true;
}
Expand Down