forked from HeyZeer0/ShieldDisruptor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3: Crash on 1.20.6 due to Identifier.of
For the update to 1.21 I needed to change new Identifier() to Identifier.of(), but this (obviously) caused a crash with lower versions. For some strange reason the tests in my dev environment didn't produce the crash however...
- Loading branch information
1 parent
b56b0d9
commit cd3f144
Showing
5 changed files
with
232 additions
and
5 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
102 changes: 102 additions & 0 deletions
102
src/main/java/me/mightyknight/sd/multiversion_mixin/ReflectionUtils.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,102 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2019 - 2024 Virtuoel | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
package me.mightyknight.sd.multiversion_mixin; | ||
|
||
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; | ||
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; | ||
import me.mightyknight.sd.common.ShieldDisruptor; | ||
import me.mightyknight.sd.multiversion_mixin.version.PehkuiVersionUtils; | ||
import net.minecraft.util.Identifier; | ||
import net.minecraft.util.InvalidIdentifierException; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.fabricmc.loader.api.Version; | ||
|
||
// The reflection code adapted and modified from Pehkui, a big thank you to Virtuoel | ||
public class ReflectionUtils { | ||
|
||
|
||
public static final MethodHandle CONSTRUCT_ID_FROM_STRING, CONSTRUCT_ID_FROM_STRINGS; | ||
final Version version = FabricLoader.getInstance().getModContainer("minecraft").get().getMetadata().getVersion(); | ||
|
||
static { | ||
|
||
final Int2ObjectMap<MethodHandle> h = new Int2ObjectArrayMap<>(); | ||
|
||
final MethodHandles.Lookup lookup = MethodHandles.lookup(); | ||
String mapped = "unset"; | ||
|
||
try { | ||
|
||
final boolean is1206Minus = PehkuiVersionUtils.MINOR < 20 || (PehkuiVersionUtils.MINOR == 20 && PehkuiVersionUtils.PATCH <= 6);; | ||
|
||
// 1.20.6 was using "new Identifier(String)" | ||
if (is1206Minus) { | ||
h.put(9, lookup.unreflectConstructor(Identifier.class.getDeclaredConstructor(String.class))); | ||
h.put(10, lookup.unreflectConstructor(Identifier.class.getDeclaredConstructor(String.class, String.class))); | ||
} | ||
|
||
} catch (NoSuchMethodException | SecurityException | IllegalAccessException e) { | ||
ShieldDisruptor.LOGGER.error("Current name lookup: {}", mapped); | ||
ShieldDisruptor.LOGGER.catching(e); | ||
} | ||
|
||
CONSTRUCT_ID_FROM_STRING = h.get(9); | ||
CONSTRUCT_ID_FROM_STRINGS = h.get(10); | ||
|
||
} | ||
|
||
public static Identifier constructIdentifier(final String id) { | ||
if (CONSTRUCT_ID_FROM_STRING != null) { | ||
try { | ||
return (Identifier) CONSTRUCT_ID_FROM_STRING.invoke(id); | ||
} catch (final InvalidIdentifierException e) { | ||
throw e; | ||
} catch (final Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
return Identifier.of(id); | ||
} | ||
|
||
public static Identifier constructIdentifier(final String namespace, final String path) { | ||
if (CONSTRUCT_ID_FROM_STRINGS != null) { | ||
try { | ||
return (Identifier) CONSTRUCT_ID_FROM_STRINGS.invoke(namespace, path); | ||
} catch (final InvalidIdentifierException e) { | ||
throw e; | ||
} catch (final Throwable e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
return Identifier.of(namespace, path); | ||
} | ||
|
||
|
||
} |
124 changes: 124 additions & 0 deletions
124
src/main/java/me/mightyknight/sd/multiversion_mixin/version/PehkuiVersionUtils.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,124 @@ | ||
/* | ||
MIT License | ||
Copyright (c) 2019 - 2024 Virtuoel | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
*/ | ||
package me.mightyknight.sd.multiversion_mixin.version; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import net.fabricmc.loader.api.FabricLoader; | ||
import net.fabricmc.loader.api.SemanticVersion; | ||
import net.fabricmc.loader.api.Version; | ||
|
||
public class PehkuiVersionUtils | ||
{ | ||
private static final List<Predicate<String>> VERSION_PREDICATES = new ArrayList<>(); | ||
|
||
static | ||
{ | ||
final int[][] ranges = | ||
{ | ||
{1, 14, 4}, | ||
{1, 15, 2}, | ||
{1, 16, 5}, | ||
{1, 17, 1}, | ||
{1, 18, 2}, | ||
{1, 19, 4}, | ||
{1, 20, 6}, | ||
{1, 21, 0}, | ||
}; | ||
|
||
final String prefix = ".compat%s%s"; | ||
|
||
for (final int[] range : ranges) | ||
{ | ||
final int major = range[0]; | ||
final int minor = range[1]; | ||
|
||
final String predicatePrefix = String.format(prefix, major, minor); | ||
final String predicateMinor = predicatePrefix + "."; | ||
final String predicateMinorPlus = predicatePrefix + "plus."; | ||
final String predicateMinorMinus = predicatePrefix + "minus."; | ||
final String predicateMinorZero = predicatePrefix + "0."; | ||
final String predicateMinorZeroMinus = predicatePrefix + "0minus."; | ||
|
||
VERSION_PREDICATES.add(n -> n.contains(predicateMinor) && PehkuiVersionUtils.MINOR != minor); | ||
VERSION_PREDICATES.add(n -> n.contains(predicateMinorPlus) && PehkuiVersionUtils.MINOR < minor); | ||
VERSION_PREDICATES.add(n -> n.contains(predicateMinorMinus) && PehkuiVersionUtils.MINOR > minor); | ||
VERSION_PREDICATES.add(n -> n.contains(predicateMinorZero) && (PehkuiVersionUtils.MINOR != minor || PehkuiVersionUtils.PATCH != 0)); | ||
VERSION_PREDICATES.add(n -> n.contains(predicateMinorZeroMinus) && (PehkuiVersionUtils.MINOR > minor || (PehkuiVersionUtils.MINOR == minor && PehkuiVersionUtils.PATCH > 0))); | ||
|
||
final int maxPatch = range[2]; | ||
|
||
for (int i = 1; i <= maxPatch; i++) | ||
{ | ||
final int patch = i; | ||
|
||
final String predicatePatch = predicatePrefix + patch + "."; | ||
final String predicatePatchPlus = predicatePrefix + patch + "plus."; | ||
final String predicatePatchMinus = predicatePrefix + patch + "minus."; | ||
|
||
VERSION_PREDICATES.add(n -> n.contains(predicatePatch) && (PehkuiVersionUtils.MINOR != minor || PehkuiVersionUtils.PATCH != patch)); | ||
VERSION_PREDICATES.add(n -> n.contains(predicatePatchPlus) && (PehkuiVersionUtils.MINOR < minor || (PehkuiVersionUtils.MINOR == minor && PehkuiVersionUtils.PATCH < patch))); | ||
VERSION_PREDICATES.add(n -> n.contains(predicatePatchMinus) && (PehkuiVersionUtils.MINOR > minor || (PehkuiVersionUtils.MINOR == minor && PehkuiVersionUtils.PATCH > patch))); | ||
} | ||
} | ||
} | ||
|
||
public static boolean shouldApplyCompatibilityMixin(String mixinClassName) | ||
{ | ||
if (mixinClassName.contains(".compat")) | ||
{ | ||
for (final Predicate<String> predicate : VERSION_PREDICATES) | ||
{ | ||
if (predicate.test(mixinClassName)) | ||
{ | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@Nullable | ||
public static final SemanticVersion MINECRAFT_VERSION = lookupMinecraftVersion(); | ||
public static final int MAJOR = getVersionComponent(0); | ||
public static final int MINOR = getVersionComponent(1); | ||
public static final int PATCH = getVersionComponent(2); | ||
|
||
private static SemanticVersion lookupMinecraftVersion() | ||
{ | ||
final Version version = FabricLoader.getInstance().getModContainer("minecraft").get().getMetadata().getVersion(); | ||
|
||
return (SemanticVersion) (version instanceof SemanticVersion ? version : null); | ||
} | ||
|
||
private static int getVersionComponent(int pos) | ||
{ | ||
return MINECRAFT_VERSION != null ? MINECRAFT_VERSION.getVersionComponent(pos) : -1; | ||
} | ||
} |