| 1 | package thunder.hack.core.manager.client;
|
| 2 |
|
| 3 | import com.google.gson.*;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import org.jetbrains.annotations.NotNull;
|
| 6 | import thunder.hack.ThunderHack;
|
| 7 | import thunder.hack.core.Managers;
|
| 8 | import thunder.hack.features.cmd.Command;
|
| 9 | import thunder.hack.core.manager.IManager;
|
| 10 | import thunder.hack.features.modules.Module;
|
| 11 | import thunder.hack.setting.Setting;
|
| 12 | import thunder.hack.setting.impl.*;
|
| 13 |
|
| 14 | import java.io.*;
|
| 15 | import java.net.URI;
|
| 16 | import java.net.URL;
|
| 17 | import java.nio.charset.StandardCharsets;
|
| 18 | import java.text.SimpleDateFormat;
|
| 19 | import java.util.*;
|
| 20 | import java.util.stream.Collectors;
|
| 21 |
|
| 22 | import static thunder.hack.features.modules.client.ClientSettings.isRu;
|
| 23 |
|
| 24 | @SuppressWarnings("ResultOfMethodCallIgnored")
|
| 25 | public class ConfigManager implements IManager {
|
| 26 | public static final String CONFIG_FOLDER_NAME = "ThunderHackRecode";
|
| 27 | public static final File MAIN_FOLDER = new File(mc.gameDirectory, CONFIG_FOLDER_NAME);
|
| 28 | public static final File CONFIGS_FOLDER = new File(MAIN_FOLDER, "configs");
|
| 29 | public static final File TEMP_FOLDER = new File(MAIN_FOLDER, "temp");
|
| 30 | public static final File MISC_FOLDER = new File(MAIN_FOLDER, "misc");
|
| 31 | public static final File SOUNDS_FOLDER = new File(MISC_FOLDER, "sounds");
|
| 32 | public static final File IMAGES_FOLDER = new File(MISC_FOLDER, "images");
|
| 33 | public static final File TABPARSER_FOLDER = new File(MISC_FOLDER, "tabparser");
|
| 34 | public static final File STASHLOGGER_FOLDER = new File(MISC_FOLDER, "stashlogger");
|
| 35 |
|
| 36 | public File currentConfig = null;
|
| 37 |
|
| 38 | public static boolean firstLaunch = false;
|
| 39 |
|
| 40 | public ConfigManager() {
|
| 41 | firstLaunch = !MAIN_FOLDER.exists();
|
| 42 | createDirs(MAIN_FOLDER, CONFIGS_FOLDER, TEMP_FOLDER, MISC_FOLDER, SOUNDS_FOLDER, IMAGES_FOLDER, TABPARSER_FOLDER, STASHLOGGER_FOLDER);
|
| 43 | }
|
| 44 |
|
| 45 | private void createDirs(File... dirs) {
|
| 46 | for (File dir : dirs) if (!dir.exists()) dir.mkdirs();
|
| 47 | }
|
| 48 |
|
| 49 | public static @NotNull String getConfigDate(String name) {
|
| 50 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 51 | if (!file.exists())
|
| 52 | return "none";
|
| 53 |
|
| 54 | return new SimpleDateFormat("dd MMM yyyy HH:mm").format(new Date(file.lastModified()));
|
| 55 | }
|
| 56 |
|
| 57 | public void load(String name, String category) {
|
| 58 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 59 | if (!file.exists()) {
|
| 60 | Command.sendMessage(isRu() ? "Конфига " + name + " не существует!" : "Config " + name + " does not exist!");
|
| 61 | return;
|
| 62 | }
|
| 63 |
|
| 64 | if (currentConfig != null)
|
| 65 | save(currentConfig);
|
| 66 |
|
| 67 | Managers.MODULE.onUnload(category);
|
| 68 | load(file, category);
|
| 69 | Managers.MODULE.onLoad(category);
|
| 70 | }
|
| 71 |
|
| 72 | public void loadBinds(String name) {
|
| 73 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 74 | if (!file.exists()) {
|
| 75 | Command.sendMessage(isRu() ? "Конфига " + name + " не существует!" : "Config " + name + " does not exist!");
|
| 76 | return;
|
| 77 | }
|
| 78 |
|
| 79 | if (currentConfig != null)
|
| 80 | save(currentConfig);
|
| 81 |
|
| 82 | loadBinds(file);
|
| 83 | }
|
| 84 |
|
| 85 | private void loadBinds(@NotNull File config) {
|
| 86 | if (!config.exists())
|
| 87 | save(config);
|
| 88 |
|
| 89 | try (FileReader reader = new FileReader(config, StandardCharsets.UTF_8)) {
|
| 90 | JsonObject modulesObject = JsonParser.parseReader(reader).getAsJsonArray().get(0).getAsJsonObject();
|
| 91 | JsonArray modules = modulesObject.getAsJsonArray("Modules");
|
| 92 |
|
| 93 | if (modules != null)
|
| 94 | for (JsonElement element : modules)
|
| 95 | parseBinds(element.getAsJsonObject());
|
| 96 |
|
| 97 | Command.sendMessage(isRu() ? "Загружены бинды с конфига: " + config.getName() : "Loaded bind from config: " + config.getName());
|
| 98 | } catch (IOException e) {
|
| 99 | LogUtils.getLogger().warn(e.getMessage());
|
| 100 | }
|
| 101 | saveCurrentConfig();
|
| 102 | }
|
| 103 |
|
| 104 | public void load(String name) {
|
| 105 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 106 | if (!file.exists()) {
|
| 107 | Command.sendMessage(isRu() ? "Конфига " + name + " не существует!" : "Config " + name + " does not exist!");
|
| 108 |
|
| 109 | return;
|
| 110 | }
|
| 111 |
|
| 112 | if (currentConfig != null)
|
| 113 | save(currentConfig);
|
| 114 |
|
| 115 | Managers.MODULE.onUnload("none");
|
| 116 | load(file);
|
| 117 | Managers.MODULE.onLoad("none");
|
| 118 | }
|
| 119 |
|
| 120 | public void loadCloud(String name) {
|
| 121 | Command.sendMessage(isRu() ? "Загружаю.." : "Downloading..");
|
| 122 | try (BufferedInputStream in = new BufferedInputStream(URI.create("https://raw.githubusercontent.com/ulybaka1337/THRecodeImprovedUtil/main/configs/" + name + ".th").toURL().openStream());
|
| 123 | FileOutputStream fileOutputStream = new FileOutputStream(new File(CONFIGS_FOLDER, name + ".th"))) {
|
| 124 | byte[] dataBuffer = new byte[1024];
|
| 125 | int bytesRead;
|
| 126 | while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1)
|
| 127 | fileOutputStream.write(dataBuffer, 0, bytesRead);
|
| 128 | Command.sendMessage(isRu() ? "Загрузил!" : "Downloaded!");
|
| 129 | load(name);
|
| 130 | } catch (Exception e) {
|
| 131 | Command.sendMessage(isRu() ? "Произошла ошибка при загрузке! Может название неправильное?" : "There was an error downloading! Maybe the name is wrong?");
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public void loadModuleOnly(String name, Module module) {
|
| 136 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 137 | if (!file.exists()) {
|
| 138 | Command.sendMessage(isRu() ? "Конфига " + name + " не существует!" : "Config " + name + " does not exist!");
|
| 139 | return;
|
| 140 | }
|
| 141 |
|
| 142 | if (module.isEnabled()) {
|
| 143 | ThunderHack.EVENT_BUS.unsubscribe(module);
|
| 144 | module.setEnabled(false);
|
| 145 | if (!Module.fullNullCheck())
|
| 146 | module.onDisable();
|
| 147 | }
|
| 148 |
|
| 149 | loadModuleOnly(file, module);
|
| 150 |
|
| 151 | if (module.isEnabled())
|
| 152 | ThunderHack.EVENT_BUS.subscribe(module);
|
| 153 | }
|
| 154 |
|
| 155 | public void load(@NotNull File config) {
|
| 156 | load(config, "none");
|
| 157 | }
|
| 158 |
|
| 159 | private void load(@NotNull File config, String category) {
|
| 160 | if (!config.exists())
|
| 161 | save(config);
|
| 162 |
|
| 163 | try {
|
| 164 | FileReader reader = new FileReader(config, StandardCharsets.UTF_8);
|
| 165 | JsonObject modulesObject = JsonParser.parseReader(reader).getAsJsonArray().get(0).getAsJsonObject();
|
| 166 | JsonArray modules = modulesObject.getAsJsonArray("Modules");
|
| 167 |
|
| 168 | if (modules != null)
|
| 169 | for (JsonElement element : modules)
|
| 170 | parseModule(element.getAsJsonObject(), category);
|
| 171 |
|
| 172 | Command.sendMessage(isRu() ? "Загружен конфиг " + config.getName() : "Loaded " + config.getName());
|
| 173 | } catch (Exception e) {
|
| 174 | e.printStackTrace();
|
| 175 | }
|
| 176 |
|
| 177 | if (Objects.equals(category, "none"))
|
| 178 | currentConfig = config;
|
| 179 |
|
| 180 | saveCurrentConfig();
|
| 181 | }
|
| 182 |
|
| 183 | public void loadModuleOnly(File config, Module module) {
|
| 184 | try (FileReader reader = new FileReader(config)) {
|
| 185 | JsonArray array = JsonParser.parseReader(reader).getAsJsonArray();
|
| 186 | JsonObject modulesObject = array.get(0).getAsJsonObject();
|
| 187 | JsonArray modules = modulesObject.getAsJsonArray("Modules");
|
| 188 |
|
| 189 | if (modules != null) {
|
| 190 | for (JsonElement element : modules) {
|
| 191 | JsonObject moduleObject = element.getAsJsonObject();
|
| 192 | Module loadedModule = Managers.MODULE.modules.stream().filter(m -> moduleObject.getAsJsonObject(m.getName()) != null).findFirst().orElse(null);
|
| 193 | if (loadedModule != null && Objects.equals(module.getName(), loadedModule.getName()))
|
| 194 | parseModule(moduleObject, "none");
|
| 195 | }
|
| 196 | }
|
| 197 | Command.sendMessage(isRu() ? "Загружен модуль " + module.getName() + " с конфига " + config.getName() :
|
| 198 | "Loaded " + module.getName() + " from " + config.getName());
|
| 199 |
|
| 200 | } catch (IOException e) {
|
| 201 | LogUtils.getLogger().warn(e.getMessage());
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | public void save(String name) {
|
| 206 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 207 | if (file.exists()) {
|
| 208 | Command.sendMessage(isRu() ? "Перезаписываем " + name + "..." : "Overwriting " + name + "...");
|
| 209 | file.delete();
|
| 210 | } else {
|
| 211 | Command.sendMessage(isRu() ? "Конфиг " + name + " успешно сохранен!" : "Config " + name + " successfully saved!");
|
| 212 | }
|
| 213 | save(file);
|
| 214 | }
|
| 215 |
|
| 216 | public void save(@NotNull File config) {
|
| 217 | try {
|
| 218 | if (!config.exists())
|
| 219 | config.createNewFile();
|
| 220 | JsonArray array = new JsonArray();
|
| 221 |
|
| 222 | JsonObject modulesObj = new JsonObject();
|
| 223 | modulesObj.add("Modules", getModuleArray());
|
| 224 | array.add(modulesObj);
|
| 225 |
|
| 226 | FileWriter writer = new FileWriter(config, StandardCharsets.UTF_8);
|
| 227 | new GsonBuilder().setPrettyPrinting().create().toJson(array, writer);
|
| 228 | writer.close();
|
| 229 | } catch (IOException e) {
|
| 230 | LogUtils.getLogger().warn(e.getMessage());
|
| 231 | }
|
| 232 | }
|
| 233 |
|
| 234 | @SuppressWarnings("unchecked")
|
| 235 | private void parseModule(JsonObject object, String category) throws NullPointerException {
|
| 236 | Module module = Managers.MODULE.modules.stream()
|
| 237 | .filter(m -> object.getAsJsonObject(m.getName()) != null)
|
| 238 | .findFirst()
|
| 239 | .orElse(null);
|
| 240 |
|
| 241 | if (module == null)
|
| 242 | return;
|
| 243 |
|
| 244 | if (!Objects.equals(category, "none") && !module.getCategory().getName().equalsIgnoreCase(category))
|
| 245 | return;
|
| 246 |
|
| 247 | JsonObject mobject = object.getAsJsonObject(module.getName());
|
| 248 |
|
| 249 | for (Setting setting : module.getSettings()) {
|
| 250 | try {
|
| 251 | if (!mobject.has(setting.getName())) continue;
|
| 252 |
|
| 253 | if (setting.getValue() instanceof SettingGroup) {
|
| 254 | } else if (setting.getValue() instanceof Boolean) {
|
| 255 | setting.setValue(mobject.getAsJsonPrimitive(setting.getName()).getAsBoolean());
|
| 256 | } else if (setting.getValue() instanceof Float) {
|
| 257 | setting.setValue(mobject.getAsJsonPrimitive(setting.getName()).getAsFloat());
|
| 258 | } else if (setting.getValue() instanceof Integer) {
|
| 259 | setting.setValue(mobject.getAsJsonPrimitive(setting.getName()).getAsInt());
|
| 260 | } else if (setting.getValue() instanceof String) {
|
| 261 | setting.setValue(mobject.getAsJsonPrimitive(setting.getName()).getAsString().replace("%%", " ").replace("++", "/"));
|
| 262 | } else if (setting.getValue() instanceof Bind) {
|
| 263 | JsonArray array = mobject.getAsJsonArray(setting.getName());
|
| 264 | if (array.get(0).getAsString().contains("M")) {
|
| 265 | setting.setValue(new Bind(Integer.parseInt(array.get(0).getAsString().replace("M", "")), true, array.get(1).getAsBoolean()));
|
| 266 | } else {
|
| 267 | setting.setValue(new Bind(Integer.parseInt(array.get(0).getAsString()), false, array.get(1).getAsBoolean()));
|
| 268 | }
|
| 269 | } else if (setting.getValue() instanceof ColorSetting colorSetting) {
|
| 270 | JsonArray array = mobject.getAsJsonArray(setting.getName());
|
| 271 | colorSetting.setColor(array.get(0).getAsInt());
|
| 272 | colorSetting.setRainbow(array.get(1).getAsBoolean());
|
| 273 | } else if (setting.getValue() instanceof PositionSetting posSetting) {
|
| 274 | JsonArray array = mobject.getAsJsonArray(setting.getName());
|
| 275 | posSetting.setX(array.get(0).getAsFloat());
|
| 276 | posSetting.setY(array.get(1).getAsFloat());
|
| 277 | } else if (setting.getValue() instanceof BooleanSettingGroup bGroup) {
|
| 278 | bGroup.setEnabled(mobject.getAsJsonPrimitive(setting.getName()).getAsBoolean());
|
| 279 | } else if (setting.getValue() instanceof ItemSelectSetting iSetting) {
|
| 280 | JsonArray array = mobject.getAsJsonArray(setting.getName());
|
| 281 | for (int i = 0; i < array.size(); i++)
|
| 282 | if (!iSetting.getItemsById().contains(array.get(i).getAsString()))
|
| 283 | iSetting.getItemsById().add(array.get(i).getAsString());
|
| 284 | } else if (setting.getValue().getClass().isEnum()) {
|
| 285 | Enum value = new EnumConverter(((Enum) setting.getValue()).getClass()).doBackward(mobject.getAsJsonPrimitive(setting.getName()));
|
| 286 | setting.setValue((value == null) ? setting.getDefaultValue() : value);
|
| 287 | }
|
| 288 | } catch (Exception e) {
|
| 289 | LogUtils.getLogger().warn("[Thunderhack] Module: " + module.getName() + " Setting: " + setting.getName() + " Error: ");
|
| 290 | e.printStackTrace();
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | @SuppressWarnings("unchecked")
|
| 296 | private void parseBinds(JsonObject object) throws NullPointerException {
|
| 297 | Module module = Managers.MODULE.modules.stream()
|
| 298 | .filter(m -> object.getAsJsonObject(m.getName()) != null)
|
| 299 | .findFirst()
|
| 300 | .orElse(null);
|
| 301 |
|
| 302 | if (module == null)
|
| 303 | return;
|
| 304 |
|
| 305 | JsonObject mobject = object.getAsJsonObject(module.getName());
|
| 306 |
|
| 307 | for (Setting setting : module.getSettings()) {
|
| 308 | try {
|
| 309 | if (!mobject.has(setting.getName())) continue;
|
| 310 |
|
| 311 | if (setting.getValue() instanceof Bind) {
|
| 312 | JsonArray array = mobject.getAsJsonArray(setting.getName());
|
| 313 | if (array.get(0).getAsString().contains("M")) {
|
| 314 | setting.setValue(new Bind(Integer.parseInt(array.get(0).getAsString().replace("M", "")), true, array.get(1).getAsBoolean()));
|
| 315 | } else {
|
| 316 | setting.setValue(new Bind(Integer.parseInt(array.get(0).getAsString()), false, array.get(1).getAsBoolean()));
|
| 317 | }
|
| 318 | }
|
| 319 | } catch (Exception e) {
|
| 320 | LogUtils.getLogger().warn("[Thunderhack] Module: " + module.getName() + " Setting: " + setting.getName() + " Error: ");
|
| 321 | e.printStackTrace();
|
| 322 | }
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | private @NotNull JsonArray getModuleArray() {
|
| 327 | JsonArray modulesArray = new JsonArray();
|
| 328 | for (Module m : Managers.MODULE.modules) {
|
| 329 | modulesArray.add(getModuleObject(m));
|
| 330 | }
|
| 331 | return modulesArray;
|
| 332 | }
|
| 333 |
|
| 334 | public JsonObject getModuleObject(@NotNull Module m) {
|
| 335 | JsonObject attribs = new JsonObject();
|
| 336 | for (Setting setting : m.getSettings()) {
|
| 337 | if (setting.getValue() instanceof ColorSetting color) {
|
| 338 | JsonArray array = new JsonArray();
|
| 339 | array.add(new JsonPrimitive(color.getRawColor()));
|
| 340 | array.add(new JsonPrimitive(color.isRainbow()));
|
| 341 | attribs.add(setting.getName(), array);
|
| 342 | } else if (setting.getValue() instanceof PositionSetting pos) {
|
| 343 | JsonArray array = new JsonArray();
|
| 344 | array.add(new JsonPrimitive(pos.getX()));
|
| 345 | array.add(new JsonPrimitive(pos.getY()));
|
| 346 | attribs.add(setting.getName(), array);
|
| 347 | } else if (setting.getValue() instanceof BooleanSettingGroup bGroup) {
|
| 348 | attribs.add(setting.getName(), JsonParser.parseString(String.valueOf(bGroup.isEnabled())));
|
| 349 | } else if (setting.getValue() instanceof Bind b) {
|
| 350 | JsonArray array = new JsonArray();
|
| 351 | if (b.isMouse())
|
| 352 | array.add(JsonParser.parseString(b.getBind()));
|
| 353 | else
|
| 354 | array.add(new JsonPrimitive(b.getKey()));
|
| 355 | array.add(new JsonPrimitive(b.isHold()));
|
| 356 | attribs.add(setting.getName(), array);
|
| 357 | } else if (setting.getValue() instanceof String str) {
|
| 358 | try {
|
| 359 | attribs.add(setting.getName(), JsonParser.parseString(str.replace(" ", "%%").replace("/", "++")));
|
| 360 | } catch (Exception ignored) {
|
| 361 | }
|
| 362 | } else if (setting.getValue() instanceof ItemSelectSetting iSelect) {
|
| 363 | JsonArray array = new JsonArray();
|
| 364 | for (String id : iSelect.getItemsById())
|
| 365 | array.add(new JsonPrimitive(id));
|
| 366 | attribs.add(setting.getName(), array);
|
| 367 | } else if (setting.isEnumSetting()) {
|
| 368 | attribs.add(setting.getName(), new EnumConverter(((Enum) setting.getValue()).getClass()).doForward((Enum) setting.getValue()));
|
| 369 | } else {
|
| 370 | try {
|
| 371 | attribs.add(setting.getName(), JsonParser.parseString(setting.getValue().toString()));
|
| 372 | } catch (Exception ignored) {
|
| 373 | }
|
| 374 | }
|
| 375 | }
|
| 376 |
|
| 377 | JsonObject moduleObject = new JsonObject();
|
| 378 | moduleObject.add(m.getName(), attribs);
|
| 379 | return moduleObject;
|
| 380 | }
|
| 381 |
|
| 382 | public void delete(@NotNull File file) {
|
| 383 | file.delete();
|
| 384 | }
|
| 385 |
|
| 386 | public void delete(String name) {
|
| 387 | File file = new File(CONFIGS_FOLDER, name + ".th");
|
| 388 | if (!file.exists()) {
|
| 389 | return;
|
| 390 | }
|
| 391 | delete(file);
|
| 392 | }
|
| 393 |
|
| 394 | public List<String> getConfigList() {
|
| 395 | if (!MAIN_FOLDER.exists() || MAIN_FOLDER.listFiles() == null) return null;
|
| 396 |
|
| 397 | List<String> list = new ArrayList<>();
|
| 398 |
|
| 399 | if (CONFIGS_FOLDER.listFiles() != null) {
|
| 400 | for (File file : Arrays.stream(Objects.requireNonNull(CONFIGS_FOLDER.listFiles())).filter(f -> f.getName().endsWith(".th")).toList()) {
|
| 401 | list.add(file.getName().replace(".th", ""));
|
| 402 | }
|
| 403 | }
|
| 404 | return list;
|
| 405 | }
|
| 406 |
|
| 407 | public List<String> getCloudConfigs() {
|
| 408 | List<String> list = new ArrayList<>();
|
| 409 | try {
|
| 410 | URL url = URI.create("https://raw.githubusercontent.com/ulybaka1337/THRecodeImprovedUtil/main/cloudConfigs.txt").toURL();
|
| 411 | BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
|
| 412 | String inputLine;
|
| 413 | while ((inputLine = in.readLine()) != null)
|
| 414 | list.add(inputLine.trim());
|
| 415 | } catch (Exception ignored) {
|
| 416 | }
|
| 417 | return list;
|
| 418 | }
|
| 419 |
|
| 420 | public void saveCurrentConfig() {
|
| 421 | File file = new File(CONFIG_FOLDER_NAME + "/misc/currentcfg.txt");
|
| 422 | try {
|
| 423 | if (file.exists()) {
|
| 424 | FileWriter writer = new FileWriter(file);
|
| 425 | writer.write(currentConfig.getName().replace(".th", ""));
|
| 426 | writer.close();
|
| 427 | } else {
|
| 428 | file.createNewFile();
|
| 429 | FileWriter writer = new FileWriter(file);
|
| 430 | writer.write(currentConfig.getName().replace(".th", ""));
|
| 431 | writer.close();
|
| 432 | }
|
| 433 | } catch (Exception e) {
|
| 434 | LogUtils.getLogger().warn(e.getMessage());
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | public File getCurrentConfig() {
|
| 439 | File file = new File(CONFIG_FOLDER_NAME + "/misc/currentcfg.txt");
|
| 440 | String name = "config";
|
| 441 | try {
|
| 442 | if (file.exists()) {
|
| 443 | Scanner reader = new Scanner(file);
|
| 444 | while (reader.hasNextLine())
|
| 445 | name = reader.nextLine();
|
| 446 | reader.close();
|
| 447 | }
|
| 448 | } catch (Exception e) {
|
| 449 | LogUtils.getLogger().warn(e.getMessage());
|
| 450 | }
|
| 451 | currentConfig = new File(CONFIGS_FOLDER, name + ".th");
|
| 452 | return currentConfig;
|
| 453 | }
|
| 454 | }
|
| 455 |
|