Skip to content

Commit

Permalink
Release 3.6.5 & Use url safe base64 encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
trychen committed Sep 29, 2020
1 parent 6e44ab9 commit a5b4561
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx3G

# Mod Properties
mod_id = Pangu
mod_version = 3.5.9
mod_version = 3.6.5
mod_group = cn.mccraft.pangu
mod_core_plugin = cn.mccraft.pangu.core.asm.PanguPlugin

Expand Down
4 changes: 0 additions & 4 deletions src/main/java/cn/mccraft/pangu/core/asm/PanguPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

@IFMLLoadingPlugin.MCVersion("1.12.2")
@IFMLLoadingPlugin.TransformerExclusions({"cn.mccraft.pangu.core.asm"})
@Log4j2(topic = "Pangu Core")
public class PanguPlugin implements IFMLLoadingPlugin {
public static Logger getLogger() {
return log;
}

@Override
public String[] getASMTransformerClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public class BridgeTransformer implements IClassTransformer {
@SuppressWarnings("Duplicates")
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (basicClass == null) return null;

ClassNode classNode = ASMHelper.newClassNode(basicClass);
boolean edited = false;

Expand Down Expand Up @@ -93,7 +95,7 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
}
}

PanguPlugin.getLogger().debug("Hook @Bridge method: " + classNode.name + "#" + method.name + method.desc + "");
// PanguPlugin.getLogger().debug("Hook @Bridge method: " + classNode.name + "#" + method.name + method.desc + "");
}
if (!edited) return basicClass;
ClassWriter cw = new PanguClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class CheckPrefixTransformer implements IClassTransformer {
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (!transformedName.equals("net.minecraftforge.registries.GameData")) return basicClass;
if (basicClass == null) return basicClass;

ClassNode classNode = ASMHelper.newClassNode(basicClass);

Expand Down Expand Up @@ -59,12 +60,14 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)

ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
classNode.accept(cw);
PanguPlugin.getLogger().info("Hooked net.minecraftforge.registries.GameData#checkPrefix(Ljava/lang/String;)Lnet/minecraft/util/ResourceLocation;");
System.out.println("Hooked net.minecraftforge.registries.GameData#checkPrefix(Ljava/lang/String;)Lnet/minecraft/util/ResourceLocation;");
// PanguPlugin.getLogger().info("Hooked net.minecraftforge.registries.GameData#checkPrefix(Ljava/lang/String;)Lnet/minecraft/util/ResourceLocation;");
return cw.toByteArray();
}
}

PanguPlugin.getLogger().error("Couldn't hook checkPrefix!");
System.err.println("[Pangu Core] Couldn't hook checkPrefix!");
// PanguPlugin.getLogger().error("Couldn't hook checkPrefix!");

return basicClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public DevTransformer() {
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
if (isDevMode) return basicClass;
if (basicClass == null) return null;

AtomicBoolean edited = new AtomicBoolean(false);
ClassNode classNode = ASMHelper.newClassNode(basicClass);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)

ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classNode.accept(cw);
PanguPlugin.getLogger().info("Hooked net.minecraft.client.gui.GuiScreen#drawWorldBackground(int);");
System.out.println("Hooked net.minecraft.client.gui.GuiScreen#drawWorldBackground(int);");
return cw.toByteArray();
}
}
}

PanguPlugin.getLogger().error("Couldn't hook drawWorldBackground!");
System.err.println("Couldn't hook drawWorldBackground!");

