Skip to content

Commit

Permalink
Backports for sounds and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
OreCruncher committed Jun 7, 2017
1 parent 1fa4792 commit e547e04
Show file tree
Hide file tree
Showing 16 changed files with 834 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@

import static org.objectweb.asm.Opcodes.*;

import java.util.ListIterator;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.VarInsnNode;

import net.minecraft.launchwrapper.IClassTransformer;
Expand All @@ -55,6 +59,9 @@ public byte[] transform(final String name, final String transformedName, byte[]
} else if ("net.minecraft.world.World".equals(name) || "ahb".equals(name)) {
logger.debug("Transforming World...");
return transformWorld(basicClass);
} else if ("net.minecraft.client.audio.SoundHandler".equals(name) || "btp".equals(name)) {
logger.debug("Transforming SoundHandler...");
return transformSoundHandler(basicClass);
} else if ("net.minecraft.client.audio.SoundManager".equals(name) || "btj".equals(name)) {
logger.debug("Transforming SoundManager...");
return transformSoundManager(basicClass);
Expand Down Expand Up @@ -171,33 +178,70 @@ private byte[] transformWorld(final byte[] classBytes) {
return cw.toByteArray();
}

private byte[] transformSoundHandler(final byte[] classBytes) {
final String managerToReplace = "net/minecraft/client/audio/SoundManager";
final String newManager = "org/blockartistry/mod/DynSurround/client/sound/SoundManagerReplacement";

final ClassReader cr = new ClassReader(classBytes);
final ClassNode cn = new ClassNode(ASM5);
cr.accept(cn, 0);

for (final MethodNode m : cn.methods) {
final ListIterator<AbstractInsnNode> itr = m.instructions.iterator();
boolean foundNew = false;
while (itr.hasNext()) {
final AbstractInsnNode node = itr.next();
if (node.getOpcode() == NEW) {
final TypeInsnNode theNew = (TypeInsnNode) node;
if (managerToReplace.equals(theNew.desc)) {
m.instructions.set(node, new TypeInsnNode(NEW, newManager));
foundNew = true;
}
} else if (node.getOpcode() == INVOKESPECIAL) {
final MethodInsnNode theInvoke = (MethodInsnNode) node;
if (managerToReplace.equals(theInvoke.owner)) {
if (foundNew) {
m.instructions.set(node, new MethodInsnNode(INVOKESPECIAL, newManager, theInvoke.name,
theInvoke.desc, false));
foundNew = false;
}
}
}
}
}

final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
cn.accept(cw);
return cw.toByteArray();
}

private byte[] transformSoundManager(final byte[] classBytes) {
final String names[];
final String urlNames[];

if (TransformLoader.runtimeDeobEnabled)
names = new String[] { "func_148594_a" };
else
names = new String[] { "getNormalizedVolume" };
if (TransformLoader.runtimeDeobEnabled) {
urlNames = new String[] { "func_148612_a" };
} else {
urlNames = new String[] { "getURLForSoundResource" };
}

final String targetName[] = new String[] { "getNormalizedVolume" };
final String urlTargetName[] = new String[] { "getURLForSoundResource" };

final ClassReader cr = new ClassReader(classBytes);
final ClassNode cn = new ClassNode(ASM5);
cr.accept(cn, 0);

for (final MethodNode m : cn.methods) {
if (m.name.equals(names[0])) {
logger.debug("Hooking " + names[0]);
final InsnList list = new InsnList();
list.add(new VarInsnNode(ALOAD, 1));
list.add(new VarInsnNode(ALOAD, 2));
list.add(new VarInsnNode(ALOAD, 3));
final String sig = "(Lnet/minecraft/client/audio/ISound;Lnet/minecraft/client/audio/SoundPoolEntry;Lnet/minecraft/client/audio/SoundCategory;)F";
list.add(new MethodInsnNode(INVOKESTATIC, "org/blockartistry/mod/DynSurround/client/sound/SoundManager",
targetName[0], sig, false));
list.add(new InsnNode(FRETURN));
if (m.name.equals(urlNames[0])) {
logger.debug("Hooking " + m.name);
InsnList list = new InsnList();
list.add(new VarInsnNode(ALOAD, 0));
final String sig = "(Lnet/minecraft/util/ResourceLocation;)Ljava/net/URL;";
list.add(new MethodInsnNode(INVOKESTATIC,
"org/blockartistry/mod/DynSurround/client/sound/cache/SoundCache", urlTargetName[0], sig,
false));
list.add(new InsnNode(ARETURN));
m.instructions.insertBefore(m.instructions.getFirst(), list);
break;

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ private static void resetSounds() {
@Override
public void process(final World world, final EntityPlayer player) {

SoundManager.keepAlive();

// Dead players hear no sounds
if (player.isDead) {
resetSounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Scanner;

import org.blockartistry.mod.DynSurround.ModLog;
import org.blockartistry.mod.DynSurround.ModOptions;
import org.blockartistry.mod.DynSurround.client.IClientEffectHandler;
import org.blockartistry.mod.DynSurround.client.footsteps.game.system.ForgeDictionary;
import org.blockartistry.mod.DynSurround.client.footsteps.game.system.PFIsolator;
Expand Down Expand Up @@ -212,7 +213,10 @@ public void process(World world, EntityPlayer player) {
reloadEverything();
}
this.isolator.onFrame();
player.nextStepDistance = Integer.MAX_VALUE;
if (ModOptions.footstepsSoundFactor > 0)
player.nextStepDistance = Integer.MAX_VALUE;
else if(player.nextStepDistance == Integer.MAX_VALUE)
player.nextStepDistance = 0;
}

@SubscribeEvent(priority = EventPriority.LOWEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.blockartistry.mod.DynSurround.client.footsteps.mcpackage.implem.NormalVariator;
import org.blockartistry.mod.DynSurround.client.footsteps.mcpackage.interfaces.IGenerator;
import org.blockartistry.mod.DynSurround.client.footsteps.mcpackage.interfaces.IIsolator;
import org.blockartistry.mod.DynSurround.client.footsteps.mcpackage.interfaces.ISolver;
import org.blockartistry.mod.DynSurround.client.footsteps.mcpackage.interfaces.IVariator;
import org.blockartistry.mod.DynSurround.client.footsteps.mcpackage.interfaces.IVariatorSettable;
import org.blockartistry.mod.DynSurround.util.MyUtils;
Expand Down Expand Up @@ -268,8 +269,6 @@ private void simulateBrushes(final EntityPlayer ply) {
if ((ply.motionX == 0d && ply.motionZ == 0d) || ply.isSneaking())
return;

// if (true || ply.onGround || ply.isOnLadder())
// {
final int yy = MathHelper.floor_double(ply.posY - 0.1d - ply.getYOffset() - (ply.onGround ? 0d : 0.25d));
final Association assos = mod.getSolver().findAssociationForBlock(MathHelper.floor_double(ply.posX), yy,
MathHelper.floor_double(ply.posZ), "find_messy_foliage");
Expand All @@ -279,15 +278,10 @@ private void simulateBrushes(final EntityPlayer ply) {
this.mod.getSolver().playAssociation(ply, assos, EventType.WALK);
}
} else {
if (this.isMessyFoliage) {
this.isMessyFoliage = false;
}
this.isMessyFoliage = false;
}
// }
}

//

protected void playSinglefoot(final EntityPlayer ply, final double verticalOffsetAsMinus, final EventType eventType,
final boolean foot) {
final Association assos = mod.getSolver().findAssociationForPlayer(ply, verticalOffsetAsMinus, isRightFoot);
Expand All @@ -297,16 +291,11 @@ protected void playSinglefoot(final EntityPlayer ply, final double verticalOffse
protected void playMultifoot(final EntityPlayer ply, final double verticalOffsetAsMinus,
final EventType eventType) {
// STILL JUMP
final Association leftFoot = mod.getSolver().findAssociationForPlayer(ply, verticalOffsetAsMinus, false);
Association rightFoot = mod.getSolver().findAssociationForPlayer(ply, verticalOffsetAsMinus, true);

if (leftFoot != null && leftFoot.equals(rightFoot) && !leftFoot.getNoAssociation()) {
rightFoot = null; // If the two feet solve to the same sound, except
// NO_ASSOCIATION, only play the sound once
}

mod.getSolver().playAssociation(ply, leftFoot, eventType);
mod.getSolver().playAssociation(ply, rightFoot, eventType);
final ISolver s = this.mod.getSolver();
final Association leftFoot = s.findAssociationForPlayer(ply, verticalOffsetAsMinus, false);
Association rightFoot = s.findAssociationForPlayer(ply, verticalOffsetAsMinus, true);
s.playAssociation(ply, leftFoot, eventType);
s.playAssociation(ply, rightFoot, eventType);
}

protected float scalex(final float number, final float min, final float max) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ public void playAssociation(final EntityPlayer ply, final Association assos, fin
@Override
public Association findAssociationForPlayer(final EntityPlayer ply, final double verticalOffsetAsMinus,
final boolean isRightFoot) {
if (Math.abs(ply.motionY) < 0.02)
return null; // Don't play sounds on every tiny bounce
final int yy = MathHelper.floor_double(ply.boundingBox.minY - 0.1d - verticalOffsetAsMinus);
final double rot = MathStuff.toRadians(MathHelper.wrapAngleTo180_float(ply.rotationYaw));
final double xn = MathStuff.cos(rot);
Expand All @@ -102,8 +104,6 @@ public Association findAssociationForPlayer(final EntityPlayer ply, final double

@Override
public Association findAssociationForLocation(final EntityPlayer player, final int x, final int y, final int z) {
if (Math.abs(player.motionY) < 0.02)
return null; // Don't play sounds on every tiny bounce
if (player.isInWater())
ModLog.debug(
"WARNING!!! Playing a sound while in the water! This is supposed to be halted by the stopping conditions!!");
Expand Down Expand Up @@ -176,16 +176,9 @@ public Association findAssociationForBlock(final int xx, int yy, final int zz) {
Block above = world.getBlock(xx, yy + 1, zz);
int aboveMeta = world.getBlockMetadata(xx, yy + 1, zz);

String association = this.isolator.getBlockMap().getBlockMapSubstrate(above, aboveMeta, "carpet"); // Try
// to
// see
// if
// the
// block
// above
// is
// a
// carpet...
String association = null;
if (above != Blocks.air)
association = this.isolator.getBlockMap().getBlockMapSubstrate(above, aboveMeta, "carpet");

// PFLog.debugf("Walking on block: %0 -- Being in block: %1", in,
// above);
Expand Down Expand Up @@ -220,10 +213,12 @@ public Association findAssociationForBlock(final int xx, int yy, final int zz) {
// => this block of code is here, not outside this if else
// group.

String foliage = this.isolator.getBlockMap().getBlockMapSubstrate(above, aboveMeta, "foliage");
if (foliage != null && !foliage.equals("NOT_EMITTER")) {
association = association + "," + foliage;
ModLog.debug("Foliage detected: " + foliage);
if (above != Blocks.air) {
String foliage = this.isolator.getBlockMap().getBlockMapSubstrate(above, aboveMeta, "foliage");
if (foliage != null && !foliage.equals("NOT_EMITTER")) {
association = association + "," + foliage;
ModLog.debug("Foliage detected: " + foliage);
}
}
}
} else {
Expand Down Expand Up @@ -344,6 +339,10 @@ public Association findAssociationForBlock(final int xx, int yy, final int zz, S
*/

Block above = world.getBlock(xx, yy + 1, zz);

if (above == Blocks.air)
return null;

int aboveMeta = world.getBlockMetadata(xx, yy + 1, zz);

String association = null;
Expand Down
Loading

0 comments on commit e547e04

Please sign in to comment.