Migrate to using Registry for LCIO and configs

This commit is contained in:
Daniella / Tove 2023-09-17 23:44:58 +02:00
parent 92bc4f150b
commit 083f74d94f
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
11 changed files with 59 additions and 110 deletions

View file

@ -78,7 +78,8 @@ dependencies {
}
jarLibs(files('libs/TuddyLIB.jar'))
implementation(files('libs/mcregistry.jar'))
// should NOT go into the jar.
implementation(files('libs/mcregistry-1.0.jar'))
jarLibs 'club.minnced:java-discord-rpc:2.0.2'

Binary file not shown.

Binary file not shown.

View file

@ -2,17 +2,19 @@ package com.baseband.client;
import com.baseband.client.command.CommandManager;
import com.baseband.client.data.LCIO;
import com.baseband.client.event.EventBus;
import com.baseband.client.event.FMLEventProcessor;
import com.baseband.client.module.Module;
import com.baseband.client.module.modules.*;
import de.tudbut.tools.Registry;
import de.tudbut.mcregistry.MCRegistry;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import tudbut.parsing.TCN;
import tudbut.tools.Lock;
import java.util.ArrayList;
@ -20,16 +22,19 @@ import java.util.ArrayList;
@Mod(modid = "baseband")
public class BaseBand {
public static int majorVersion = 1;
public static int buildNumber = 50;
public static String hash = "57ef0c99a21035ef";
public static int buildNumber = 69;
public static String hash = "1251518fc0644b2b";
public static String name = "BaseBand";
public long timeOfCompile = 1694973944897L;
public long timeOfCompile = 1694986783983L;
public CommandManager commandRegistry;
public EventBus eventBus;
public static ConfigManager configManager = new ConfigManager("config.bb");
public ArrayList<Module> modules = new ArrayList<>();
public static BaseBand INSTANCE; { INSTANCE = this; }
public static BaseBand INSTANCE;
public static Registry Registry;
private static TCN registryData;
{ INSTANCE = this; }
public static BaseBand getInstance() {
if(INSTANCE == null) {
@ -39,10 +44,6 @@ public class BaseBand {
}
public static final Logger log = LogManager.getLogger("BaseBand");
public static void handleLCIO(String key, Object value) {
}
@Mod.EventHandler
public void eventInit(FMLPreInitializationEvent event) {
onInit();
@ -50,9 +51,20 @@ public class BaseBand {
public void onInit() {
Utils.check();
String lcioClass = System.getProperty("org.bb.lcio");
System.setProperty("org.bb.lcio", "");
LCIO.init(lcioClass);
try {
Registry = MCRegistry.registerMod("baseband");
registryData = Registry.register("*");
} catch (Exception e) {
// tamper detected
Utils.crash();
}
// cant be a normal if statement because it might be null
if(registryData.get("LoaderPresent") != Boolean.TRUE) {
// do other stuff here later?
log.info("No loader present, but able to start anyway ==> Debug environment detected.");
}
// unset so this wont be discovered and manipulated
registryData.set("LoaderPresent", null);
commandRegistry = new CommandManager();
eventBus = new EventBus();
@ -73,7 +85,6 @@ public class BaseBand {
MinecraftForge.EVENT_BUS.register(new FMLEventProcessor());
try {
configManager.load();
for (Module m : modules) {
if(m.isEnabled()) {
MinecraftForge.EVENT_BUS.register(m);
@ -81,14 +92,14 @@ public class BaseBand {
}
}
} catch(Exception e) {
configManager.save();
ConfigManager.save();
}
new Thread(() -> {
Lock lock = new Lock();
while(true) {
lock.lock(10000);
configManager.save();
ConfigManager.save();
lock.waitHere();
}
}, "Config save thread").start();

View file

@ -13,38 +13,11 @@ import java.nio.file.Paths;
public class ConfigManager {
private final String path;
public ConfigManager(String path) {
this.path = path;
}
public void save() {
TCN tcn = new TCN();
tcn.set("base", ConfigSaverTCN2.write(BaseBand.INSTANCE, false, true));
public static void save() {
for (Module module : BaseBand.INSTANCE.modules) {
tcn.set(module.getClass().getSimpleName(), ConfigSaverTCN2.write(module, false, true));
}
try(OutputStream stream = Files.newOutputStream(Paths.get(path))) {
stream.write(JSON.write(tcn).getBytes());
} catch (IOException e) {
e.printStackTrace();
module.updateData();
}
}
public void load() {
try(InputStream stream = Files.newInputStream(Paths.get(path))) {
TCN tcn = JSON.read(new StreamReader(stream).readAllAsString());
ConfigSaverTCN2.read(tcn.getSub("base"), BaseBand.INSTANCE);
for (Module module : BaseBand.INSTANCE.modules) {
ConfigSaverTCN2.read(tcn.getSub(module.getClass().getSimpleName()), module);
}
} catch (IOException | JSONFormatException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}

View file

@ -1,30 +0,0 @@
package com.baseband.client.data;
import com.baseband.client.BaseBand;
import java.util.LinkedHashMap;
import java.util.Map;
public class LCIO {
private static Map<String, Object> params = new LinkedHashMap<>();
public static void init(String loaderLCIO) {
try {
params = (Map<String, Object>) Class.forName(loaderLCIO).getMethod("init", Class.class).invoke(null, LCIO.class);
for (Map.Entry<String, Object> entry : params.entrySet().toArray(new Map.Entry[0])) {
BaseBand.handleLCIO(entry.getKey(), entry.getValue());
}
} catch (Exception ignored) {
// ignore. no loader is fine after utils has checked it
}
}
public static void update(String param, Object value, Map<String, Object> params) {
LCIO.params = params;
BaseBand.handleLCIO(param, value);
}
}

View file

@ -1,9 +1,13 @@
package com.baseband.client.module;
import com.baseband.client.BaseBand;
import de.tudbut.tools.Registry;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.MinecraftForge;
import scala.xml.dtd.impl.Base;
import tudbut.obj.Save;
import tudbut.parsing.TCN;
import tudbut.tools.ConfigSaverTCN2;
public abstract class Module {
@Save
@ -27,7 +31,20 @@ public abstract class Module {
}
}
TCN data;
{
try {
data = BaseBand.Registry.register(this.toString());
if(data.getSub("@Save") != null)
ConfigSaverTCN2.read(data.getSub("@Save"), this);
} catch (IllegalAccessException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public void updateData() {
data.set("@Save", ConfigSaverTCN2.write(this, false, true));
}
protected static Minecraft mc = Minecraft.getMinecraft();

View file

@ -71,6 +71,8 @@ dependencies {
}
implementation project(path: ':Client')
// should NOT go into the jar.
implementation files('libs/mcregistry-1.0.jar')
annotationProcessor('org.spongepowered:mixin:0.8.5:processor') {

Binary file not shown.

View file

@ -1,32 +0,0 @@
package org.baseband.launcher.data;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Allows interop between loader and client
*/
public class LCIO {
private static Class<?> clientLCIO;
private static final Map<String, Object> params = new LinkedHashMap<>();
public static Map<String, Object> init(Class<?> lcioClient) {
clientLCIO = lcioClient;
return params;
}
public static void setClientParam(String param, Object data) {
params.put(param, data);
if(clientLCIO != null) {
// already initialized. update params
try {
clientLCIO.getMethod("update", String.class, Object.class, Map.class).invoke(null, param, data, params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}

View file

@ -1,11 +1,13 @@
package org.baseband.launcher.launch;
import de.tudbut.mcregistry.MCRegistry;
import de.tudbut.tools.Registry;
import net.minecraft.launchwrapper.Launch;
import org.baseband.launcher.Tweaker;
import org.baseband.launcher.data.LCIO;
import org.baseband.launcher.util.CustomClassloader;
import org.baseband.launcher.util.Key;
import sun.misc.Unsafe;
import tudbut.parsing.TCN;
import javax.swing.*;
import java.io.*;
@ -213,7 +215,12 @@ public class Loader {
}
}
System.setProperty("org.bb.lcio", LCIO.class.getName());
Registry baseBandRegistry = MCRegistry.registerMod("baseband");
TCN tcn = baseBandRegistry.register("*");
tcn.set("LoaderPresent", true);
// this is not the real mod, so unregister so the actual client can register it
baseBandRegistry.unregister("*", tcn);
MCRegistry.unregisterMod("baseband", baseBandRegistry);
Tweaker.log("Loaded.");
Tweaker.latch.countDown();