Implement more debug stuff

This commit is contained in:
LLytho 2022-08-13 17:35:57 +02:00
parent 8d6f2e565a
commit 3a03e1595e
12 changed files with 234 additions and 18 deletions

View file

@ -1,11 +1,13 @@
package com.almostreliable.unified;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.DebugConfig;
import com.almostreliable.unified.config.DuplicationConfig;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.RecipeDumper;
import com.almostreliable.unified.recipe.RecipeTransformer;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.FileUtils;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
@ -15,6 +17,8 @@ import net.minecraft.tags.TagManager;
import net.minecraft.world.item.Item;
import javax.annotation.Nullable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
@ -27,8 +31,12 @@ public class AlmostUnifiedRuntime {
}
public void run(Map<ResourceLocation, JsonElement> recipes) {
createGitIgnoreIfNotExists();
DuplicationConfig dupConfig = Config.load(DuplicationConfig.NAME, new DuplicationConfig.Serializer());
UnifyConfig unifyConfig = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
DebugConfig debugConfig = Config.load(DebugConfig.NAME, new DebugConfig.Serializer());
debugConfig.logRecipes(recipes, "recipes_before_unification");
List<UnifyTag<Item>> allowedTags = unifyConfig.bakeTags();
TagMap tagMap = createTagMap(allowedTags);
@ -39,7 +47,19 @@ public class AlmostUnifiedRuntime {
replacementMap,
unifyConfig,
dupConfig).transformRecipes(recipes);
new RecipeDumper(result, startTime, System.currentTimeMillis()).dump();
RecipeDumper dumper = new RecipeDumper(result, startTime, System.currentTimeMillis());
dumper.dump(debugConfig.dumpOverview, debugConfig.dumpUnification, debugConfig.dumpDuplicates);
debugConfig.logRecipes(recipes, "recipes_after_unification");
}
private void createGitIgnoreIfNotExists() {
Path path = AlmostUnifiedPlatform.INSTANCE.getConfigPath();
if (!(Files.exists(path) && Files.isDirectory(path))) {
FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getConfigPath(),
".gitignore",
sb -> sb.append(DebugConfig.NAME).append(".json").append("\n"));
}
}
public void updateTagManager(TagManager tagManager) {

View file

@ -0,0 +1,99 @@
package com.almostreliable.unified.config;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.utils.FileUtils;
import com.almostreliable.unified.utils.TagMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.StringUtils;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Collectors;
public class DebugConfig extends Config {
public static String NAME = "debug";
public final boolean dumpTagMap;
public final boolean dumpDuplicates;
public final boolean dumpUnification;
public final boolean dumpOverview;
public final boolean dumpRecipes;
public DebugConfig(boolean dumpTagMap, boolean dumpDuplicates, boolean dumpUnification, boolean dumpOverview, boolean dumpRecipes) {
this.dumpTagMap = dumpTagMap;
this.dumpDuplicates = dumpDuplicates;
this.dumpUnification = dumpUnification;
this.dumpOverview = dumpOverview;
this.dumpRecipes = dumpRecipes;
}
public void logUnifyTagDump(TagMap tagMap) {
if (!dumpTagMap) {
return;
}
FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getLogPath(), "unify_tag_dump.txt", sb -> {
tagMap
.getTags()
.stream()
.sorted(Comparator.comparing(t -> t.location().toString()))
.forEach(t -> sb
.append(StringUtils.rightPad(t.location().toString(), 40))
.append(" => ")
.append(tagMap
.getItems(t)
.stream()
.map(ResourceLocation::toString)
.sorted()
.collect(Collectors.joining(", ")))
.append("\n"));
});
}
public void logRecipes(Map<ResourceLocation, JsonElement> recipes, String filename) {
if (!dumpRecipes) {
return;
}
FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getLogPath(),
filename,
sb -> recipes.forEach((key, value) -> sb
.append(key.toString())
.append(" [JSON]:")
.append(value.toString())
.append("\n")));
}
public static class Serializer extends Config.Serializer<DebugConfig> {
public static final String DUMP_TAG_MAP = "dumpTagMap";
public static final String DUMP_DUPLICATES = "dumpDuplicates";
public static final String DUMP_UNIFICATION = "dumpUnification";
public static final String DUMP_OVERVIEW = "dumpOverview";
public static final String DUMP_RECIPES = "dumpRecipes";
@Override
public DebugConfig deserialize(JsonObject json) {
return new DebugConfig(
safeGet(() -> json.get(DUMP_TAG_MAP).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_DUPLICATES).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_UNIFICATION).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_OVERVIEW).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_RECIPES).getAsBoolean(), false)
);
}
@Override
public JsonObject serialize(DebugConfig src) {
JsonObject json = new JsonObject();
json.addProperty(DUMP_TAG_MAP, src.dumpTagMap);
json.addProperty(DUMP_DUPLICATES, src.dumpDuplicates);
json.addProperty(DUMP_UNIFICATION, src.dumpUnification);
json.addProperty(DUMP_OVERVIEW, src.dumpOverview);
json.addProperty(DUMP_RECIPES, src.dumpRecipes);
return json;
}
}
}

View file

