Repository navigation
perf: read inventory holders without copying the block #33
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/test/java/net/tfminecraft/surgery/HolderLookupPatch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package net.tfminecraft.surgery; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.is; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import net.bytebuddy.agent.ByteBuddyAgent; | ||
| import net.bytebuddy.agent.builder.AgentBuilder; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.dynamic.DynamicType; | ||
| import net.bytebuddy.utility.JavaModule; | ||
| import org.bukkit.inventory.Inventory; | ||
| import org.bukkit.inventory.InventoryHolder; | ||
| import org.junit.platform.launcher.LauncherSession; | ||
| import org.junit.platform.launcher.LauncherSessionListener; | ||
| import org.mockbukkit.mockbukkit.inventory.InventoryMock; | ||
|
|
||
| /** | ||
| * MockBukkit throws on Paper's {@code getHolder(false)}. Its inventories hold no block snapshot, | ||
| * so the patch answers with the plain holder, which is what Paper returns for plugin GUIs. | ||
| */ | ||
| public final class HolderLookupPatch implements LauncherSessionListener { | ||
| private static boolean installed; | ||
|
|
||
| @Override | ||
| public void launcherSessionOpened(LauncherSession session) { | ||
| install(); | ||
| } | ||
|
|
||
| /** Patches {@link InventoryMock#getHolder(boolean)} once per test JVM. */ | ||
| public static synchronized void install() { | ||
| if (installed) { | ||
| return; | ||
| } | ||
| AtomicBoolean patched = new AtomicBoolean(); | ||
| AtomicBoolean rejected = new AtomicBoolean(); | ||
| new AgentBuilder.Default() | ||
| .disableClassFormatChanges() | ||
| .with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION) | ||
| .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE) | ||
| .with(AgentBuilder.TypeStrategy.Default.REDEFINE) | ||
| .with(AgentBuilder.Listener.StreamWriting.toSystemError().withErrorsOnly()) | ||
| .with(new AgentBuilder.Listener.Adapter() { | ||
| @Override | ||
| public void onTransformation(TypeDescription type, ClassLoader loader, JavaModule module, | ||
| boolean loaded, DynamicType dynamicType) { | ||
| if (type.getName().equals(InventoryMock.class.getName())) { | ||
| patched.set(true); | ||
| } | ||
| } | ||
|
|
||
| // The JVM can still reject the new bytes after onTransformation. | ||
| @Override | ||
| public void onError(String typeName, ClassLoader loader, JavaModule module, boolean loaded, | ||
| Throwable throwable) { | ||
| if (typeName.equals(InventoryMock.class.getName())) { | ||
| rejected.set(true); | ||
| } | ||
| } | ||
| }) | ||
| .type(is(InventoryMock.class)) | ||
| .transform((builder, type, loader, module, domain) -> builder.visit(Advice.to(PlainHolder.class) | ||
| .on(named("getHolder").and(takesArguments(boolean.class))))) | ||
| .installOn(ByteBuddyAgent.install()); | ||
| // A failed retransformation only reaches the listeners, so check it took before marking it done. | ||
| if (!patched.get() || rejected.get()) { | ||
| throw new IllegalStateException("Could not patch InventoryMock#getHolder(boolean)"); | ||
| } | ||
| installed = true; | ||
| } | ||
|
|
||
| /** Skips the throwing body and returns {@code getHolder()} instead. */ | ||
| static final class PlainHolder { | ||
| private PlainHolder() { | ||
| } | ||
|
|
||
| @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) | ||
| static boolean skip() { | ||
| return true; | ||
| } | ||
|
|
||
| @Advice.OnMethodExit | ||
| static void exit(@Advice.This Inventory inventory, | ||
| @Advice.Return(readOnly = false) InventoryHolder holder) { | ||
| holder = inventory.getHolder(); | ||
| } | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
src/test/resources/META-INF/services/org.junit.platform.launcher.LauncherSessionListener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| net.tfminecraft.surgery.HolderLookupPatch |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.