Skip to content
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

Expiremental Perf Code #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>com.comphenix.protocol</groupId>
<artifactId>ProtocolLib-API</artifactId>
<version>4.0</version>
<version>4.4.0</version>
<scope>provided</scope>
<optional>true</optional>
<exclusions>
Expand All @@ -49,6 +49,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>net.openhft</groupId>
<artifactId>zero-allocation-hashing</artifactId>
<version>0.8</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.lishid</groupId>
<artifactId>orebfuscator-v1_9_R1</artifactId>
Expand Down
115 changes: 71 additions & 44 deletions Plugin/src/main/java/com/lishid/orebfuscator/config/WorldConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,31 @@
import java.util.*;

public class WorldConfig {
private String name;
private String name;
private Boolean enabled;
private Boolean darknessHideBlocks;
private Boolean antiTexturePackAndFreecam;
private Boolean bypassObfuscationForSignsWithText;
private Integer airGeneratorMaxChance;
private Integer airGeneratorMaxChance;
private HashSet<Integer> obfuscateBlocks;
private HashSet<Integer> darknessBlocks;
private byte[] obfuscateAndProximityBlocks;
private byte[] obfuscateAndProximityBlocks;
private Integer[] randomBlocks;
private Integer[] randomBlocks2;
private Integer mode1BlockId;
private int[] paletteBlocks;
private ProximityHiderConfig proximityHiderConfig;
private boolean initialized;


/**
* Added in 1.13 patchset to allow faster, less determinstic random shuffles.
*/
private static final Random random = new Random();

public WorldConfig() {
this.proximityHiderConfig = new ProximityHiderConfig();
}

public void setDefaults() {
this.enabled = true;
this.darknessHideBlocks = false;
Expand All @@ -47,65 +52,65 @@ public void setDefaults() {

this.randomBlocks = new Integer[0];
this.randomBlocks2 = this.randomBlocks;

this.mode1BlockId = NmsInstance.current.getConfigDefaults().defaultMode1BlockId;
this.paletteBlocks = null;

this.proximityHiderConfig.setDefaults();
}

public void init(WorldConfig baseWorld) {
if(this.initialized) {
return;
}

if(baseWorld != null) {
if(this.enabled == null) {
this.enabled = baseWorld.enabled;
}

if(this.darknessHideBlocks == null) {
this.darknessHideBlocks = baseWorld.darknessHideBlocks;
}

if(this.antiTexturePackAndFreecam == null) {
this.antiTexturePackAndFreecam = baseWorld.antiTexturePackAndFreecam;
}

if(this.bypassObfuscationForSignsWithText == null) {
this.bypassObfuscationForSignsWithText = baseWorld.bypassObfuscationForSignsWithText;
}

if(this.airGeneratorMaxChance == null) {
this.airGeneratorMaxChance = baseWorld.airGeneratorMaxChance;
}

if(this.obfuscateBlocks == null) {
this.obfuscateBlocks = baseWorld.obfuscateBlocks != null ? (HashSet<Integer>)baseWorld.obfuscateBlocks.clone(): null;
}

if(this.darknessBlocks == null) {
this.darknessBlocks = baseWorld.darknessBlocks != null ? (HashSet<Integer>)baseWorld.darknessBlocks.clone(): null;
}

if(this.randomBlocks == null) {
this.randomBlocks = baseWorld.randomBlocks != null ? baseWorld.randomBlocks.clone(): null;
this.randomBlocks2 = baseWorld.randomBlocks2 != null ? baseWorld.randomBlocks2.clone(): null;
}

if(this.mode1BlockId == null) {
this.mode1BlockId = baseWorld.mode1BlockId;
}

this.proximityHiderConfig.init(baseWorld.proximityHiderConfig);
setObfuscateAndProximityBlocks();
}

setPaletteBlocks();

this.initialized = true;
}

public boolean isInitialized() {
return this.initialized;
}
Expand All @@ -117,47 +122,47 @@ public String getName() {
public void setName(String value) {
this.name = value;
}

public Boolean isEnabled() {
return this.enabled;
}

public void setEnabled(Boolean value) {
this.enabled = value;
}

public Boolean isDarknessHideBlocks() {
return this.darknessHideBlocks;
}

public void setDarknessHideBlocks(Boolean value) {
this.darknessHideBlocks = value;
}

public Boolean isAntiTexturePackAndFreecam() {
return this.antiTexturePackAndFreecam;
}

public void setAntiTexturePackAndFreecam(Boolean value) {
this.antiTexturePackAndFreecam = value;
}

public Boolean isBypassObfuscationForSignsWithText() {
return this.bypassObfuscationForSignsWithText;
}

public void setBypassObfuscationForSignsWithText(Boolean value) {
this.bypassObfuscationForSignsWithText = value;
}

public Integer getAirGeneratorMaxChance() {
return this.airGeneratorMaxChance;
}

public void setAirGeneratorMaxChance(Integer value) {
this.airGeneratorMaxChance = value;
}

public HashSet<Integer> getObfuscateBlocks() {
return this.obfuscateBlocks;
}
Expand Down Expand Up @@ -211,11 +216,11 @@ private void setObfuscateMask(Set<Integer> blockIds, boolean isDarknessBlock, bo
public byte[] getObfuscateAndProximityBlocks() {
return this.obfuscateAndProximityBlocks;
}

public HashSet<Integer> getDarknessBlocks() {
return this.darknessBlocks;
}

public void setDarknessBlocks(HashSet<Integer> values) {
this.darknessBlocks = values;
}
Expand All @@ -228,14 +233,36 @@ public void setRandomBlocks(Integer[] values) {
this.randomBlocks = values;
this.randomBlocks2 = values;
}

public void shuffleRandomBlocks() {
synchronized (this.randomBlocks) {
Collections.shuffle(Arrays.asList(this.randomBlocks));
Collections.shuffle(Arrays.asList(this.randomBlocks2));
}
// Use a Fisher-Yates shuffle over Collections.shuffle().
// Both are O(N) time, however with Collections.shuffle() we have
// to copy into a list to shuffle, and Collections.shuffle under the
// hood copies to an array shuffles, and then writes back.
//
// By performing fisher-yates directly we save the Copying back and forth.
if (this.randomBlocks.length != 0) {
synchronized (this.randomBlocks) {
for (int idx = 1; idx < this.randomBlocks.length; ++idx) {
int rand = random.nextInt(idx);
Integer save = this.randomBlocks[idx];
this.randomBlocks[idx] = this.randomBlocks[rand];
this.randomBlocks[rand] = save;
}
}
}
if (this.randomBlocks2.length != 0) {
synchronized (this.randomBlocks2) {
for (int idx = 1; idx < this.randomBlocks2.length; ++idx) {
int rand = random.nextInt(idx);
Integer save = this.randomBlocks2[idx];
this.randomBlocks2[idx] = this.randomBlocks2[rand];
this.randomBlocks2[rand] = save;
}
}
}
}

public Integer getMode1BlockId() {
return this.mode1BlockId;
}
Expand All @@ -247,43 +274,43 @@ public void setMode1BlockId(Integer value) {
public int[] getPaletteBlocks() {
return this.paletteBlocks;
}

private void setPaletteBlocks() {
if(this.randomBlocks == null) {
return;
}

HashSet<Integer> map = new HashSet<Integer>();

map.add(NmsInstance.current.getCaveAirBlockId());
map.add(this.mode1BlockId);

if(this.proximityHiderConfig.isUseSpecialBlock()) {
map.add(this.proximityHiderConfig.getSpecialBlockID());
}

for(Integer id : this.randomBlocks) {
if(id != null) {
map.add(id);
}
}

int[] paletteBlocks = new int[map.size()];
int index = 0;

for(Integer id : map) {
paletteBlocks[index++] = id;
}

this.paletteBlocks = paletteBlocks;
}

public ProximityHiderConfig getProximityHiderConfig() {
return this.proximityHiderConfig;
}

// Helper methods

public int getObfuscatedBits(int id) {
return this.obfuscateAndProximityBlocks[id];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private static ObfuscatedCachedChunk tryUseCache(ChunkData chunkData, Player pla
chunkData.useCache = true;

// Hash the chunk
long hash = CalculationsUtil.Hash(chunkData.data, chunkData.data.length);
long hash = CalculationsUtil.Hash(chunkData.data);
// Get cache folder
File cacheFolder = new File(ObfuscatedDataCache.getCacheFolder(), player.getWorld().getName());
// Create cache objects
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@

package com.lishid.orebfuscator.obfuscation;

import java.util.zip.CRC32;
import net.openhft.hashing.LongHashFunction;

public class CalculationsUtil {
public static long Hash(byte[] data, int length) {
CRC32 crc = new CRC32();
crc.reset();
crc.update(data, 0, length);
return crc.getValue();
public static long Hash(byte[] data) {
return LongHashFunction.xx().hashBytes(data);
}

public static int increment(int current, int max) {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<repositories>
<repository>
<id>dmulloy2-repo</id>
<url>http://repo.dmulloy2.net/content/groups/public/</url>
<url>http://repo.dmulloy2.net/nexus/repository/public/</url>
</repository>
<repository>
<id>spigot-repo</id>
Expand Down