Skip to content

Commit

Permalink
fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
Ecdcaeb committed Aug 31, 2024
1 parent 15a9381 commit f10eeea
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 50 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ if (useMirror) apply from: "mirror.gradle"
apply plugin: "net.minecraftforge.gradle.forge"
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.

version = "1.4 for 1.12.2-1.8.8"
version = "1.5 for 1.12.2-1.8.8"
group = "com.ideallandframework.silent_assassin" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "redirectionor"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.Hileb.teampotato.redirectionor;

import net.minecraftforge.fml.relauncher.CoreModManager;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.io.File;
import java.util.Map;

@IFMLLoadingPlugin.Name(Redirectionor.MODID)
public class Redirectionor implements IFMLLoadingPlugin {

public Redirectionor(){
try{
RedirectionorConfig.initConfig();
Expand All @@ -15,10 +20,17 @@ public Redirectionor(){
}
}

public static void makeFMLCorePluginContainsFMLMod(File file){
String name = file.getName();
CoreModManager.getIgnoredMods().remove(name);
CoreModManager.getReparseableCoremods().add(name);
}

public static final String MODID = "redirectionor";

public static final String TRANSFORMERCLASS = "com.Hileb.teampotato.redirectionor.RedirectionorTransformer";

public static final Logger LOGGER = LogManager.getLogger(MODID);

@Override
public String[] getASMTransformerClass() {
Expand All @@ -37,7 +49,11 @@ public String getSetupClass() {
}

@Override
public void injectData(Map<String, Object> data) {}
public void injectData(Map<String, Object> data) {
// if (data.containsKey("coremodLocation")){
// makeFMLCorePluginContainsFMLMod((File)data.get("coremodLocation"));
// }
}

@Override
public String getAccessTransformerClass() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,63 +6,158 @@
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import net.minecraft.launchwrapper.Launch;
import net.minecraftforge.fml.common.ICrashCallable;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;

/**
* @Project Redirectionor
* @Author Hileb
* @Date 2023/9/9 18:14
**/
public class RedirectionorConfig {

public static File CONFIG_FILE = null;

public static void decode(JsonObject jsonObject){
try{
if (jsonObject.has("printTransformedClasses")){
Config.printTransformedClasses = jsonObject.get("printTransformedClasses").getAsBoolean();
}
Config.isBlock = Config.setBlocking(jsonObject.get("type").getAsString());
{
JsonArray contains = jsonObject.get("contains").getAsJsonArray();
Config.contains = new HashSet<>(contains.size());
for (JsonElement element : contains) {
Config.contains.add(element.getAsString());
}
}
{
JsonArray prefix = jsonObject.get("prefix").getAsJsonArray();
Config.prefix = new HashSet<>(prefix.size());
for (JsonElement element : prefix) {
Config.prefix.add(element.getAsString());
}
}
}catch (Throwable e){
throw new RuntimeException("Could not read the config", e);
}
}

public static JsonObject encode(){
JsonObject json = new JsonObject();
json.addProperty("printTransformedClasses", Config.isBlock);
json.addProperty("type", Config.isBlock ? "block" : "allow");

JsonArray contains = new JsonArray();
for(String element : Config.contains){
contains.add(element);
}

JsonArray prefix = new JsonArray();
for(String element : Config.prefix){
contains.add(element);
}

json.add("contains", contains);
return json;
}

public static void save(){
if (CONFIG_FILE == null) initConfig();
try (PrintWriter pw = new PrintWriter(CONFIG_FILE,"UTF-8")){
pw.println(encode());
} catch (IOException e) {
throw new RuntimeException(e);
}
}


public static void initConfig(){
File gameRunRoot = Launch.minecraftHome;
File config=new File(gameRunRoot,"config");
File config = new File(gameRunRoot,"config");

if (!config.exists()){
config.mkdir();
}
File file=new File(config,"redirectionor_cfg.json");
if (!file.exists()){

File file = new File(config,"redirectionor_cfg.json");
if (file.exists()){
try {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject)jsonParser.parse(new String(Files.readAllBytes(file.toPath())));
decode(jsonObject);
} catch (IOException e) {
throw new RuntimeException("Could not read the config", e);
} catch (JsonSyntaxException e){
throw new RuntimeException("The Json is bad", e);
}
} else {
try {
file.createNewFile();
PrintWriter pw = new PrintWriter(file,"UTF-8");
pw.println("{\"printTransformedClasses\":true,\"type\":\"block\", \"contains\":[]}");
pw.close();
} catch (IOException e) {
throw new RuntimeException(e);
throw new RuntimeException("Could not operate config file", e);
}
}
try {
JsonParser jsonParser = new JsonParser();
JsonObject jsonObject = (JsonObject)jsonParser.parse(new String(Files.readAllBytes(file.toPath())));
if (jsonObject.has("printTransformedClasses")){
RedirectionorTransformer.isDeBug = jsonObject.get("printTransformedClasses").getAsBoolean();
}
Config.type = jsonObject.get("type").getAsString();
JsonArray contains = jsonObject.get("contains").getAsJsonArray();
for(JsonElement element : contains){
Config.contains.add(element.getAsString());
}
try (PrintWriter pw = new PrintWriter(file,"UTF-8")){
pw.println(encode());
} catch (IOException e) {
throw new RuntimeException("Could not read the config", e);
} catch (ClassCastException e){
throw new RuntimeException("The config is not json", e);
} catch (JsonSyntaxException e){
throw new RuntimeException("The Json is bad", e);
throw new RuntimeException(e);
}
CONFIG_FILE = file;
}
public static class Config{
public static String type = "block";
public static boolean printTransformedClasses = false;
public static boolean isBlock = true;
public static HashSet<String> contains = new HashSet<>();
public static boolean isBlocking(){
if ("block".equals(type.toLowerCase(Locale.ENGLISH)))return true;
else if ("allow".equals(type.toLowerCase(Locale.ENGLISH)))return false;
else throw new IllegalArgumentException("unknown type for config/redirectionor_cfg.json :"+type+" it should be \"block\" or \"allow\"");
public static HashSet<String> prefix = new HashSet<>();
public static boolean setBlocking(String s){
if ("block".equals(s.toLowerCase(Locale.ENGLISH))) return true;
else if ("allow".equals(s.toLowerCase(Locale.ENGLISH))) return false;
else throw new IllegalArgumentException("unknown type for config/redirectionor_cfg.json :" + s + " it should be \"block\" or \"allow\"");
}

}

// For 1.9.4 and above, scanned, but coremod and 1.8.8 care, we inject it.
public static class CrashHandler implements ICrashCallable {

@Override
public String call() {
handleCrash();
save();
return "An automatic prefix block config generated";
}

@Override
public String getLabel() {
return "Redirectionor is enabled. Check your enums!";
}

public static final Object lock = new Object();

public static void handleCrash(){
synchronized (lock){
if (Config.isBlock){
for(Map.Entry<Thread, StackTraceElement[]> thread : Thread.getAllStackTraces().entrySet()){
for(StackTraceElement elements : thread.getValue()){
try {
if (RedirectionorFastUtil.isEnum(Launch.classLoader.getClassBytes(elements.getClassName()))){
Config.prefix.add(elements.getClassName());
}
} catch (IOException ignored) {
//declare but never throws
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.google.common.eventbus.EventBus;
import net.minecraftforge.fml.common.DummyModContainer;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.LoadController;
import net.minecraftforge.fml.common.ModMetadata;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;

/**
* @Project Redirectionor
Expand All @@ -17,7 +19,7 @@ public RedirectionorContainer(){
metadata.modId=Redirectionor.MODID;
metadata.name="Redirectionor";
metadata.description="Redirectionor is the implementation of this concept, specifically for the Direction enum class stuff, to reduce the required memory of the game.";
metadata.version="1.4 for 1.12.2-1.8.8";
metadata.version="1.5 for 1.12.2-1.8.8";
metadata.url="https://www.curseforge.com/minecraft/mc-mods/redirectionor";
metadata.logoFile="/icon_redirectionor.png";
metadata.authorList.add("MCTeamPotato");
Expand All @@ -26,6 +28,11 @@ public RedirectionorContainer(){
}
@Override
public boolean registerBus(EventBus bus, LoadController controller) {
onConstruct();
return true;
}

public static void onConstruct(){
ObfuscationReflectionHelper.setPrivateValue(FMLCommonHandler.class, FMLCommonHandler.instance(), new RedirectionorConfig.CrashHandler(), "crashCallables");
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package com.Hileb.teampotato.redirectionor;

import it.unimi.dsi.fastutil.ints.Int2IntArrayMap;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;

import java.lang.reflect.Modifier;

/**
* @Project Redirectionor
* @Author Hileb
* @Date 2023/8/29 22:00
**/
public class RedirectionorFastUtil {
public static boolean isEnum(byte[] clazz){
int constantsCount = readUnsignedShort(clazz,8);
if (clazz == null || clazz.length < 8) return false;
int constantsCount = readUnsignedShort(clazz, 8);
int passcount = 10;
for(int i = 1; i < constantsCount; i++){
int size=0;
Expand All @@ -32,8 +27,7 @@ public static boolean isEnum(byte[] clazz){
size = 9;
break;
case 1:
int UTFSize=readUnsignedShort(clazz,passcount + 1);
size = 3 + UTFSize;
size = 3 + readUnsignedShort(clazz,passcount + 1);
break;
case 15:
size = 4;
Expand All @@ -50,12 +44,26 @@ public static boolean isEnum(byte[] clazz){
public static int readUnsignedShort(byte[] b, int index) {
return ((b[index] & 0xFF) << 8) | (b[index + 1] & 0xFF);
}

public static boolean isAvailable(String name){
return RedirectionorConfig.Config.isBlock != (isPrefixed(name) || isContained(name));
}

public static boolean isContained(String name){
for(String modid : RedirectionorConfig.Config.contains){
if (name.contains(modid)){
return !RedirectionorConfig.Config.isBlocking();
return true;
}
}
return false;
}

public static boolean isPrefixed(String name){
for(String modid : RedirectionorConfig.Config.prefix){
if (name.startsWith(modid)){
return true;
}
}
return RedirectionorConfig.Config.isBlocking();
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.MethodNode;

import java.util.ListIterator;
Expand All @@ -17,7 +16,6 @@
* @Date 2023/8/24 12:31
**/
public class RedirectionorTransformer implements IClassTransformer {
public static boolean isDeBug = true;
@Override
public byte[] transform(String name, String transformedName, byte[] basicClass) {
try{
Expand All @@ -26,10 +24,9 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
}
ClassReader classReader = new ClassReader(basicClass);
ClassNode cn = new ClassNode();
classReader.accept(cn,0);
classReader.accept(cn, 0);
for(MethodNode mn:cn.methods){
if ("values".equals(mn.name) && mn.desc.startsWith("()")){
InsnList il=mn.instructions;
ListIterator<AbstractInsnNode> iterator = mn.instructions.iterator();
AbstractInsnNode node;
while (iterator.hasNext()){
Expand All @@ -39,12 +36,13 @@ public byte[] transform(String name, String transformedName, byte[] basicClass)
iterator.remove();
}
}
if (isDeBug)System.out.println("Redirect "+transformedName);
if (RedirectionorConfig.Config.printTransformedClasses) Redirectionor.LOGGER.info(transformedName);
ClassWriter classWriter = new ClassWriter(classReader, 0);
cn.accept(classWriter);
return classWriter.toByteArray();
}
}
ClassWriter classWriter=new ClassWriter(classReader, 0);
cn.accept(classWriter);
return classWriter.toByteArray();
return basicClass;
}catch (Exception ignore){
return basicClass;
}
Expand Down

0 comments on commit f10eeea

Please sign in to comment.