aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/cc
diff options
context:
space:
mode:
authorDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-06-27 14:48:10 +0200
committerDeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com>2022-06-27 14:48:10 +0200
commit61861be3663dc05883c387070c81eae9154444ef (patch)
tree7e06a816a7d6c7a4ee101a05d0c12710986dba2b /src/main/java/cc
parentd7ec593b877b6f41c6b5b431584789e9080aede7 (diff)
downloadOneConfig-61861be3663dc05883c387070c81eae9154444ef.tar.gz
OneConfig-61861be3663dc05883c387070c81eae9154444ef.tar.bz2
OneConfig-61861be3663dc05883c387070c81eae9154444ef.zip
non profile specific system
Diffstat (limited to 'src/main/java/cc')
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/Config.java57
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java7
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/annotations/NonProfileSpecific.java14
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java2
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java26
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java26
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java4
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java15
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java4
-rw-r--r--src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java1
10 files changed, 105 insertions, 51 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
index 4228277..7af4e0c 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java
@@ -1,7 +1,6 @@
package cc.polyfrost.oneconfig.config;
import cc.polyfrost.oneconfig.config.annotations.CustomOption;
-import cc.polyfrost.oneconfig.config.annotations.Exclude;
import cc.polyfrost.oneconfig.config.annotations.HUD;
import cc.polyfrost.oneconfig.config.annotations.Page;
import cc.polyfrost.oneconfig.config.core.ConfigUtils;
@@ -11,6 +10,8 @@ import cc.polyfrost.oneconfig.config.data.PageLocation;
import cc.polyfrost.oneconfig.config.elements.BasicOption;
import cc.polyfrost.oneconfig.config.elements.OptionPage;
import cc.polyfrost.oneconfig.config.elements.OptionSubcategory;
+import cc.polyfrost.oneconfig.config.gson.NonProfileSpecificExclusionStrategy;
+import cc.polyfrost.oneconfig.config.gson.ProfileExclusionStrategy;
import cc.polyfrost.oneconfig.config.profiles.Profiles;
import cc.polyfrost.oneconfig.gui.OneConfigGui;
import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton;
@@ -19,6 +20,8 @@ import cc.polyfrost.oneconfig.hud.HUDUtils;
import cc.polyfrost.oneconfig.internal.config.annotations.Option;
import cc.polyfrost.oneconfig.internal.config.core.ConfigCore;
import cc.polyfrost.oneconfig.internal.config.core.KeyBindHandler;
+import cc.polyfrost.oneconfig.utils.JsonUtils;
+import cc.polyfrost.oneconfig.utils.TickDelay;
import cc.polyfrost.oneconfig.utils.gui.GuiUtils;
import com.google.gson.*;
@@ -36,7 +39,8 @@ import java.util.function.Supplier;
public class Config {
public final transient HashMap<String, BasicOption> optionNames = new HashMap<>();
transient protected final String configFile;
- transient protected final Gson gson = new GsonBuilder().setExclusionStrategies(new ExcludeStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create();
+ transient protected final Gson gson = new GsonBuilder().setExclusionStrategies(new ProfileExclusionStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create();
+ transient protected final Gson nonProfileSpecificGson = new GsonBuilder().setExclusionStrategies(new NonProfileSpecificExclusionStrategy()).excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting().create();
transient public Mod mod;
public transient boolean hasBeenInitialized = false;
public boolean enabled = true;
@@ -48,7 +52,7 @@ public class Config {
*/
public Config(Mod modData, String configFile, boolean initialize) {
this.configFile = configFile;
- if (initialize) init(modData);
+ if (initialize) new TickDelay(() -> init(modData), 1); // wait one tick so every class in config gets initialized
}
/**
@@ -76,9 +80,16 @@ public class Config {
* Save current config to file
*/
public void save() {
+ Profiles.getProfileFile(configFile).getParentFile().mkdirs();
+ Profiles.getNonProfileSpecificDir(configFile).getParentFile().mkdirs();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Profiles.getProfileFile(configFile).toPath()), StandardCharsets.UTF_8))) {
writer.write(gson.toJson(this));
- } catch (IOException e) {
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Profiles.getNonProfileSpecificDir(configFile).toPath()), StandardCharsets.UTF_8))) {
+ writer.write(nonProfileSpecificGson.toJson(this));
+ } catch (Exception e) {
e.printStackTrace();
}
}
@@ -88,8 +99,13 @@ public class Config {
*/
public void load() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(Profiles.getProfileFile(configFile).toPath()), StandardCharsets.UTF_8))) {
- deserializePart(new JsonParser().parse(reader).getAsJsonObject(), this);
- } catch (IOException e) {
+ deserializePart(JsonUtils.PARSER.parse(reader).getAsJsonObject(), this);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(Profiles.getNonProfileSpecificDir(configFile).toPath()), StandardCharsets.UTF_8))) {
+ deserializePart(JsonUtils.PARSER.parse(reader).getAsJsonObject(), this);
+ } catch (Exception e) {
e.printStackTrace();
}
}
@@ -240,33 +256,4 @@ public class Config {
keyBind.setRunnable(runnable);
KeyBindHandler.INSTANCE.addKeyBind(keyBind);
}
-
- private static class ExcludeStrategy implements ExclusionStrategy {
-
- /**
- * @param f the field object that is under test
- * @return true if the field should be ignored; otherwise false
- */
- @Override
- public boolean shouldSkipField(FieldAttributes f) {
- Exclude annotation = f.getAnnotation(Exclude.class);
- if (annotation != null) {
- return annotation.type() != Exclude.ExcludeType.HUD;
- }
- return false;
- }
-
- /**
- * @param clazz the class object that is under test
- * @return true if the class should be ignored; otherwise false
- */
- @Override
- public boolean shouldSkipClass(Class<?> clazz) {
- Exclude annotation = clazz.getAnnotation(Exclude.class);
- if (annotation != null) {
- return annotation.type() != Exclude.ExcludeType.HUD;
- }
- return false;
- }
- }
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java
index eddb490..8736e56 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/Exclude.java
@@ -14,11 +14,4 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface Exclude {
- ExcludeType type() default ExcludeType.ALL;
-
- enum ExcludeType {
- ALL,
- CONFIG,
- HUD
- }
}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/annotations/NonProfileSpecific.java b/src/main/java/cc/polyfrost/oneconfig/config/annotations/NonProfileSpecific.java
new file mode 100644
index 0000000..96f6410
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/config/annotations/NonProfileSpecific.java
@@ -0,0 +1,14 @@
+package cc.polyfrost.oneconfig.config.annotations;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Makes a field non profile specific, useful for things like an API key
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.FIELD, ElementType.TYPE})
+public @interface NonProfileSpecific {
+}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java
index b8958de..75bd98d 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/core/ConfigUtils.java
@@ -56,8 +56,6 @@ public class ConfigUtils {
parentClass = clazz;
}
for (Field field : fields) {
- Exclude exclude = findAnnotation(field, Exclude.class);
- if (exclude != null && exclude.type() != Exclude.ExcludeType.CONFIG) continue;
Option option = findAnnotation(field, Option.class);
if (option == null) continue;
options.add(getOption(option, field, object));
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java
new file mode 100644
index 0000000..8efa0b7
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/NonProfileSpecificExclusionStrategy.java
@@ -0,0 +1,26 @@
+package cc.polyfrost.oneconfig.config.gson;
+
+import cc.polyfrost.oneconfig.config.annotations.Exclude;
+import cc.polyfrost.oneconfig.config.annotations.NonProfileSpecific;
+import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldAttributes;
+
+public class NonProfileSpecificExclusionStrategy implements ExclusionStrategy {
+ /**
+ * @param f the field object that is under test
+ * @return true if the field should be ignored; otherwise false
+ */
+ @Override
+ public boolean shouldSkipField(FieldAttributes f) {
+ return f.getAnnotation(NonProfileSpecific.class) == null || f.getAnnotation(Exclude.class) != null;
+ }
+
+ /**
+ * @param clazz the class object that is under test
+ * @return true if the class should be ignored; otherwise false
+ */
+ @Override
+ public boolean shouldSkipClass(Class<?> clazz) {
+ return clazz.getAnnotation(Exclude.class) != null;
+ }
+}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java
new file mode 100644
index 0000000..c94e8bd
--- /dev/null
+++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/ProfileExclusionStrategy.java
@@ -0,0 +1,26 @@
+package cc.polyfrost.oneconfig.config.gson;
+
+import cc.polyfrost.oneconfig.config.annotations.Exclude;
+import cc.polyfrost.oneconfig.config.annotations.NonProfileSpecific;
+import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldAttributes;
+
+public class ProfileExclusionStrategy implements ExclusionStrategy {
+ /**
+ * @param f the field object that is under test
+ * @return true if the field should be ignored; otherwise false
+ */
+ @Override
+ public boolean shouldSkipField(FieldAttributes f) {
+ return f.getAnnotation(NonProfileSpecific.class) != null || f.getAnnotation(Exclude.class) != null;
+ }
+
+ /**
+ * @param clazz the class object that is under test
+ * @return true if the class should be ignored; otherwise false
+ */
+ @Override
+ public boolean shouldSkipClass(Class<?> clazz) {
+ return clazz.getAnnotation(NonProfileSpecific.class) != null || clazz.getAnnotation(Exclude.class) != null;
+ }
+}
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java b/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java
index 4a9cd3b..f09d648 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java
@@ -3,6 +3,7 @@ package cc.polyfrost.oneconfig.config.migration;
import cc.polyfrost.oneconfig.config.compatibility.vigilance.VigilanceName;
import java.io.BufferedReader;
+import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Field;
import java.util.HashMap;
@@ -16,13 +17,16 @@ public class VigilanceMigrator implements Migrator {
private static final Pattern stringPattern = Pattern.compile("\"?(?<name>[^\\s\"]+)\"? = \"(?<value>.+)\"");
protected final String filePath;
protected HashMap<String, HashMap<String, HashMap<String, Object>>> values = null;
+ protected final boolean fileExists;
public VigilanceMigrator(String filePath) {
this.filePath = filePath;
+ this.fileExists = new File(filePath).exists();
}
@Override
public Object getValue(Field field, String name, String category, String subcategory) {
+ if (!fileExists) return null;
if (values == null) getOptions();
if (field.isAnnotationPresent(VigilanceName.class)) {
VigilanceName annotation = field.getAnnotation(VigilanceName.class);
diff --git a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java b/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java
index 5f6a946..0f5ce09 100644
--- a/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java
+++ b/src/main/java/cc/polyfrost/oneconfig/config/profiles/Profiles.java
@@ -1,8 +1,8 @@
package cc.polyfrost.oneconfig.config.profiles;
import cc.polyfrost.oneconfig.internal.OneConfig;
-import cc.polyfrost.oneconfig.internal.config.core.ConfigCore;
import cc.polyfrost.oneconfig.internal.config.OneConfigConfig;
+import cc.polyfrost.oneconfig.internal.config.core.ConfigCore;
import org.apache.commons.io.FileUtils;
import java.io.File;
@@ -11,7 +11,8 @@ import java.util.ArrayList;
import java.util.Arrays;
public class Profiles {
- private static final File profileDir = new File("OneConfig/profiles");
+ public static final File nonProfileSpecificDir = new File("OneConfig/config");
+ public static final File profileDir = new File("OneConfig/profiles");
public static ArrayList<String> profiles;
public static String getCurrentProfile() {
@@ -20,7 +21,7 @@ public class Profiles {
return null;
}
if (profiles == null) {
- String[] profilesArray = new File("OneConfig/profiles").list((file, s) -> file.isDirectory());
+ String[] profilesArray = profileDir.list((file, s) -> file.isDirectory());
if (profilesArray != null) profiles = new ArrayList<>(Arrays.asList(profilesArray));
}
if (!getProfileDir(OneConfigConfig.currentProfile).exists()) {
@@ -43,13 +44,17 @@ public class Profiles {
}
public static File getProfileDir(String profile) {
- return new File(new File("OneConfig/profiles"), profile);
+ return new File(profileDir, profile);
}
public static File getProfileFile(String file) {
return new File(getProfileDir(), file);
}
+ public static File getNonProfileSpecificDir(String file) {
+ return new File(nonProfileSpecificDir, file);
+ }
+
public static void loadProfile(String profile) {
ConfigCore.saveAll();
OneConfigConfig.currentProfile = profile;
@@ -59,7 +64,7 @@ public class Profiles {
public static void renameProfile(String name, String newName) {
try {
- File newFile = new File(new File("OneConfig/profiles"), newName);
+ File newFile = new File(profileDir, newName);
FileUtils.moveDirectory(getProfileDir(name), newFile);
if (OneConfigConfig.currentProfile.equals(name)) OneConfigConfig.currentProfile = newName;
profiles.remove(name);
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java
index 90c1062..3057c4f 100644
--- a/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java
+++ b/src/main/java/cc/polyfrost/oneconfig/internal/config/core/ConfigCore.java
@@ -12,14 +12,14 @@ import java.util.TimerTask;
import java.util.stream.Collectors;
public class ConfigCore {
- static {
+ /*static {
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
saveAll();
}
}, 30000, 30000);
- }
+ }*/
public static List<Mod> oneConfigMods = new ArrayList<>();
public static void saveAll() {
diff --git a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java
index 4acd533..1ae0af8 100644
--- a/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java
+++ b/src/main/java/cc/polyfrost/oneconfig/test/TestConfig_Test.java
@@ -12,6 +12,7 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
public class TestConfig_Test extends Config {
+ @NonProfileSpecific
@Switch(
name = "Test Switch",
size = OptionSize.DUAL