Skip to content

Commit

Permalink
add report system
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Aug 6, 2023
1 parent 40b8361 commit 835a7fe
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 48 deletions.
56 changes: 48 additions & 8 deletions codebook-cli/src/main/java/io/papermc/codebook/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
import io.papermc.codebook.config.CodeBookUriResource;
import io.papermc.codebook.config.CodeBookVersionInput;
import io.papermc.codebook.exceptions.UserErrorException;
import io.papermc.codebook.report.ReportType;
import io.papermc.codebook.report.Reports;
import io.papermc.codebook.util.Downloader;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.zip.ZipFile;
Expand All @@ -59,6 +62,39 @@
usageHelpAutoWidth = true)
public final class Main implements Callable<Integer> {

@CommandLine.ArgGroup(multiplicity = "1", exclusive = false)
private @Nullable ReportOptions reports;

static final class ReportOptions {

@CommandLine.Option(
names = "--reports-dir",
paramLabel = "<reports-dir>",
description = "Parent directory to output any generated reports",
hidden = true)
private @Nullable Path reportsDir;

@CommandLine.ArgGroup(multiplicity = "1")
private SelectedReports selectedReports;

static final class SelectedReports {

@CommandLine.Option(
names = "--report",
paramLabel = "<report>",
description = "Set of report types to generate",
hidden = true)
private Set<ReportType> reports;

@CommandLine.Option(
names = "--all-reports",
paramLabel = "<all-reports>",
description = "Generate all reports",
hidden = true)
private boolean allReports;
}
}

@CommandLine.ArgGroup(
multiplicity = "1",
heading = "%n%nThe remapper must be an executable tiny-remapper jar. "
Expand Down Expand Up @@ -86,13 +122,6 @@ static final class RemapperOptions {
description =
"A download URL for the executable AutoRenamingTool jar to use for the remapping process.")
private @Nullable URI remapperUri;

@CommandLine.Option(
names = "--log-missing-lvt-suggestions",
paramLabel = "<log-missing-lvt-suggestions>",
description = "Include a report of missing lvt name suggestions in the remapping log",
hidden = true)
private boolean logMissingLvtSuggestions;
}

@CommandLine.ArgGroup(
Expand Down Expand Up @@ -421,6 +450,17 @@ private CodeBookContext createContext() {
return new Coords(c.constantsCoords, "constants", null, this.unpickMavenBaseUrl);
});

@Nullable Reports reports = null;
if (this.reports != null && this.reports.reportsDir != null) {
final Set<ReportType> reportsToGenerate;
if (this.reports.selectedReports.allReports) {
reportsToGenerate = Set.of(ReportType.values());
} else {
reportsToGenerate = this.reports.selectedReports.reports;
}
reports = new Reports(this.reports.reportsDir, reportsToGenerate);
}

return CodeBookContext.builder()
.remapperJar(remapper)
.mappings(mappings)
Expand All @@ -430,7 +470,7 @@ private CodeBookContext createContext() {
.outputJar(this.outputJar)
.overwrite(this.forceWrite)
.input(input)
.logMissingLvtSuggestions(this.remapper.logMissingLvtSuggestions)
.reports(reports)
.build();
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/io/papermc/codebook/CodeBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import io.papermc.codebook.pages.RemapLvtPage;
import io.papermc.codebook.pages.UnpickPage;
import io.papermc.codebook.util.IOUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -95,6 +96,13 @@ private void exec(final Path tempDir) {
}

IOUtil.move(resultJar, this.ctx.outputJar());
if (this.ctx.reports() != null) {
try {
this.ctx.reports().generateReports();
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
}

private static Injector injector(final Module module) {
Expand Down Expand Up @@ -172,6 +180,13 @@ protected void configure() {
} else {
this.bind(CodeBookPage.ConstantsJar.KEY).toProvider(Providers.of(null));
}

if (CodeBook.this.ctx.reports() != null) {
this.bind(CodeBookPage.Report.KEY).toInstance(CodeBook.this.ctx.reports());
this.install(CodeBook.this.ctx.reports());
} else {
this.bind(CodeBookPage.Report.KEY).toProvider(Providers.of(null));
}
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package io.papermc.codebook.config;

import io.papermc.codebook.report.Reports;
import io.soabase.recordbuilder.core.RecordBuilder;
import java.nio.file.Path;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -41,7 +42,7 @@ public record CodeBookContext(
@NotNull Path outputJar,
boolean overwrite,
@NotNull CodeBookInput input,
boolean logMissingLvtSuggestions) {
@Nullable @org.jetbrains.annotations.Nullable Reports reports) {

public static CodeBookContextBuilder builder() {
return CodeBookContextBuilder.builder();
Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/papermc/codebook/lvt/LvtNamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@
import dev.denwav.hypo.model.data.MethodData;
import dev.denwav.hypo.model.data.types.JvmType;
import dev.denwav.hypo.model.data.types.PrimitiveType;
import io.papermc.codebook.report.Reports;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.model.Mapping;
import org.cadixdev.lorenz.model.MethodMapping;
Expand All @@ -62,12 +60,10 @@ public class LvtNamer {
private final LvtTypeSuggester lvtTypeSuggester;
private final RootLvtSuggester lvtAssignSuggester;

public final Map<String, AtomicInteger> missedNameSuggestions = new ConcurrentHashMap<>();

public LvtNamer(final HypoContext context, final MappingSet mappings) throws IOException {
public LvtNamer(final HypoContext context, final MappingSet mappings, final Reports reports) throws IOException {
this.mappings = mappings;
this.lvtTypeSuggester = new LvtTypeSuggester(context);
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, this.missedNameSuggestions);
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, reports);
}

public void processClass(final AsmClassData classData) throws IOException {
Expand Down
23 changes: 9 additions & 14 deletions src/main/java/io/papermc/codebook/lvt/RootLvtSuggester.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
import io.papermc.codebook.lvt.suggestion.numbers.MthRandomSuggester;
import io.papermc.codebook.lvt.suggestion.numbers.RandomSourceSuggester;
import io.papermc.codebook.report.Reports;
import io.papermc.codebook.report.type.MissingMethodLvtSuggestion;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
Expand Down Expand Up @@ -81,18 +81,15 @@ public final class RootLvtSuggester extends AbstractModule implements LvtSuggest

private final HypoContext hypoContext;
private final LvtTypeSuggester lvtTypeSuggester;
public final Map<String, AtomicInteger> missedNameSuggestions;
private final Injector injector;
private final List<? extends LvtSuggester> suggesters;

public RootLvtSuggester(
final HypoContext hypoContext,
final LvtTypeSuggester lvtTypeSuggester,
final Map<String, AtomicInteger> missedNameSuggestions) {
final HypoContext hypoContext, final LvtTypeSuggester lvtTypeSuggester, final Reports reports) {
this.hypoContext = hypoContext;
this.lvtTypeSuggester = lvtTypeSuggester;
this.missedNameSuggestions = missedNameSuggestions;
final Injector injector = Guice.createInjector(this);
this.suggesters = SUGGESTERS.stream().map(injector::getInstance).toList();
this.injector = Guice.createInjector(this, reports);
this.suggesters = SUGGESTERS.stream().map(this.injector::getInstance).toList();
}

@Override
Expand Down Expand Up @@ -207,11 +204,9 @@ public static String determineFinalName(final String suggestedName, final Set<St
return suggestion;
}
}
this.missedNameSuggestions
.computeIfAbsent(
call.data().name() + "," + insn.owner().name() + "," + insn.node().desc,
(k) -> new AtomicInteger(0))
.incrementAndGet();
this.injector
.getInstance(MissingMethodLvtSuggestion.class)
.reportMissingMethodLvtSuggestion(call.data(), insn.node());
return null;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/io/papermc/codebook/pages/CodeBookPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.inject.util.Providers;
import dev.denwav.hypo.core.HypoContext;
import io.papermc.codebook.config.CodeBookContext;
import io.papermc.codebook.report.Reports;
import jakarta.inject.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -156,4 +157,11 @@ public void to(final @Nullable T value) {
public @interface Hypo {
Key<HypoContext> KEY = Key.get(HypoContext.class, Hypo.class);
}

@Qualifier
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Report {
Key<Reports> KEY = Key.get(Reports.class, Report.class);
}
}
21 changes: 5 additions & 16 deletions src/main/java/io/papermc/codebook/pages/RemapLvtPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,42 @@
import dev.denwav.hypo.core.HypoContext;
import dev.denwav.hypo.model.HypoModelUtil;
import dev.denwav.hypo.model.data.ClassData;
import io.papermc.codebook.config.CodeBookContext;
import io.papermc.codebook.exceptions.UnexpectedException;
import io.papermc.codebook.lvt.LvtNamer;
import io.papermc.codebook.report.Reports;
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import org.cadixdev.lorenz.MappingSet;

public final class RemapLvtPage extends CodeBookPage {

private final HypoContext context;
private final MappingSet paramMappings;
private final boolean logMissingLvtSuggestions;
private final Reports reports;

@Inject
public RemapLvtPage(
@Hypo final HypoContext hypoContext,
@ParamMappings final MappingSet paramMappings,
@Context final CodeBookContext context) {
@Report final Reports reports) {
this.context = hypoContext;
this.paramMappings = paramMappings;
this.logMissingLvtSuggestions = context.logMissingLvtSuggestions();
this.reports = reports;
}

@Override
public void exec() {
final LvtNamer lvtNamer;
try {
lvtNamer = new LvtNamer(this.context, this.paramMappings);
lvtNamer = new LvtNamer(this.context, this.paramMappings, this.reports);
} catch (final IOException e) {
throw new UnexpectedException("Failed to create LVT namer", e);
}

this.remapLvt(lvtNamer);

if (this.logMissingLvtSuggestions) {
final var comparator = Comparator.<Map.Entry<String, AtomicInteger>, Integer>comparing(
e -> e.getValue().get());
lvtNamer.missedNameSuggestions.entrySet().stream()
.sorted(comparator.reversed())
.forEach(s -> System.out.println("missed: " + s.getKey() + " -- " + s.getValue() + " times"));
}
}

private void remapLvt(final LvtNamer lvtNamer) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/papermc/codebook/report/ReportType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* codebook is a remapper utility for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 3 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

package io.papermc.codebook.report;

public enum ReportType {
MISSING_METHOD_LVT_SUGGESTION,
;
}
Loading

0 comments on commit 835a7fe

Please sign in to comment.