@ -13,7 +13,7 @@ import java.util.Set;
import java.util.stream.Collectors;
public class DuplicationConfig extends Config {
public static final String NAME = BuildConfig.MOD_ID + "-duplicates";
public static final String NAME = "duplicates";
private final JsonCompare.CompareSettings defaultRules;
private final LinkedHashMap<ResourceLocation, JsonCompare.CompareSettings> overrideRules;
private final Set<ResourceLocation> ignoreRecipeTypes;

View file

@ -12,7 +12,7 @@ import java.util.*;
import java.util.stream.Collectors;
public class UnifyConfig extends Config {
public static String NAME = BuildConfig.MOD_ID + "-unify";
public static String NAME = "unify";
private final List<String> stoneStrata;
private final List<String> materials;
private final List<String> unbakedTags;

View file

@ -27,25 +27,32 @@ public class RecipeDumper {
this.endTime = endTime;
}
public void dump() {
public void dump(boolean dumpOverview, boolean dumpUnify, boolean dumpDuplicate) {
String last = "# Last execution: " + format.format(new Date(startTime));
if (dumpOverview) {
StringBuilder overviewBuilder = new StringBuilder();
overviewBuilder.append(last).append("\n");
dumpOverview(overviewBuilder);
write(overviewBuilder, AlmostUnifiedPlatform.INSTANCE.getLogPath(), "overview_dump.txt");
}
if (dumpUnify) {
StringBuilder unifyStringBuilder = new StringBuilder();
unifyStringBuilder.append(last).append("\n");
dumpUnifyRecipes(unifyStringBuilder, true);
write(unifyStringBuilder, AlmostUnifiedPlatform.INSTANCE.getLogPath(), "unify_dump.txt");
}
if (dumpDuplicate) {
StringBuilder duplicatesStringBuilder = new StringBuilder();
duplicatesStringBuilder.append(last).append("\n");
dumpDuplicates(duplicatesStringBuilder);
write(duplicatesStringBuilder, AlmostUnifiedPlatform.INSTANCE.getLogPath(), "duplicates_dump.txt");
}
}
private void dumpDuplicates(StringBuilder stringBuilder) {
getSortedUnifiedRecipeTypes().forEach(type -> {
Collection<RecipeLink.DuplicateLink> duplicates = result

View file

@ -6,6 +6,7 @@ import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.JsonCompare;
import com.almostreliable.unified.utils.JsonQuery;
import com.almostreliable.unified.utils.RecipeTypePropertiesLogger;
import com.almostreliable.unified.utils.ReplacementMap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
@ -27,6 +28,8 @@ public class RecipeTransformer {
private final UnifyConfig unifyConfig;
private final DuplicationConfig duplicationConfig;
private final RecipeTypePropertiesLogger propertiesLogger = new RecipeTypePropertiesLogger();
public RecipeTransformer(RecipeHandlerFactory factory, ReplacementMap replacementMap, UnifyConfig unifyConfig, DuplicationConfig duplicationConfig) {
this.factory = factory;
this.replacementMap = replacementMap;
@ -172,6 +175,7 @@ public class RecipeTransformer {
if (result != null) {
recipe.setUnified(result);
}
propertiesLogger.log(recipe.getType(), recipe.getOriginal(), builder.getKeys());
} catch (Exception e) {
AlmostUnified.LOG.warn("Error unifying recipe '{}': {}", recipe.getId(), e.getMessage());
e.printStackTrace();

View file

@ -7,6 +7,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
@ -74,6 +75,10 @@ public class RecipeUnifierBuilderImpl implements RecipeUnifierBuilder {
return result;
}
public Collection<String> getKeys() {
return consumers.keySet();
}
private record Entry<T extends JsonElement>(Class<T> expectedType,
BiFunction<T, RecipeContext, T> func) {
@Nullable

View file

@ -0,0 +1,27 @@
package com.almostreliable.unified.utils;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.function.Consumer;
public class FileUtils {
public static void write(Path path, String filename, Consumer<StringBuilder> callback) {
StringBuilder sb = new StringBuilder();
callback.accept(sb);
try {
Files.createDirectories(path);
Path filePath = path.resolve(filename);
Files.writeString(filePath,
sb.toString(),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -0,0 +1,50 @@
package com.almostreliable.unified.utils;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
public class RecipeTypePropertiesLogger {
private final Map<String, List<String>> properties = new HashMap<>();
private List<String> getProperties(String mod) {
return properties.computeIfAbsent(mod, $ -> new ArrayList<>());
}
public void log(ResourceLocation recipeType, JsonObject recipe, Collection<String> keys) {
String mod = recipeType.getNamespace();
recipe.entrySet().forEach(e -> getProperties(mod).add(e.getKey()));
}
public void log(String mod, String property) {
getProperties(mod).add(property);
}
public void writeFile() {
StringBuilder sb = new StringBuilder();
properties.forEach((mod, properties) -> {
sb.append(mod).append(":\n");
properties.sort(String::compareTo);
properties.forEach(property -> sb.append(" ").append(property).append("\n"));
});
Path path = AlmostUnifiedPlatform.INSTANCE.getLogPath();
try {
Files.createDirectories(path);
Path filePath = path.resolve("debug_recipe_properties.txt");
Files.writeString(filePath,
sb.toString(),
StandardOpenOption.CREATE,
StandardOpenOption.TRUNCATE_EXISTING,
StandardOpenOption.WRITE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

View file

@ -70,4 +70,8 @@ public class TagMap {
public int itemSize() {
return itemsToTags.size();
}
public Collection<UnifyTag<Item>> getTags() {
return Collections.unmodifiableSet(tagsToItems.keySet());
}
}

View file

@ -24,7 +24,7 @@ public class AlmostUnifiedPlatformFabric implements AlmostUnifiedPlatform {
@Override
public Path getConfigPath() {
return FabricLoader.getInstance().getConfigDir();
return FabricLoader.getInstance().getConfigDir().resolve(BuildConfig.MOD_ID);
}
@Override

View file

@ -28,7 +28,7 @@ public class AlmostUnifiedPlatformForge implements AlmostUnifiedPlatform {
@Override
public Path getConfigPath() {
return FMLPaths.CONFIGDIR.get();
return FMLPaths.CONFIGDIR.get().resolve(BuildConfig.MOD_ID);
}
@Override