| 1 | package com.tangykiwi.kiwiclient.util;
|
| 2 |
|
| 3 | import static com.tangykiwi.kiwiclient.KiwiClient.LOGGER;
|
| 4 | import static com.tangykiwi.kiwiclient.KiwiClient.mc;
|
| 5 |
|
| 6 | import java.io.FileWriter;
|
| 7 | import java.nio.file.Files;
|
| 8 | import java.nio.file.NoSuchFileException;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.nio.file.Paths;
|
| 11 | import java.util.ArrayList;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.HashMap;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 |
|
| 17 | import com.google.gson.Gson;
|
| 18 | import com.google.gson.GsonBuilder;
|
| 19 | import com.google.gson.JsonElement;
|
| 20 | import com.google.gson.JsonObject;
|
| 21 | import com.google.gson.JsonParseException;
|
| 22 | import com.google.gson.JsonParser;
|
| 23 | import com.tangykiwi.kiwiclient.KiwiClient;
|
| 24 | import com.tangykiwi.kiwiclient.gui.clickgui.CategoryWindow;
|
| 25 | import com.tangykiwi.kiwiclient.gui.clickgui.ClickGUIScreen;
|
| 26 | import com.tangykiwi.kiwiclient.gui.hudeditor.HUDEditorScreen;
|
| 27 | import com.tangykiwi.kiwiclient.gui.hudeditor.components.HUDComponent;
|
| 28 | import com.tangykiwi.kiwiclient.module.Module;
|
| 29 | import com.tangykiwi.kiwiclient.module.setting.Setting;
|
| 30 |
|
| 31 | public class ConfigManager {
|
| 32 | private static Path dir;
|
| 33 | private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
| 34 |
|
| 35 | public static void init() {
|
| 36 | dir = Paths.get(mc.gameDirectory.getPath(), "kiwiclient/");
|
| 37 | if (!dir.toFile().exists()) {
|
| 38 | dir.toFile().mkdirs();
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | public static void loadModules(String config) {
|
| 43 | JsonObject jo = readJsonFile("modules_" + config + ".json");
|
| 44 |
|
| 45 | if (jo == null) return;
|
| 46 |
|
| 47 | for (Map.Entry<String, JsonElement> e : jo.entrySet()) {
|
| 48 | Module module = KiwiClient.moduleManager.getModule(e.getKey());
|
| 49 |
|
| 50 | if (module == null) {
|
| 51 | LOGGER.error("Module " + e.getKey() + " not found, skipping");
|
| 52 | continue;
|
| 53 | }
|
| 54 |
|
| 55 | if (e.getValue().isJsonObject()) {
|
| 56 | JsonObject mo = e.getValue().getAsJsonObject();
|
| 57 | if (mo.has("toggled")) {
|
| 58 | try {
|
| 59 | boolean enabled = mo.get("toggled").getAsBoolean();
|
| 60 | module.setEnabled(enabled);
|
| 61 | } catch (Exception ex) {
|
| 62 | LOGGER.error("Error enabling module " + e.getKey() + ", disabling!");
|
| 63 | module.setEnabled(false);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | if (mo.has("settings") && mo.get("settings").isJsonObject()) {
|
| 68 | Map<String, Setting<?>> settingMap = getSettingMap(module.getSettings());
|
| 69 |
|
| 70 | for (Map.Entry<String, JsonElement> se : mo.get("settings").getAsJsonObject().entrySet()) {
|
| 71 | try {
|
| 72 | Setting<?> s = settingMap.get(se.getKey());
|
| 73 | if (s != null) s.read(se.getValue());
|
| 74 | else LOGGER.error("Error loading setting \"" + se.getKey() + "\" for module " + e.getKey());
|
| 75 | } catch (Exception ex) {
|
| 76 | LOGGER.error("Error loading setting \"" + se.getKey() + "\" for module " + e.getKey() + ": " + se.getValue());
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 | }
|
| 83 |
|
| 84 | public static void saveModules(String config) {
|
| 85 | JsonObject jo = new JsonObject();
|
| 86 |
|
| 87 | for (Module module : KiwiClient.moduleManager.moduleList) {
|
| 88 | JsonObject mo = new JsonObject();
|
| 89 | mo.addProperty("toggled", module.isEnabled());
|
| 90 |
|
| 91 | JsonObject so = new JsonObject();
|
| 92 | Map<String, Setting<?>> settingMap = getSettingMap(module.getSettings());
|
| 93 | for (Map.Entry<String, Setting<?>> s : settingMap.entrySet()) {
|
| 94 | so.add(s.getKey(), s.getValue().write());
|
| 95 | }
|
| 96 |
|
| 97 | if (so.size() != 0) mo.add("settings", so);
|
| 98 | if (mo.size() != 0) jo.add(module.getName(), mo);
|
| 99 | }
|
| 100 |
|
| 101 | setJsonFile("modules_" + config + ".json", jo);
|
| 102 | }
|
| 103 |
|
| 104 | private static Map<String, Setting<?>> getSettingMap(Collection<Setting<?>> settings) {
|
| 105 | Map<String, Setting<?>> settingMap = new HashMap<>();
|
| 106 | for (Setting<?> s : settings) {
|
| 107 | String name = s.getName();
|
| 108 | int i = 1;
|
| 109 | while (settingMap.containsKey(name))
|
| 110 | name = s.getName() + "$" + i++;
|
| 111 |
|
| 112 | settingMap.put(name, s);
|
| 113 | }
|
| 114 |
|
| 115 | return settingMap;
|
| 116 | }
|
| 117 |
|
| 118 | public static void loadClickGUI(String config) {
|
| 119 | JsonObject jo = readJsonFile("clickgui_" + config + ".json");
|
| 120 |
|
| 121 | if (jo == null) return;
|
| 122 |
|
| 123 | for (Map.Entry<String, JsonElement> e : jo.entrySet()) {
|
| 124 | if (!e.getValue().isJsonObject()) continue;
|
| 125 |
|
| 126 | CategoryWindow w = ClickGUIScreen.INSTANCE.getWindow(e.getKey());
|
| 127 | JsonObject jw = e.getValue().getAsJsonObject();
|
| 128 |
|
| 129 | try {
|
| 130 | w.x = jw.get("x").getAsInt();
|
| 131 | w.y = jw.get("y").getAsInt();
|
| 132 | w.expanded = jw.get("expanded").getAsBoolean();
|
| 133 | } catch (Exception ex) {
|
| 134 | LOGGER.error("Error loading ClickGUI window \"" + e.getKey() + "\": " + e.getValue());
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | public static void saveClickGUI(String config) {
|
| 140 | JsonObject jo = new JsonObject();
|
| 141 |
|
| 142 | for (CategoryWindow w : ClickGUIScreen.INSTANCE.windows) {
|
| 143 | JsonObject jw = new JsonObject();
|
| 144 | jw.addProperty("x", w.x);
|
| 145 | jw.addProperty("y", w.y);
|
| 146 | jw.addProperty("expanded", w.expanded);
|
| 147 | jo.add(w.title, jw);
|
| 148 | }
|
| 149 |
|
| 150 | setJsonFile("clickgui_" + config + ".json", jo);
|
| 151 | }
|
| 152 |
|
| 153 | public static void loadHUD(String config) {
|
| 154 | JsonObject jo = readJsonFile("hud_" + config + ".json");
|
| 155 |
|
| 156 | if (jo == null) return;
|
| 157 |
|
| 158 | for (Map.Entry<String, JsonElement> e : jo.entrySet()) {
|
| 159 | if (!e.getValue().isJsonObject()) continue;
|
| 160 |
|
| 161 | HUDComponent hudComponent = HUDEditorScreen.INSTANCE.getComponent(e.getKey());
|
| 162 | JsonObject jw = e.getValue().getAsJsonObject();
|
| 163 |
|
| 164 | try {
|
| 165 | hudComponent.setX(jw.get("x").getAsFloat());
|
| 166 | hudComponent.setY(jw.get("y").getAsFloat());
|
| 167 | } catch (Exception ex) {
|
| 168 | LOGGER.error("Error loading HUD component \"" + e.getKey() + "\": " + e.getValue());
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | public static void saveHUD(String config) {
|
| 174 | JsonObject jo = new JsonObject();
|
| 175 |
|
| 176 | for (HUDComponent component : HUDEditorScreen.INSTANCE.components) {
|
| 177 | JsonObject jw = new JsonObject();
|
| 178 | jw.addProperty("x", component.getX());
|
| 179 | jw.addProperty("y", component.getY());
|
| 180 | jo.add(component.getName(), jw);
|
| 181 | }
|
| 182 |
|
| 183 | setJsonFile("hud_" + config + ".json", jo);
|
| 184 | }
|
| 185 |
|
| 186 | /**
|
| 187 | * FILE IO UTILITIES
|
| 188 | */
|
| 189 |
|
| 190 | public static Path getDir() {
|
| 191 | return dir;
|
| 192 | }
|
| 193 |
|
| 194 | public static boolean fileExists(String path) {
|
| 195 | try {
|
| 196 | return getDir().resolve(path).toFile().exists();
|
| 197 | } catch (Exception e) {
|
| 198 | return false;
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | public static String readFile(String path) {
|
| 203 | try {
|
| 204 | return Files.readString(getDir().resolve(path));
|
| 205 | } catch (NoSuchFileException ignored) {
|
| 206 |
|
| 207 | } catch (Exception e) {
|
| 208 | LOGGER.error("Error Reading File: " + path + "\n" + e);
|
| 209 | }
|
| 210 |
|
| 211 | return "";
|
| 212 | }
|
| 213 |
|
| 214 | public static List<String> readFileLines(String path) {
|
| 215 | try {
|
| 216 | return Files.readAllLines(getDir().resolve(path));
|
| 217 | } catch (NoSuchFileException ignored) {
|
| 218 |
|
| 219 | } catch (Exception e) {
|
| 220 | LOGGER.error("Error Reading File: " + path + "\n" + e);
|
| 221 | }
|
| 222 |
|
| 223 | return new ArrayList<>();
|
| 224 | }
|
| 225 |
|
| 226 | public static void createFile(String path) {
|
| 227 | try {
|
| 228 | if (!fileExists(path)) {
|
| 229 | getDir().resolve(path).getParent().toFile().mkdirs();
|
| 230 | Files.createFile(getDir().resolve(path));
|
| 231 | }
|
| 232 | } catch (Exception e) {
|
| 233 | LOGGER.error("Error Creating File: " + path + "\n" + e);
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | public static void createEmptyFile(String path) {
|
| 238 | try {
|
| 239 | createFile(path);
|
| 240 |
|
| 241 | FileWriter writer = new FileWriter(getDir().resolve(path).toFile());
|
| 242 | writer.write("");
|
| 243 | writer.close();
|
| 244 | } catch (Exception e) {
|
| 245 | System.out.println("Error Clearing/Creating File: " + path + "\n" + e);
|
| 246 | }
|
| 247 | }
|
| 248 |
|
| 249 | public static void appendFile(String path, String content) {
|
| 250 | try {
|
| 251 | String fileContent = new String(Files.readAllBytes(getDir().resolve(path)));
|
| 252 | FileWriter writer = new FileWriter(getDir().resolve(path).toFile(), true);
|
| 253 | writer.write(
|
| 254 | (fileContent.endsWith("\n") || !fileContent.contains("\n") ? "" : "\n")
|
| 255 | + content
|
| 256 | + (content.endsWith("\n") ? "" : "\n"));
|
| 257 | writer.close();
|
| 258 | } catch (Exception e) {
|
| 259 | System.out.println("Error Appending File: " + path + "\n" + e);
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | public static void deleteFile(String path) {
|
| 264 | try {
|
| 265 | Files.deleteIfExists(getDir().resolve(path));
|
| 266 | } catch (Exception e) {
|
| 267 | System.out.println("Error Deleting File: " + path + "\n" + e);
|
| 268 | }
|
| 269 | }
|
| 270 |
|
| 271 | public static void setJsonFile(String path, JsonObject element) {
|
| 272 | createEmptyFile(path);
|
| 273 | appendFile(path, GSON.toJson(element));
|
| 274 | }
|
| 275 |
|
| 276 | public static JsonObject readJsonFile(String path) {
|
| 277 | String content = readFile(path);
|
| 278 |
|
| 279 | if (content.isEmpty())
|
| 280 | return null;
|
| 281 |
|
| 282 | try {
|
| 283 | return JsonParser.parseString(content).getAsJsonObject();
|
| 284 | } catch (JsonParseException | IllegalStateException e) {
|
| 285 | System.out.println("Error trying to read json file \"" + path + "\", Deleting file!");
|
| 286 |
|
| 287 | deleteFile(path);
|
| 288 | return null;
|
| 289 | }
|
| 290 | }
|
| 291 | }
|
| 292 |
|