diff --git a/src/openonyx/misc/ConfigManager.java b/src/openonyx/misc/ConfigManager.java new file mode 100644 index 0000000..377e37d --- /dev/null +++ b/src/openonyx/misc/ConfigManager.java @@ -0,0 +1,696 @@ +package openonyx.misc; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.CopyOption; +import java.nio.file.Files; +import java.nio.file.LinkOption; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.nio.file.attribute.FileAttribute; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Stream; +import openonyx.core.MinecraftAccess; +import openonyx.core.OnyxClient; +import openonyx.features.Module; +import openonyx.features.ModuleCategory; +import openonyx.history.Setting; +import openonyx.history.SettingGroup; +import openonyx.history.SettingValue; +import openonyx.history.UndoManager; +import openonyx.utilities.FileUtils; +import org.apache.logging.log4j.Logger; + +public class ConfigManager implements MinecraftAccess { + private static final String I = ".json"; + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + public static final String i = "default"; + private String f; + private static final int H = 1; + private static final Pattern PATTERN = Pattern.compile("[a-zA-Z0-9_-]{1,64}"); + + public ConfigManagerData L(String string, List arrayList) { + Set set = arrayList == null ? Set.of() : new LinkedHashSet(arrayList); + arrayList = new ArrayList(); + JsonObject jsonObject; + if ((jsonObject = L(this.L(string))) == null) { + arrayList.addAll(set); + return new ConfigManagerData(0, List.copyOf(arrayList)); + } + + int ix = 0; + Iterator iterator = set.iterator(); + + label38: + while (true) { + Iterator iterator1 = iterator; + + while (iterator1.hasNext()) { + String string1 = (String)iterator.next(); + if (!string.equals(string1) && this.i(string1)) { + JsonObject jsonObject1; + JsonObject jsonObject2; + if ((jsonObject2 = L(jsonObject1 = this.L(string1))) != null) { + L(jsonObject, jsonObject2); + if (this.L(string1, jsonObject1)) { + ix++; + } else { + arrayList.add(string1); + } + continue label38; + } + + iterator1 = iterator; + arrayList.add(string1); + } else { + arrayList.add(string1); + iterator1 = iterator; + } + } + + return new ConfigManagerData(ix, List.copyOf(arrayList)); + } + } + + public String D(String string) { + ConfigManager configManager1 = this; + if (!configManager1.i(string)) { + return string; + } + + int ix; + for (int k = ix = 2; k < 10000; k = ++ix) { + String string1 = new StringBuilder().insert(0, "_").append(ix).toString(); + int j = Math.min(string.length(), 64 - string1.length()); + String string2 = new StringBuilder().insert(0, string.substring(0, j)).append(string1).toString(); + if (!configManager1.i(string2)) { + return string2; + } + } + + return null; + } + + public String d(String string) { + ConfigManager configManager1 = this; + if (!configManager1.i(string)) { + return null; + } + + int ix; + for (int k = ix = 1; k < 10000; k = ++ix) { + String string1 = ix == 1 ? "_copy" : new StringBuilder().insert(0, "_copy_").append(ix).toString(); + int j = Math.min(string.length(), 64 - string1.length()); + String string2 = new StringBuilder().insert(0, string.substring(0, j)).append(string1).toString(); + if (!configManager1.i(string2)) { + try { + Path path = configManager1.L(string); + Path path1 = configManager1.L(string2); + CopyOption[] copyOptions = new CopyOption[0]; + boolean bl = true; + Files.copy(path, path1, copyOptions); + return string2; + } catch (IOException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[3]; + boolean bl1 = true; + objects[0] = string; + objects[1] = string2; + objects[2] = iOException; + logger.error("Failed to duplicate config '{}' as '{}'", objects); + return null; + } + } + } + + return null; + } + + private Path D() { + return this.d().resolve("last-config.txt"); + } + + private boolean L(String string, JsonObject jsonObject) { + try { + Path path = this.L(); + FileAttribute[] fileAttributes = new FileAttribute[0]; + boolean bl1 = true; + Files.createDirectories(path, fileAttributes); + path = this.L(string); + byte[] b = GSON.toJson(jsonObject).getBytes(StandardCharsets.UTF_8); + OpenOption[] openOptions = new OpenOption[0]; + boolean bl = true; + Files.write(path, b, openOptions); + return true; + } catch (IOException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl2 = true; + objects[0] = string; + objects[1] = iOException; + logger.error("Failed to write config '{}'", objects); + return false; + } + } + + private void L(SettingValue settingValue, JsonElement jsonElement) { + if (jsonElement != null && !jsonElement.isJsonNull()) { + try { + settingValue.fromJson(jsonElement); + } catch (RuntimeException runtimeException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl = true; + objects[0] = settingValue.getName(); + objects[1] = runtimeException; + logger.warn("Failed to load setting '{}'", objects); + } + } + } + + public String D() { + return this.f; + } + + public boolean F(String string) { + ConfigManager configManager1 = this; + if (!configManager1.h(string)) { + return false; + } + + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("version", 1); + jsonObject.add("modules", configManager1.L()); + Path path = configManager1.L().resolve(new StringBuilder().insert(0, string).append(".json").append(".tmp").toString()); + + try { + Path path1 = configManager1.L(); + FileAttribute[] fileAttributes = new FileAttribute[0]; + boolean bl1 = true; + Files.createDirectories(path1, fileAttributes); + byte[] b = GSON.toJson(jsonObject).getBytes(StandardCharsets.UTF_8); + OpenOption[] openOptions = new OpenOption[0]; + boolean bl = true; + Files.write(path, b, openOptions); + + try { + Path path3 = configManager1.L(string); + CopyOption[] copyOptions1 = new CopyOption[2]; + bl = true; + copyOptions1[0] = StandardCopyOption.ATOMIC_MOVE; + copyOptions1[1] = StandardCopyOption.REPLACE_EXISTING; + Files.move(path, path3, copyOptions1); + } catch (AtomicMoveNotSupportedException atomicMoveNotSupportedException) { + Path path2 = configManager1.L(string); + CopyOption[] copyOptions = new CopyOption[1]; + bl = true; + copyOptions[0] = StandardCopyOption.REPLACE_EXISTING; + Files.move(path, path2, copyOptions); + } + + return true; + } catch (IOException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl2 = true; + objects[0] = string; + objects[1] = iOException; + logger.error("Failed to save config '{}'", objects); + return false; + } + } + + private JsonObject L(String string) { + ConfigManager configManager1 = this; + if (!configManager1.i(string)) { + return null; + } + + try { + String string1 = new String(Files.readAllBytes(configManager1.L(string)), StandardCharsets.UTF_8); + JsonElement jsonElement; + return (jsonElement = new JsonParser().parse(string1)).isJsonObject() ? jsonElement.getAsJsonObject() : null; + } catch (IOException | RuntimeException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl = true; + objects[0] = string; + objects[1] = iOException; + logger.error("Failed to read config '{}'", objects); + return null; + } + } + + private static void L(Setting settingValue) { + Setting setting = settingValue; + if (setting instanceof SettingValue) { + (settingValue = (SettingValue)setting).reset(); + } else { + if (setting instanceof SettingGroup) { + SettingGroup settingGroup; + if ((settingGroup = (SettingGroup)setting).getEnabled() != null) { + settingGroup.getEnabled().reset(); + } + + for (Iterator iterator1 = iterator = settingGroup.getSettings().iterator(); iterator1.hasNext(); iterator1 = iterator) { + L((Setting)iterator.next()); + } + } + } + } + + public void i() { + ConfigManager configManager1 = this; + String string; + if ((string = configManager1.L()) == null || !configManager1.D(string)) { + configManager1.D("default"); + } + } + + private void d(String string) { + ConfigManager configManager1 = this; + + try { + Path path = configManager1.d(); + FileAttribute[] fileAttributes = new FileAttribute[0]; + boolean bl1 = true; + Files.createDirectories(path, fileAttributes); + path = configManager1.D(); + byte[] b = string.getBytes(StandardCharsets.UTF_8); + OpenOption[] openOptions = new OpenOption[0]; + boolean bl = true; + Files.write(path, b, openOptions); + } catch (IOException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl2 = true; + objects[0] = string; + objects[1] = iOException; + logger.error("Failed to remember config '{}'", objects); + } + } + + public boolean l(String string) { + if (this.h(string) && !this.i(string)) { + this.D(); + return this.F(string); + } else { + return false; + } + } + + public String d() { + return this.f == null ? "default" : this.f; + } + + private static void L(JsonObject jsonObject, JsonObject jsonObject1) { + JsonObject jsonObject2 = jsonObject; + Iterator iterator = OnyxClient.moduleManager.getModules().iterator(); + + label37: + while (true) { + Iterator iterator1 = iterator; + + while (iterator1.hasNext()) { + Module module; + if ((module = (Module)iterator.next()).getModuleCategory() != ModuleCategory.RENDER && module.getModuleCategory() != ModuleCategory.HUD) { + iterator1 = iterator; + } else { + JsonElement jsonElement; + if ((jsonElement = jsonObject2.get(module.getName())) == null) { + continue label37; + } + + if (!jsonElement.isJsonObject()) { + iterator1 = iterator; + } else { + jsonElement = jsonElement.getAsJsonObject(); + JsonElement jsonElement1; + JsonObject jsonObject3 = (jsonElement1 = jsonObject1.get(module.getName())) != null && jsonElement1.isJsonObject() + ? jsonElement1.getAsJsonObject() + : new JsonObject(); + iterator1 = iterator; + L(jsonElement, jsonObject3, "enabled"); + L(jsonElement, jsonObject3, "settings"); + jsonObject1.add(module.getName(), jsonObject3); + } + } + } + + return; + } + } + + public boolean h(String string) { + return string != null && PATTERN.matcher(string).matches(); + } + + public Path d() { + return new File(mc.mcDataDir, "onyx").toPath(); + } + + private void L(String string) { + this.f = string; + this.d(string); + } + + public Path L(String string) { + return this.L().resolve(new StringBuilder().insert(0, string).append(".json").toString()); + } + + private void L(JsonObject jsonObject, int i) { + ConfigManager configManager1 = this; + Iterator iterator = OnyxClient.moduleManager.getModules().iterator(); + + label53: + while (true) { + Iterator iterator3 = iterator; + + while (iterator3.hasNext()) { + Module module = (Module)iterator.next(); + JsonElement jsonElement; + if ((jsonElement = jsonObject.get(module.getName())) == null) { + continue label53; + } + + if (!jsonElement.isJsonObject()) { + iterator3 = iterator; + } else { + JsonObject jsonObject2; + JsonObject jsonObject3 = jsonObject2 = jsonElement.getAsJsonObject(); + configManager1.L(module.getBind(), jsonObject2.get("bind")); + configManager1.L(module.getBindMode(), jsonObject2.get("bindMode")); + JsonElement jsonElement1; + if ((jsonElement1 = jsonObject3.get("settings")) != null && jsonElement1.isJsonObject()) { + JsonObject jsonObject1 = jsonElement1.getAsJsonObject(); + + for (Iterator iterator2 = iterator1 = module.getSettings().iterator(); iterator2.hasNext(); iterator2 = iterator1) { + SettingGroup.loadSetting((Setting)iterator1.next(), jsonObject1); + } + } + + if (module instanceof MiscListener_629) { + MiscListener_629 miscListener_629; + (miscListener_629 = (MiscListener_629)module).L(); + } + + if (module.D()) { + JsonElement jsonElement2; + if ((jsonElement2 = jsonObject2.get("enabled")) != null && jsonElement2.isJsonPrimitive() && jsonElement2.getAsJsonPrimitive().isBoolean()) { + module.setEnabled(jsonElement2.getAsBoolean()); + } + continue label53; + } + + iterator3 = iterator; + } + } + + return; + } + } + + private static JsonObject L(JsonObject jsonObject) { + JsonObject jsonObject1 = jsonObject; + if (jsonObject1 == null) { + return null; + } + + JsonElement jsonElement; + return (jsonElement = jsonObject1.get("modules")) != null && jsonElement.isJsonObject() ? jsonElement.getAsJsonObject() : null; + } + + public boolean L() { + return this.F(this.d()); + } + + public boolean I(String string) { + return string != null && string.equals(this.f); + } + + private void D() { + UndoManager.L(() -> { + Iterator iterator; + for (Iterator iterator3 = iterator = OnyxClient.moduleManager.getModules().iterator(); iterator3.hasNext(); iterator3 = iterator) { + Module module; + if ((module = (Module)iterator.next()).D()) { + module.setEnabled(false); + } + + Iterator iterator1; + for (Iterator iterator2 = iterator1 = module.getSettings().iterator(); iterator2.hasNext(); iterator2 = iterator1) { + L((Setting)iterator1.next()); + } + + if (module instanceof MiscListener_629) { + MiscListener_629 miscListener_629; + (miscListener_629 = (MiscListener_629)module).L(); + } + + module.refreshSettings(); + } + }); + } + + public boolean i(String string) { + if (this.h(string)) { + Path path = this.L(string); + LinkOption[] linkOptions = new LinkOption[0]; + boolean bl = true; + if (Files.isRegularFile(path, linkOptions)) { + return true; + } + } + + return false; + } + + private String L() { + ConfigManager configManager1 = this; + Path path; + Path path1 = path = configManager1.D(); + LinkOption[] linkOptions = new LinkOption[0]; + boolean bl = true; + if (!Files.isRegularFile(path1, linkOptions)) { + return null; + } + + try { + String string = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim(); + return configManager1.i(string) ? string : null; + } catch (IOException iOException) { + OnyxClient.LOGGER.error("Failed to read the last config", iOException); + return null; + } + } + + private static JsonElement L(JsonElement jsonElement) { + return new JsonParser().parse(jsonElement.toString()); + } + + public void d() { + ConfigManager configManager1 = this; + + try { + Path path = configManager1.L(); + FileAttribute[] fileAttributes = new FileAttribute[0]; + boolean bl = true; + Files.createDirectories(path, fileAttributes); + } catch (IOException iOException) { + OnyxClient.LOGGER.error("Failed to create config folder", iOException); + return; + } + + FileUtils.L(configManager1.L()); + } + + public boolean D(String string) { + ConfigManager configManager1 = this; + if (!configManager1.i(string)) { + return false; + } + + JsonObject jsonObject; + try { + String string1 = new String(Files.readAllBytes(configManager1.L(string)), StandardCharsets.UTF_8); + jsonObject = new JsonParser().parse(string1).getAsJsonObject(); + } catch (IOException | RuntimeException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl = true; + objects[0] = string; + objects[1] = iOException; + logger.error("Failed to read config '{}'", objects); + return false; + } + + int ix = 1; + JsonElement jsonElement; + if ((jsonElement = jsonObject.get("version")) != null && jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isNumber()) { + ix = jsonElement.getAsInt(); + } + + JsonElement jsonElement1; + if ((jsonElement1 = jsonObject.get("modules")) != null && jsonElement1.isJsonObject()) { + jsonElement1 = jsonElement1.getAsJsonObject(); + int j = ix; + UndoManager.L(() -> this.L(jsonElement1, j)); + if (ix < 1) { + configManager1.F(string); + } + + configManager1.L(string); + return true; + } else { + return false; + } + } + + private JsonObject L() { + JsonObject jsonObject1 = new JsonObject(); + Iterator iterator1; + Iterator iterator2 = iterator1 = OnyxClient.moduleManager.getModules().iterator(); + + while (iterator2.hasNext()) { + Module module = (Module)iterator1.next(); + JsonObject jsonObject = new JsonObject(); + if (module.D()) { + jsonObject.addProperty("enabled", module.isEnabled()); + } + + jsonObject.add("bind", module.getBind().toJson()); + jsonObject.add("bindMode", module.getBindMode().toJson()); + JsonObject jsonObject2 = new JsonObject(); + Iterator iterator; + iterator2 = iterator = module.getSettings().iterator(); + + while (iterator2.hasNext()) { + Setting setting; + if (!(setting = (Setting)iterator.next()).hasVisibilityCondition()) { + iterator2 = iterator; + } else { + jsonObject2.add(setting.getName(), setting.toJson()); + iterator2 = iterator; + } + } + + jsonObject.add("settings", jsonObject2); + iterator2 = iterator1; + jsonObject1.add(module.getName(), jsonObject); + } + + return jsonObject1; + } + + public boolean L(String string) { + ConfigManager configManager1 = this; + if (!configManager1.h(string)) { + return false; + } + + try { + boolean bl; + if ((bl = Files.deleteIfExists(configManager1.L(string))) && configManager1.I(string)) { + configManager1.f = null; + configManager1.L(); + } + + return bl; + } catch (IOException iOException) { + Logger logger = OnyxClient.LOGGER; + Object[] objects = new Object[2]; + boolean bl1 = true; + objects[0] = string; + objects[1] = iOException; + logger.error("Failed to delete config '{}'", objects); + return false; + } + } + + private static void L(JsonObject jsonObject, JsonObject jsonObject1, String string) { + JsonElement jsonElement; + if ((jsonElement = jsonObject.get(string)) == null) { + jsonObject1.remove(string); + } else { + jsonObject1.add(string, L(jsonElement)); + } + } + + public Path L() { + return this.d().resolve("configs"); + } + + public List L() { + ConfigManager configManager1 = this; + Path path = configManager1.L(); + LinkOption[] linkOptions = new LinkOption[0]; + boolean bl = true; + if (!Files.isDirectory(path, linkOptions)) { + return List.of(); + } + + try { + Stream stream = Files.list(configManager1.L()); + + List list; + try { + list = stream.filter(path -> { + LinkOption[] linkOptionsx = new LinkOption[0]; + boolean blx = true; + return Files.isRegularFile(pathx, linkOptionsx); + }) + .map(path -> pathx.getFileName().toString()) + .filter(string -> string.endsWith(".json")) + .map(string -> string.substring(0, string.length() - ".json".length())) + .filter(configManager1::h) + .sorted() + .toList(); + } catch (Throwable throwable1) { + if (stream != null) { + try { + stream.close(); + } catch (Throwable throwable) { + throwable1.addSuppressed(throwable); + throw throwable1; + } + + throw throwable1; + } + + throw throwable1; + } + + if (stream != null) { + stream.close(); + } + + return list; + } catch (IOException iOException) { + OnyxClient.LOGGER.error("Failed to list configs", iOException); + return List.of(); + } + } + + private void L() { + ConfigManager configManager1 = this; + + try { + Files.deleteIfExists(configManager1.D()); + } catch (IOException iOException) { + OnyxClient.LOGGER.error("Failed to forget the last config", iOException); + } + } +} diff --git a/src/openonyx/misc/ConfigManagerData.java b/src/openonyx/misc/ConfigManagerData.java new file mode 100644 index 0000000..0d05d95 --- /dev/null +++ b/src/openonyx/misc/ConfigManagerData.java @@ -0,0 +1,6 @@ +package openonyx.misc; + +import java.util.List; + +public record ConfigManagerData(int copied, List failed) { +} diff --git a/src/openonyx/misc/MiscListener_629.java b/src/openonyx/misc/MiscListener_629.java new file mode 100644 index 0000000..8fd964a --- /dev/null +++ b/src/openonyx/misc/MiscListener_629.java @@ -0,0 +1,5 @@ +package openonyx.misc; + +public interface MiscListener_629 { + void L(); +}