Repository navigation
Fix #25 #27
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
Fix #25 #27
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,91 +1,63 @@ | ||
| package io.github.misode.packtest; | ||
|
|
||
| import com.google.common.collect.*; | ||
| import com.mojang.brigadier.CommandDispatcher; | ||
| import net.minecraft.commands.CommandSourceStack; | ||
| import net.minecraft.commands.Commands; | ||
| import net.minecraft.core.*; | ||
| import net.minecraft.resources.FileToIdConverter; | ||
| import net.minecraft.resources.Identifier; | ||
| import net.minecraft.server.packs.resources.PreparableReloadListener; | ||
| import net.minecraft.server.MinecraftServer; | ||
| import net.minecraft.server.packs.resources.Resource; | ||
| import net.minecraft.server.packs.resources.ResourceManager; | ||
| import net.minecraft.server.permissions.PermissionSet; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.ForkJoinPool; | ||
|
|
||
| public class PackTestLibrary implements PreparableReloadListener { | ||
| public class PackTestLibrary { | ||
|
Comment on lines
-25
to
+21
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do you realize that it was already using this, and your PR would switch away from using that? |
||
| private static final FileToIdConverter LISTER = new FileToIdConverter("test", ".mcfunction"); | ||
|
|
||
| private static final Map<Identifier, PackTestFunction> LOADED = new HashMap<>(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A static map of loaded functions is a really bad idea. Tests are already stored in the registry. |
||
|
|
||
| private final PackTestRegistries registries; | ||
| private final PermissionSet testCompilationPermissions; | ||
| private final CommandDispatcher<CommandSourceStack> dispatcher; | ||
|
|
||
| public PackTestLibrary( | ||
| HolderLookup.Provider registries, | ||
| PermissionSet testCompilationPermissions, | ||
| CommandDispatcher<CommandSourceStack> dispatcher) { | ||
| public PackTestLibrary(HolderLookup.Provider registries) { | ||
| this.registries = new PackTestRegistries(registries); | ||
| this.testCompilationPermissions = testCompilationPermissions; | ||
| this.dispatcher = dispatcher; | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull CompletableFuture<Void> reload( | ||
| @NotNull SharedState currentReload, | ||
| @NotNull Executor taskExecutor, | ||
| PreparationBarrier preparationBarrier, | ||
| @NotNull Executor reloadExecutor) { | ||
| ResourceManager manager = currentReload.resourceManager(); | ||
| CompletableFuture<RegistryAccess.Frozen> registryLoad = this.registries.load(manager, taskExecutor); | ||
|
|
||
| CompletableFuture<Map<Identifier, CompletableFuture<PackTestFunction>>> testFunctions = CompletableFuture | ||
| .supplyAsync(() -> LISTER.listMatchingResources(manager), taskExecutor) | ||
| .thenComposeAsync(map -> prepareTestFunctions(map, taskExecutor)); | ||
|
|
||
| return CompletableFuture.allOf(registryLoad, testFunctions) | ||
| .thenCompose(preparationBarrier::wait) | ||
| .thenAcceptAsync((_) -> | ||
| this.registries.register(registryLoad.join(), collectTestFunctions(testFunctions.join())), | ||
| reloadExecutor); | ||
| public static @Nullable PackTestFunction getLoaded(Identifier id) { | ||
| return LOADED.get(id); | ||
| } | ||
|
|
||
| private CompletableFuture<Map<Identifier, CompletableFuture<PackTestFunction>>> prepareTestFunctions( | ||
| Map<Identifier, Resource> resources, | ||
| Executor taskExecutor) { | ||
| Map<Identifier, CompletableFuture<PackTestFunction>> result = new HashMap<>(); | ||
| CommandSourceStack compilationContext = Commands.createCompilationContext(this.testCompilationPermissions); | ||
| public void load(MinecraftServer server) { | ||
| ResourceManager manager = server.getResourceManager(); | ||
| RegistryAccess.Frozen registryLookup = this.registries | ||
| .load(manager, ForkJoinPool.commonPool()) | ||
| .join(); | ||
| CommandDispatcher<CommandSourceStack> dispatcher = server.getCommands().getDispatcher(); | ||
| CommandSourceStack source = server.createCommandSourceStack(); | ||
|
|
||
| Map<Identifier, Resource> resources = LISTER.listMatchingResources(manager); | ||
| Map<Identifier, PackTestFunction> loaded = new HashMap<>(); | ||
| for (Map.Entry<Identifier, Resource> entry : resources.entrySet()) { | ||
| Identifier id = LISTER.fileToId(entry.getKey()); | ||
| result.put(id, CompletableFuture.supplyAsync(() -> { | ||
| try { | ||
| List<String> lines = readLines(entry.getValue()); | ||
| return PackTestFunction.fromLines(this.dispatcher, compilationContext, lines); | ||
| }, taskExecutor)); | ||
| } | ||
|
|
||
| return CompletableFuture.allOf(result.values().toArray(new CompletableFuture[0])).handle((_, _) -> result); | ||
| } | ||
|
|
||
| private static Map<Identifier, PackTestFunction> collectTestFunctions(Map<Identifier, CompletableFuture<PackTestFunction>> futures) { | ||
| ImmutableMap.Builder<Identifier, PackTestFunction> result = ImmutableMap.builder(); | ||
| futures.forEach((id, future) -> future.handle((test, e) -> { | ||
| if (e == null) { | ||
| result.put(id, test); | ||
| } else { | ||
| PackTestFunction fn = PackTestFunction.fromLines(dispatcher, source, lines); | ||
| loaded.put(id, fn); | ||
| } catch (Exception e) { | ||
| PackTest.LOGGER.error("Failed to load test {}", id, e); | ||
| } | ||
| return null; | ||
| }).join()); | ||
| return result.build(); | ||
| } | ||
|
|
||
| LOADED.clear(); | ||
| LOADED.putAll(loaded); | ||
| this.registries.register(registryLookup, loaded); | ||
| PackTest.LOGGER.info("Loaded {} test functions", loaded.size()); | ||
| } | ||
|
|
||
| private static List<String> readLines(Resource resource) { | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What issue is this intended to solve? Why did
withSuppressedOutputnot work?