-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add DataFixers * Add deterministic MetaBlocks generation * Upgrade of old Metablocks to new format
- Loading branch information
dan
authored
May 8, 2021
1 parent
0983830
commit f556ca8
Showing
27 changed files
with
1,491 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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 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 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,71 @@ | ||
package gregtech.api.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class Version implements Comparable<Version> { | ||
|
||
private final int[] nums; | ||
|
||
public Version(int... nums) { | ||
if (nums.length == 0) { | ||
throw new IllegalArgumentException("Must be at least one version number!"); | ||
} | ||
for (int num : nums) { | ||
if (num < 0) { | ||
throw new IllegalArgumentException("Version numbers must be positive!"); | ||
} | ||
} | ||
this.nums = nums; | ||
} | ||
|
||
public static Version parse(String vStr) { | ||
return new Version(Arrays.stream(vStr.split(Pattern.quote("."))).mapToInt(Integer::parseInt).toArray()); | ||
} | ||
|
||
public int getNumber(int index) { | ||
if (index < 0) { | ||
throw new IndexOutOfBoundsException("Index must be nonnegative!"); | ||
} | ||
return index < nums.length ? nums[index] : 0; | ||
} | ||
|
||
@Override | ||
public int compareTo(Version o) { | ||
int numBound = Math.max(nums.length, o.nums.length); | ||
for (int i = 0; i < numBound; i++) { | ||
int cmp = Integer.compare(getNumber(i), o.getNumber(i)); | ||
if (cmp != 0) { | ||
return cmp; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return obj instanceof Version && compareTo((Version) obj) == 0; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int hash = 0; | ||
for (int i = 0; i < nums.length; i++) { | ||
hash ^= Integer.rotateLeft(nums[i], i * 7); | ||
} | ||
return hash; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toString(nums.length); | ||
} | ||
|
||
public String toString(int sigPlaces) { | ||
return Arrays.stream(nums, 0, Math.min(sigPlaces, nums.length)) | ||
.mapToObj(Integer::toString) | ||
.collect(Collectors.joining(".")); | ||
} | ||
|
||
} |
This file contains 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
38 changes: 38 additions & 0 deletions
38
src/main/java/gregtech/common/asm/CompoundDataFixerGetVersionVisitor.java
This file contains 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,38 @@ | ||
package gregtech.common.asm; | ||
|
||
import gregtech.common.asm.util.ObfMapping; | ||
import gregtech.common.asm.util.SafeMethodVisitor; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
public class CompoundDataFixerGetVersionVisitor extends SafeMethodVisitor { | ||
|
||
public static final String TARGET_CLASS_NAME = "net/minecraftforge/common/util/CompoundDataFixer$1"; | ||
public static final ObfMapping TARGET_METHOD = new ObfMapping(TARGET_CLASS_NAME, "getVersion", "(Ljava/lang/String;)I"); | ||
|
||
private static final String WORLD_DATA_HOOKS_OWNER = "gregtech/common/datafix/fixes/metablockid/WorldDataHooks"; | ||
private static final String WORLD_DATA_HOOKS_METHOD_NAME = "getFallbackModVersion"; | ||
private static final String WORLD_DATA_HOOKS_SIGNATURE = "(Ljava/lang/String;)I"; | ||
|
||
public CompoundDataFixerGetVersionVisitor(MethodVisitor mv) { | ||
super(Opcodes.ASM5, mv); | ||
} | ||
|
||
@Override | ||
public void visitInsn(int opcode) { | ||
if (opcode == Opcodes.ICONST_M1) { | ||
markPatchedSuccessfully(); | ||
super.visitVarInsn(Opcodes.ALOAD, 1); | ||
super.visitMethodInsn(Opcodes.INVOKESTATIC, WORLD_DATA_HOOKS_OWNER, | ||
WORLD_DATA_HOOKS_METHOD_NAME, WORLD_DATA_HOOKS_SIGNATURE, false); | ||
} else { | ||
super.visitInsn(opcode); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getInjectTargetString() { | ||
return String.format("Patch target: %s; (point not found)", TARGET_METHOD); | ||
} | ||
|
||
} |
This file contains 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
59 changes: 59 additions & 0 deletions
59
src/main/java/gregtech/common/asm/SaveFormatOldLoadVisitor.java
This file contains 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,59 @@ | ||
package gregtech.common.asm; | ||
|
||
import gregtech.common.asm.util.ObfMapping; | ||
import gregtech.common.asm.util.SafeMethodVisitor; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
public class SaveFormatOldLoadVisitor extends SafeMethodVisitor { | ||
|
||
public static final String TARGET_CLASS_NAME = "net/minecraft/world/storage/SaveFormatOld"; | ||
public static final ObfMapping TARGET_METHOD = new ObfMapping(TARGET_CLASS_NAME, "loadAndFix", // forge method | ||
"(Ljava/io/File;Lnet/minecraft/util/datafix/DataFixer;Lnet/minecraft/world/storage/SaveHandler;)Lnet/minecraft/world/storage/WorldInfo;"); | ||
|
||
private static final String LOAD_COMPRESSED_OWNER = "net/minecraft/nbt/CompressedStreamTools"; | ||
private static final ObfMapping LOAD_COMPRESSED_METHOD = new ObfMapping(LOAD_COMPRESSED_OWNER, "func_74796_a", | ||
"(Ljava/io/InputStream;)Lnet/minecraft/nbt/NBTTagCompound;").toRuntime(); | ||
|
||
private static final String WORLD_DATA_HOOKS_OWNER = "gregtech/common/datafix/fixes/metablockid/WorldDataHooks"; | ||
private static final String WORLD_DATA_HOOKS_METHOD_NAME = "onWorldLoad"; | ||
private static final String WORLD_DATA_HOOKS_SIGNATURE = "(Lnet/minecraft/world/storage/SaveHandler;Lnet/minecraft/nbt/NBTTagCompound;)V"; | ||
|
||
private State state = State.WAITING_FOR_READ; | ||
|
||
public SaveFormatOldLoadVisitor(MethodVisitor mv) { | ||
super(Opcodes.ASM5, mv); | ||
} | ||
|
||
@Override | ||
public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { | ||
super.visitMethodInsn(opcode, owner, name, desc, itf); | ||
if (state == State.WAITING_FOR_READ && opcode == Opcodes.INVOKESTATIC | ||
&& owner.equals(LOAD_COMPRESSED_OWNER) && LOAD_COMPRESSED_METHOD.matches(name, desc)) { | ||
state = State.WAITING_FOR_VAR; | ||
} | ||
} | ||
|
||
@Override | ||
public void visitVarInsn(int opcode, int var) { | ||
super.visitVarInsn(opcode, var); | ||
if (state == State.WAITING_FOR_VAR && opcode == Opcodes.ASTORE) { | ||
state = State.DONE; | ||
markPatchedSuccessfully(); | ||
super.visitVarInsn(Opcodes.ALOAD, 2); | ||
super.visitVarInsn(Opcodes.ALOAD, var); | ||
super.visitMethodInsn(Opcodes.INVOKESTATIC, WORLD_DATA_HOOKS_OWNER, | ||
WORLD_DATA_HOOKS_METHOD_NAME, WORLD_DATA_HOOKS_SIGNATURE, false); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getInjectTargetString() { | ||
return String.format("Patch target: %s; (point not found)", TARGET_METHOD); | ||
} | ||
|
||
private enum State { | ||
WAITING_FOR_READ, WAITING_FOR_VAR, DONE | ||
} | ||
|
||
} |
This file contains 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 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
Oops, something went wrong.