return basicClass;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ public MultiPartPacketBuffer(String key, byte[] bytes) {

public MultiPartPacketBuffer(MultiPartPacket packet) {
packets = new MultiPartPacket[packet.total];
process(packet);
}

public void process(MultiPartPacket packet) {
Expand All @@ -377,13 +376,12 @@ public byte[] getBytes() {
byte[] bytes = new byte[size];
int cursor = 0;
for (MultiPartPacket packet : packets) {
System.arraycopy(packet.bytes, 0, bytes, cursor, cursor + packet.getBytes().length);
System.arraycopy(packet.bytes, 0, bytes, cursor, packet.getBytes().length);
cursor += packet.getBytes().length;
}
return bytes;
}
}

Map<UUID, MultiPartPacketBuffer> MULTI_PART_PACKET_BUFFER = new ConcurrentHashMap<>();

@AllArgsConstructor
Expand All @@ -401,9 +399,12 @@ public IMessage onMessage(MultiPartPacket message, MessageContext ctx) {
}

MultiPartPacketBuffer buffer = MULTI_PART_PACKET_BUFFER.computeIfAbsent(message.getUuid(), it -> new MultiPartPacketBuffer(message));
buffer.process(message);

if (!buffer.isComplete()) return null;

MULTI_PART_PACKET_BUFFER.remove(message.getUuid());

if (!solution.isSync()) {
try {
solution.solve(solution.side().isServer() ? ctx.getServerHandler().player : Games.player(), buffer.getBytes());
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/cn/mccraft/pangu/core/util/Base64Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cn.mccraft.pangu.core.util;

import java.util.Base64;

public interface Base64Utils {
static String safeUrlBase64Encode(byte[] data){
String encodeBase64 = Base64.getEncoder().encodeToString(data);
String safeBase64Str = encodeBase64.replace('+', '-');
safeBase64Str = safeBase64Str.replace('/', '_');
safeBase64Str = safeBase64Str.replaceAll("=", "");
return safeBase64Str;
}

static byte[] safeUrlBase64Decode(final String safeBase64Str){
String base64Str = safeBase64Str.replace('-', '+');
base64Str = base64Str.replace('_', '/');
int mod4 = base64Str.length()%4;
if(mod4 > 0){
base64Str = base64Str + "====".substring(mod4);
}
return Base64.getDecoder().decode(base64Str);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.mccraft.pangu.core.util.image;

import cn.mccraft.pangu.core.PanguCore;
import cn.mccraft.pangu.core.util.Base64Utils;
import cn.mccraft.pangu.core.util.Http;
import cn.mccraft.pangu.core.util.LocalCache;
import lombok.Getter;
Expand Down Expand Up @@ -42,7 +43,7 @@ public Thread newThread(Runnable r) {
protected RemoteGif(String urlPath) throws URISyntaxException {
this.urlPath = urlPath;
this.url = new URI(urlPath);
this.id = Base64.getEncoder().encodeToString(urlPath.getBytes());
this.id = Base64Utils.safeUrlBase64Encode(urlPath.getBytes());
this.cachedFilePath = createCachedFilePath();
LocalCache.markFileUsed(cachedFilePath.toPath());

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/cn/mccraft/pangu/core/util/image/RemoteImage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.mccraft.pangu.core.util.image;

import cn.mccraft.pangu.core.PanguCore;
import cn.mccraft.pangu.core.util.Base64Utils;
import cn.mccraft.pangu.core.util.Games;
import cn.mccraft.pangu.core.util.Http;
import cn.mccraft.pangu.core.util.LocalCache;
Expand Down Expand Up @@ -59,7 +60,7 @@ public Thread newThread(Runnable r) {
@Getter
protected String id;
@Getter
protected transient boolean exception = false, loaded = false;
protected transient boolean exception = false, loaded = false, requested = false;

@Getter
protected File cachedFilePath;
Expand All @@ -76,10 +77,12 @@ public Thread newThread(Runnable r) {
protected RemoteImage(String urlPath) throws URISyntaxException {
this.urlPath = urlPath;
this.url = new URI(urlPath);
this.id = Base64.getEncoder().encodeToString(urlPath.getBytes()).replace('/', '_').replace('\\', '_');
this.id = Base64Utils.safeUrlBase64Encode(urlPath.getBytes());
this.cachedFilePath = createCachedFilePath();
LocalCache.markFileUsed(cachedFilePath.toPath());

// requested = true;
// fetch();
}

public static RemoteImage deserialize(DataInput out) throws IOException {
Expand Down Expand Up @@ -116,6 +119,10 @@ public ResourceLocation getTexture(ResourceLocation loading, ResourceLocation er
@Override
public int getTextureID() {
if (textureID > 0) return textureID;
if (!requested) {
requested = true;
fetch();
}
if (!loaded) return 0;
if (exception) return -1;
if (imageBuffer == null) {
Expand Down

0 comments on commit a5b4561

Please sign in to comment.