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 all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

- Bugfix: Update death safe exceptions on config import from clipboard. (#406)

## 1.8.2

- Dev: Dynamically obtain coin item variations. (#398)
Expand Down
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); // inefficient, but unimportant
} catch (Exception e) {
return null;
}
}
}

return null;
}

Expand Down