Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: import death safe exceptions #406

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main/java/dinkplugin/SettingsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.runelite.api.GameState;
import net.runelite.api.Varbits;
import net.runelite.api.events.CommandExecuted;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.VarbitChanged;
import net.runelite.client.callback.ClientThread;
import net.runelite.client.config.ConfigManager;
Expand Down Expand Up @@ -367,7 +366,7 @@ private void handleImport(Map<String, Object> map) {
return;
}

Object value = convertTypeFromJson(valueType, rawValue);
Object value = convertTypeFromJson(gson, valueType, rawValue);
if (value == null) {
log.debug("Encountered config value with incorrect type: {} = {}", key, rawValue);
return;
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/dinkplugin/util/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dinkplugin.util;

import com.google.gson.Gson;
import lombok.experimental.UtilityClass;
import net.runelite.api.Client;
import net.runelite.api.widgets.ComponentID;
Expand All @@ -10,9 +11,11 @@
import org.jetbrains.annotations.Nullable;

import java.awt.Color;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.regex.Pattern;
import java.util.stream.Stream;

Expand All @@ -29,7 +32,7 @@ public Stream<String> readDelimited(String value) {
}

@Nullable
public Object convertTypeFromJson(@NotNull Type type, @NotNull Object in) {
public Object convertTypeFromJson(@NotNull Gson gson, @NotNull Type type, @NotNull Object in) {
if (in instanceof Boolean)
return type == boolean.class || type == Boolean.class ? in : null;

Expand Down Expand Up @@ -98,6 +101,17 @@ public Object convertTypeFromJson(@NotNull Type type, @NotNull Object in) {
}
}

if (in instanceof Collection && type instanceof ParameterizedType) {
Type rawType = ((ParameterizedType) type).getRawType();
if (rawType instanceof Class && Collection.class.isAssignableFrom((Class<?>) rawType)) {
try {
return gson.fromJson(gson.toJson(in), type); // TODO: make more efficient
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimizing this is probably not worth

--- a/src/main/java/dinkplugin/util/ConfigUtil.java	(revision 7e708a656c522fe9f2dc74c83899d56cae416f8c)
+++ b/src/main/java/dinkplugin/util/ConfigUtil.java	(revision 08d37fe24e4204ed5f10ec297c4b774b6f6aeb09)
@@ -2,6 +2,7 @@
 
 import com.google.gson.Gson;
 import lombok.experimental.UtilityClass;
+import lombok.extern.slf4j.Slf4j;
 import net.runelite.api.Client;
 import net.runelite.api.widgets.ComponentID;
 import net.runelite.api.widgets.Widget;
@@ -16,9 +17,12 @@
 import java.time.Duration;
 import java.time.Instant;
 import java.util.Collection;
+import java.util.EnumSet;
+import java.util.Set;
 import java.util.regex.Pattern;
 import java.util.stream.Stream;
 
+@Slf4j
 @UtilityClass
 public class ConfigUtil {
 
@@ -102,17 +106,35 @@
         }
 
         if (in instanceof Collection && type instanceof ParameterizedType) {
-            Type rawType = ((ParameterizedType) type).getRawType();
+            ParameterizedType pType = (ParameterizedType) type;
+            Type rawType = pType.getRawType();
             if (rawType instanceof Class && Collection.class.isAssignableFrom((Class<?>) rawType)) {
+                Type[] args = pType.getActualTypeArguments();
+                if (args != null && args.length == 1) {
+                    Type eType = args[0];
+                    if (eType instanceof Class && Enum.class.isAssignableFrom((Class<?>) eType)) {
+                        //noinspection unchecked,rawtypes
+                        return convert((Class<? extends Enum>) eType, (Collection<?>) in);
+                    }
+                }
+            }
+        }
+
+        return null;
+    }
+
+    private <E extends Enum<E>> Set<E> convert(Class<E> enumClass, Iterable<?> values) {
+        Set<E> set = EnumSet.noneOf(enumClass);
+        for (Object value : values) {
+            if (value instanceof String) {
                 try {
-                    return gson.fromJson(gson.toJson(in), type); // TODO: make more efficient
-                } catch (Exception e) {
-                    return null;
+                    set.add(Enum.valueOf(enumClass, (String) value));
+                } catch (IllegalArgumentException e) {
+                    log.debug("Encountered unknown enum value for {}: {}", enumClass.getSimpleName(), value, e);
                 }
             }
         }
-
-        return null;
+        return set;
     }
 
     public boolean isSettingsOpen(@NotNull Client client) {

} catch (Exception e) {
return null;
}
}
}

return null;
}

Expand Down