From 15f25320096b9282972046b250653e93663dd28b Mon Sep 17 00:00:00 2001 From: Phoenix-Starlight Date: Sun, 3 Sep 2023 16:50:09 -0700 Subject: [PATCH] Dynamic sound loading --- .../SoundBufferLibraryMixin.java | 45 +++++++++++++++++++ .../dynamicresources/DynamicSoundHelpers.java | 14 ++++++ 2 files changed, 59 insertions(+) create mode 100644 common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/SoundBufferLibraryMixin.java create mode 100644 common/src/main/java/org/embeddedt/modernfix/dynamicresources/DynamicSoundHelpers.java diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/SoundBufferLibraryMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/SoundBufferLibraryMixin.java new file mode 100644 index 00000000..5fec79e1 --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/dynamic_resources/SoundBufferLibraryMixin.java @@ -0,0 +1,45 @@ +package org.embeddedt.modernfix.common.mixin.perf.dynamic_resources; + +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalNotification; +import com.mojang.blaze3d.audio.SoundBuffer; +import net.minecraft.client.sounds.SoundBufferLibrary; +import net.minecraft.resources.ResourceLocation; +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; +import org.embeddedt.modernfix.dynamicresources.DynamicSoundHelpers; +import org.embeddedt.modernfix.ModernFix; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Mutable; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; +import java.util.Map; + +@Mixin(SoundBufferLibrary.class) +@ClientOnlyMixin +public abstract class SoundBufferLibraryMixin { + + private static final boolean debugDynamicSoundLoading = Boolean.getBoolean("modernfix.debugDynamicSoundLoading"); + + @Shadow @Final @Mutable + private Map> cache = + CacheBuilder.newBuilder() + .maximumSize(DynamicSoundHelpers.MAX_SOUND_COUNT) + .expireAfterAccess(DynamicSoundHelpers.MAX_SOUND_LIFETIME_SECS, TimeUnit.SECONDS) + // Excessive use of type hinting due to it assuming Object as the broadest correct type + .>removalListener(this::onSoundRemoval) + .>build() + .asMap(); + + private > void onSoundRemoval(RemovalNotification notification) { + notification.getValue().thenAccept(SoundBuffer::discardAlBuffer); + if(debugDynamicSoundLoading) { + K k = notification.getKey(); + if(k == null) + return; + ModernFix.LOGGER.warn("Evicted sound {}", k); + } + } +} diff --git a/common/src/main/java/org/embeddedt/modernfix/dynamicresources/DynamicSoundHelpers.java b/common/src/main/java/org/embeddedt/modernfix/dynamicresources/DynamicSoundHelpers.java new file mode 100644 index 00000000..c7864666 --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/dynamicresources/DynamicSoundHelpers.java @@ -0,0 +1,14 @@ +package org.embeddedt.modernfix.dynamicresources; + +public class DynamicSoundHelpers { + /** + * The duration until a sound is eligible for eviction if unused. + */ + public static final int MAX_SOUND_LIFETIME_SECS = 120; + + /** + * The max amount of sounds loaded + * This was chosen because Minecraft Java can play up to 255 sounds, with a bit of leeway for extra sounds being loaded in. + */ + public static final int MAX_SOUND_COUNT = 300; +}