From abc3cb7ab4fabf3a91d805b754404e2a77c41a02 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 14 Jul 2019 16:44:24 +0200 Subject: [PATCH] Add settings & fix enabled hacks not loading --- src/main/java/net/wurstclient/Feature.java | 24 +++ .../java/net/wurstclient/WurstClient.java | 15 +- src/main/java/net/wurstclient/hack/Hack.java | 21 --- .../java/net/wurstclient/hack/HackList.java | 8 +- .../net/wurstclient/settings/Setting.java | 61 ++++++++ .../wurstclient/settings/SettingsFile.java | 148 ++++++++++++++++++ .../net/wurstclient/util/json/WsonObject.java | 16 ++ 7 files changed, 268 insertions(+), 25 deletions(-) create mode 100644 src/main/java/net/wurstclient/settings/Setting.java create mode 100644 src/main/java/net/wurstclient/settings/SettingsFile.java diff --git a/src/main/java/net/wurstclient/Feature.java b/src/main/java/net/wurstclient/Feature.java index 6a7a7503..21f6a2b1 100644 --- a/src/main/java/net/wurstclient/Feature.java +++ b/src/main/java/net/wurstclient/Feature.java @@ -7,14 +7,22 @@ */ package net.wurstclient; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; + import net.minecraft.client.MinecraftClient; import net.wurstclient.hack.HackCategory; +import net.wurstclient.settings.Setting; public abstract class Feature { protected static final WurstClient WURST = WurstClient.INSTANCE; protected static final MinecraftClient MC = WurstClient.MC; + private final LinkedHashMap settings = + new LinkedHashMap<>(); + public abstract String getName(); public abstract String getDescription(); @@ -33,4 +41,20 @@ public abstract class Feature { return false; } + + public final Map getSettings() + { + return Collections.unmodifiableMap(settings); + } + + protected final void addSetting(Setting setting) + { + String key = setting.getName().toLowerCase(); + + if(settings.containsKey(key)) + throw new IllegalArgumentException( + "Duplicate setting: " + getName() + " " + key); + + settings.put(key, setting); + } } diff --git a/src/main/java/net/wurstclient/WurstClient.java b/src/main/java/net/wurstclient/WurstClient.java index bcbe50a1..6d9f3935 100644 --- a/src/main/java/net/wurstclient/WurstClient.java +++ b/src/main/java/net/wurstclient/WurstClient.java @@ -22,6 +22,7 @@ import net.wurstclient.events.KeyPressListener; import net.wurstclient.hack.HackList; import net.wurstclient.keybinds.KeybindList; import net.wurstclient.keybinds.KeybindProcessor; +import net.wurstclient.settings.SettingsFile; public enum WurstClient { @@ -31,6 +32,7 @@ public enum WurstClient private WurstAnalytics analytics; private EventManager eventManager; + private SettingsFile settingsFile; private HackList hax; private CmdList cmds; private ClickGui gui; @@ -52,11 +54,15 @@ public enum WurstClient eventManager = new EventManager(this); Path enabledHacksFile = wurstFolder.resolve("enabled-hacks.json"); - Path settingsFile = wurstFolder.resolve("settings.json"); - hax = new HackList(enabledHacksFile, settingsFile); + hax = new HackList(enabledHacksFile); + hax.loadEnabledHacks(); cmds = new CmdList(); + Path settingsFile = wurstFolder.resolve("settings.json"); + this.settingsFile = new SettingsFile(settingsFile, hax, cmds); + this.settingsFile.load(); + Path keybindsFile = wurstFolder.resolve("keybinds.json"); KeybindList keybinds = new KeybindList(keybindsFile); @@ -102,6 +108,11 @@ public enum WurstClient return eventManager; } + public void saveSettings() + { + settingsFile.save(); + } + public HackList getHax() { return hax; diff --git a/src/main/java/net/wurstclient/hack/Hack.java b/src/main/java/net/wurstclient/hack/Hack.java index cc39f2e9..7edfe6f3 100644 --- a/src/main/java/net/wurstclient/hack/Hack.java +++ b/src/main/java/net/wurstclient/hack/Hack.java @@ -17,10 +17,6 @@ public abstract class Hack extends Feature private final String description; private HackCategory category; - // TODO - // private final LinkedHashMap settings = - // new LinkedHashMap<>(); - private boolean enabled; private final boolean stateSaved = !getClass().isAnnotationPresent(DontSaveState.class); @@ -59,23 +55,6 @@ public abstract class Hack extends Feature this.category = category; } - // TODO - // public final Map getSettings() - // { - // return Collections.unmodifiableMap(settings); - // } - // - // protected final void addSetting(Setting setting) - // { - // String key = setting.getName().toLowerCase(); - // - // if(settings.containsKey(key)) - // throw new IllegalArgumentException( - // "Duplicate setting: " + name + " " + key); - // - // settings.put(key, setting); - // } - @Override public final boolean isEnabled() { diff --git a/src/main/java/net/wurstclient/hack/HackList.java b/src/main/java/net/wurstclient/hack/HackList.java index 3a9ff1cf..6b116ccd 100644 --- a/src/main/java/net/wurstclient/hack/HackList.java +++ b/src/main/java/net/wurstclient/hack/HackList.java @@ -156,10 +156,9 @@ public final class HackList new TreeMap<>((o1, o2) -> o1.compareToIgnoreCase(o2)); private final EnabledHacksFile enabledHacksFile; - public HackList(Path enabledHacksFile, Path settingsFile) + public HackList(Path enabledHacksFile) { this.enabledHacksFile = new EnabledHacksFile(enabledHacksFile); - this.enabledHacksFile.load(this); try { @@ -180,6 +179,11 @@ public final class HackList } } + public void loadEnabledHacks() + { + enabledHacksFile.load(this); + } + public void saveEnabledHax() { enabledHacksFile.save(this); diff --git a/src/main/java/net/wurstclient/settings/Setting.java b/src/main/java/net/wurstclient/settings/Setting.java new file mode 100644 index 00000000..87cdf3d0 --- /dev/null +++ b/src/main/java/net/wurstclient/settings/Setting.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved. + * + * This source code is subject to the terms of the GNU General Public + * License, version 3. If a copy of the GPL was not distributed with this + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt + */ +package net.wurstclient.settings; + +import java.util.Objects; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import net.wurstclient.clickgui.Component; + +public abstract class Setting +{ + private final String name; + private final String description; + + public Setting(String name, String description) + { + this.name = Objects.requireNonNull(name); + this.description = Objects.requireNonNull(description); + } + + public final String getName() + { + return name; + } + + public final String getDescription() + { + return description; + } + + public abstract Component getComponent(); + + public abstract void fromJson(JsonElement json); + + public abstract JsonElement toJson(); + + public void legacyFromJson(JsonObject json) + { + + } + + public void update() + { + + } + + // public void addToFeatureScreen(NavigatorFeatureScreen featureScreen) + // { + // + // } + // + // public abstract ArrayList getPossibleKeybinds( + // String featureName); +} diff --git a/src/main/java/net/wurstclient/settings/SettingsFile.java b/src/main/java/net/wurstclient/settings/SettingsFile.java new file mode 100644 index 00000000..f32403d0 --- /dev/null +++ b/src/main/java/net/wurstclient/settings/SettingsFile.java @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved. + * + * This source code is subject to the terms of the GNU General Public + * License, version 3. If a copy of the GPL was not distributed with this + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt + */ +package net.wurstclient.settings; + +import java.io.IOException; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.util.Collection; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Map.Entry; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; + +import net.wurstclient.Feature; +import net.wurstclient.command.CmdList; +import net.wurstclient.command.Command; +import net.wurstclient.hack.Hack; +import net.wurstclient.hack.HackList; +import net.wurstclient.util.json.JsonException; +import net.wurstclient.util.json.JsonUtils; +import net.wurstclient.util.json.WsonObject; + +public final class SettingsFile +{ + private final Path path; + private final Map featuresWithSettings; + private boolean disableSaving; + + public SettingsFile(Path path, HackList hax, CmdList cmds) + { + this.path = path; + featuresWithSettings = createFeatureMap(hax, cmds); + } + + private Map createFeatureMap(HackList hax, CmdList cmds) + { + LinkedHashMap map = new LinkedHashMap<>(); + + for(Hack hack : hax.getAllHax()) + if(!hack.getSettings().isEmpty()) + map.put(hack.getName(), hack); + + for(Command cmd : cmds.getAllCmds()) + if(!cmd.getSettings().isEmpty()) + map.put("." + cmd.getName(), cmd); + + return Collections.unmodifiableMap(map); + } + + public void load() + { + try + { + WsonObject wson = JsonUtils.parseWsonObject(path); + loadSettings(wson); + + }catch(NoSuchFileException e) + { + // The file doesn't exist yet. No problem, we'll create it later. + + }catch(IOException | JsonException e) + { + System.out.println("Couldn't load " + path.getFileName()); + e.printStackTrace(); + } + + save(); + } + + private void loadSettings(WsonObject wson) + { + try + { + disableSaving = true; + + for(Entry e : wson.getAllJsonObjects() + .entrySet()) + { + Feature feature = featuresWithSettings.get(e.getKey()); + if(feature == null) + continue; + + loadSettings(feature, e.getValue()); + } + + }finally + { + disableSaving = false; + } + } + + private void loadSettings(Feature feature, JsonObject json) + { + Map settings = feature.getSettings(); + + for(Entry e : json.entrySet()) + { + String key = e.getKey().toLowerCase(); + if(!settings.containsKey(key)) + continue; + + settings.get(key).fromJson(e.getValue()); + } + } + + public void save() + { + if(disableSaving) + return; + + JsonObject json = createJson(); + + try + { + JsonUtils.toJson(json, path); + + }catch(IOException | JsonException e) + { + System.out.println("Couldn't save " + path.getFileName()); + e.printStackTrace(); + } + } + + private JsonObject createJson() + { + JsonObject json = new JsonObject(); + + for(Feature feature : featuresWithSettings.values()) + { + Collection settings = feature.getSettings().values(); + + JsonObject jsonSettings = new JsonObject(); + settings.forEach(s -> jsonSettings.add(s.getName(), s.toJson())); + + json.add(feature.getName(), jsonSettings); + } + + return json; + } +} diff --git a/src/main/java/net/wurstclient/util/json/WsonObject.java b/src/main/java/net/wurstclient/util/json/WsonObject.java index 347a2921..49017398 100644 --- a/src/main/java/net/wurstclient/util/json/WsonObject.java +++ b/src/main/java/net/wurstclient/util/json/WsonObject.java @@ -54,6 +54,22 @@ public final class WsonObject return map; } + public LinkedHashMap getAllJsonObjects() + { + LinkedHashMap map = new LinkedHashMap<>(); + + for(Entry entry : json.entrySet()) + { + JsonElement value = entry.getValue(); + if(!value.isJsonObject()) + continue; + + map.put(entry.getKey(), value.getAsJsonObject()); + } + + return map; + } + public JsonObject toJsonObject() { return json;