I feel dirty

This commit is contained in:
JFronny 2020-12-12 18:21:50 +01:00
parent 0f8ea0fe12
commit e08615177a
4 changed files with 165 additions and 30 deletions

View File

@ -9,6 +9,7 @@ import org.apache.logging.log4j.Logger;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -38,7 +39,15 @@ public class FabricLoaderInterface {
public static void synchronize(FabricLoader fabricLoader) { public static void synchronize(FabricLoader fabricLoader) {
try { try {
modsField.set(fabricLoader, Collections.synchronizedList((List<ModContainer>)modsField.get(fabricLoader))); modsField.set(fabricLoader, new IteratorCallbackList((List<ModContainer>)modsField.get(fabricLoader),
(s) -> {
try {
modsField.set(fabricLoader, s);
} catch (IllegalAccessException e) {
logger.error("Failed to reset mods field", e);
e.printStackTrace();
}
}));
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
logger.error("Failed to make mods list synchronized.", e); logger.error("Failed to make mods list synchronized.", e);
throw new IllegalStateException(e); throw new IllegalStateException(e);
@ -62,22 +71,4 @@ public class FabricLoaderInterface {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
} }
/*public static int getModCount(FabricLoader fabricLoader) {
try {
return abstractListModCount.getInt(getMods(fabricLoader));
} catch (IllegalAccessException e) {
logger.error("Failed to get modCount from fabric-loader.", e);
throw new IllegalStateException(e);
}
}
public static void setModCount(FabricLoader fabricLoader, int modCount) {
try {
abstractListModCount.setInt(getMods(fabricLoader), modCount);
} catch (IllegalAccessException e) {
logger.error("Failed to set modCount in fabric-loader.", e);
throw new IllegalStateException(e);
}
}*/
} }

View File

@ -0,0 +1,143 @@
package io.gitlab.jfronny.modsmod;
import net.fabricmc.loader.ModContainer;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.function.Consumer;
public class IteratorCallbackList implements List<ModContainer> {
private List<ModContainer> containers;
private Consumer<List<ModContainer>> reset;
boolean modified = false;
public IteratorCallbackList(List<ModContainer> base, Consumer<List<ModContainer>> reset) {
containers = Collections.synchronizedList(base);
this.reset = reset;
}
@Override
public int size() {
return containers.size();
}
@Override
public boolean isEmpty() {
return containers.isEmpty();
}
@Override
public boolean contains(Object o) {
return containers.contains(o);
}
@NotNull
@Override
public Iterator<ModContainer> iterator() {
if (!modified) {
PreLaunch.init();
modified = true;
}
reset.accept(containers);
return containers.iterator();
}
@NotNull
@Override
public Object[] toArray() {
return containers.toArray();
}
@NotNull
@Override
public <T> T[] toArray(@NotNull T[] ts) {
return containers.toArray(ts);
}
@Override
public boolean add(ModContainer modContainer) {
return containers.add(modContainer);
}
@Override
public boolean remove(Object o) {
return containers.remove(o);
}
@Override
public boolean containsAll(@NotNull Collection<?> collection) {
return containers.containsAll(collection);
}
@Override
public boolean addAll(@NotNull Collection<? extends ModContainer> collection) {
return containers.addAll(collection);
}
@Override
public boolean addAll(int i, @NotNull Collection<? extends ModContainer> collection) {
return containers.addAll(i, collection);
}
@Override
public boolean removeAll(@NotNull Collection<?> collection) {
return containers.removeAll(collection);
}
@Override
public boolean retainAll(@NotNull Collection<?> collection) {
return containers.removeAll(collection);
}
@Override
public void clear() {
containers.clear();
}
@Override
public ModContainer get(int i) {
return containers.get(i);
}
@Override
public ModContainer set(int i, ModContainer modContainer) {
return containers.set(i, modContainer);
}
@Override
public void add(int i, ModContainer modContainer) {
containers.add(i, modContainer);
}
@Override
public ModContainer remove(int i) {
return containers.remove(i);
}
@Override
public int indexOf(Object o) {
return containers.indexOf(o);
}
@Override
public int lastIndexOf(Object o) {
return containers.lastIndexOf(o);
}
@NotNull
@Override
public ListIterator<ModContainer> listIterator() {
return containers.listIterator();
}
@NotNull
@Override
public ListIterator<ModContainer> listIterator(int i) {
return containers.listIterator(i);
}
@NotNull
@Override
public List<ModContainer> subList(int i, int i1) {
return containers.subList(i, i1);
}
}

View File

@ -1,14 +1,12 @@
package io.gitlab.jfronny.modsmod; package io.gitlab.jfronny.modsmod;
import net.fabricmc.loader.api.LanguageAdapter; import net.fabricmc.loader.api.LanguageAdapter;
import net.fabricmc.loader.api.LanguageAdapterException;
import net.fabricmc.loader.api.ModContainer;
public class ModsModAdapter implements LanguageAdapter { public class ModsModAdapter implements LanguageAdapter {
@Override @Override
public native <T> T create(net.fabricmc.loader.api.ModContainer mod, String value, Class<T> type); public native <T> T create(net.fabricmc.loader.api.ModContainer mod, String value, Class<T> type);
static { static {
new PreLaunch().onPrePrePreLaunch(); PreLaunch.prepare();
} }
} }

View File

@ -26,8 +26,10 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
public class PreLaunch { public class PreLaunch {
public void onPrePrePreLaunch() { static HashSet<File> m;
FabricLoader loader = FabricLoader.INSTANCE; static FabricLoader loader;
public static void prepare() {
loader = FabricLoader.INSTANCE;
//Load config //Load config
AutoConfig.register(Cfg.class, JanksonConfigSerializer::new); AutoConfig.register(Cfg.class, JanksonConfigSerializer::new);
Cfg cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig(); Cfg cfg = AutoConfig.getConfigHolder(Cfg.class).getConfig();
@ -60,7 +62,7 @@ public class PreLaunch {
System.err.println("Failed to validate modsmod config cache, caching will not be available"); System.err.println("Failed to validate modsmod config cache, caching will not be available");
e.printStackTrace(); e.printStackTrace();
} }
HashSet<File> m = new HashSet<>(); m = new HashSet<>();
//Generate mods //Generate mods
for (int i = 0; i < cfg.modsCount; i++) { for (int i = 0; i < cfg.modsCount; i++) {
File f = new File(path, "f" + (i + 1) + ".jar"); File f = new File(path, "f" + (i + 1) + ".jar");
@ -108,15 +110,16 @@ public class PreLaunch {
} }
System.gc(); System.gc();
} }
//FabricLoaderInterface.synchronize(loader); FabricLoaderInterface.synchronize(loader);
//int oldCount = FabricLoaderInterface.getModCount(loader); }
public static void init() {
for (File f : m) { for (File f : m) {
loadMod(loader, f.toPath()); loadMod(loader, f.toPath());
} }
//FabricLoaderInterface.setModCount(loader, oldCount);
} }
private void loadMod(FabricLoader loader, Path modPath) { private static void loadMod(FabricLoader loader, Path modPath) {
ModCandidate candidate = parseMod(modPath); ModCandidate candidate = parseMod(modPath);
if (loader.isDevelopmentEnvironment()) { if (loader.isDevelopmentEnvironment()) {
@ -127,7 +130,7 @@ public class PreLaunch {
FabricLauncherBase.getLauncher().propose(candidate.getOriginUrl()); FabricLauncherBase.getLauncher().propose(candidate.getOriginUrl());
} }
private ModCandidate parseMod(Path modPath) { private static ModCandidate parseMod(Path modPath) {
try { try {
FileSystemUtil.FileSystemDelegate jarFs = FileSystemUtil.getJarFileSystem(modPath, false); FileSystemUtil.FileSystemDelegate jarFs = FileSystemUtil.getJarFileSystem(modPath, false);