Skip to content

Commit

Permalink
Fixed an IndexOutOfBoundsException when using the index system.
Browse files Browse the repository at this point in the history
  • Loading branch information
ToxicStoxm committed Dec 25, 2024
1 parent 698a59e commit 3f50205
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

import static com.toxicstoxm.LEDSuite.tools.YamlTools.checkIfKeyExists;
import static com.toxicstoxm.LEDSuite.tools.YamlTools.ensureKeyExists;
Expand Down Expand Up @@ -84,7 +86,7 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
ConfigurationSection menuContentSection = yaml.getConfigurationSection(Constants.Communication.YAML.Keys.Reply.MenuReply.CONTENT);
if (menuContentSection == null) throw new DeserializationException("Menu content section is empty!", ErrorCode.MenuContentKeyMissing);

List<PreferencesGroup> groups = new ArrayList<>();
TreeMap<Integer, PreferencesGroup> groups = new TreeMap<>();
List<PreferencesGroup> unsortedGroups = new ArrayList<>();

// Loop through the content widgets and ensure they are groups, because all top level widgets need to be groups
Expand All @@ -102,24 +104,26 @@ public AnimationMenu deserializeAnimationMenu(String menuYAML) throws Deserializ
if (index < 0) {
unsortedGroups.add(group);
} else {
groups.set(index, group);
if (groups.containsKey(index)) {
unsortedGroups.add(group);
} else {
groups.put(index, group);
}
}
}

groups.addAll(unsortedGroups);
AtomicInteger highestIndex = new AtomicInteger(groups.lastKey());
unsortedGroups.forEach(entry -> {
groups.put(highestIndex.incrementAndGet(), entry);
});

for (int i = 0; i < groups.size(); i++) {
PreferencesGroup group = groups.get(i);
if (group != null) {
groups.forEach((index, group) -> {
try {
animationMenu.animationMenuContent.append(group);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize animation menu group '" + group.getName() + "'!", e, e.getErrorCode());
throw new DeserializationException("Failed to deserialize animation menu group '" + group.getName() + "' at index '" + index + "!", e, e.getErrorCode());
}
} else {
LEDSuiteApplication.getLogger().warn("Skipping group index '" + i + "', because the group at that position is 'null'!", new LEDSuiteLogAreas.COMMUNICATION());
}
}
});

return animationMenu;
}
Expand Down Expand Up @@ -188,7 +192,7 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
if (menuGroupContentSection == null)
throw new DeserializationException("Group content section is empty!", ErrorCode.GroupContentSectionEmptyOrMissing);

List<DeserializableWidget> widgets = new ArrayList<>();
TreeMap<Integer, DeserializableWidget> widgets = new TreeMap<>();
List<DeserializableWidget> unsortedWidgets = new ArrayList<>();

for (String widgetKey : menuGroupContentSection.getKeys(false)) {
Expand Down Expand Up @@ -217,28 +221,30 @@ private PreferencesGroup deserializeAnimationMenuGroup(@NotNull String animation
if (index < 0) {
unsortedWidgets.add(deserializableWidget);
} else {
widgets.set(index, deserializableWidget);
if (widgets.containsKey(index)) {
unsortedWidgets.add(deserializableWidget);
} else {
widgets.put(index, deserializableWidget);
}
}
}

widgets.addAll(unsortedWidgets);
AtomicInteger highestIndex = new AtomicInteger(widgets.lastKey());
unsortedWidgets.forEach(entry -> {
widgets.put(highestIndex.incrementAndGet(), entry);
});

for (int i = 0; i < widgets.size(); i++) {
DeserializableWidget widget = widgets.get(i);
if (widget != null) {
widgets.forEach((index, deserializableWidget) -> {
try {
menuGroup.add(
get(widget.widgetType()).deserialize(
widget
get(deserializableWidget.widgetType()).deserialize(
deserializableWidget
)
);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize widget '" + widget.widgetKey() + "' with type '" + widget.widgetType() + "'!", e, e.getErrorCode());
throw new DeserializationException("Failed to deserialize widget '" + deserializableWidget.widgetKey() + "' with type '" + deserializableWidget.widgetType() + "' at index '" + index + "'!", e, e.getErrorCode());
}
} else {
LEDSuiteApplication.getLogger().warn("Skipping widget index '" + i + "', because the widget at that position is 'null'!", new LEDSuiteLogAreas.COMMUNICATION());
}
}
});

return menuGroup;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
import com.toxicstoxm.LEDSuite.communication.packet_management.packets.replys.menu_reply.animation_menu.DeserializableWidget;
import com.toxicstoxm.LEDSuite.communication.packet_management.packets.replys.menu_reply.animation_menu.WidgetType;
import com.toxicstoxm.LEDSuite.communication.packet_management.packets.replys.menu_reply.animation_menu.widgets.templates.AnimationMenuRowWidget;
import com.toxicstoxm.LEDSuite.tools.YamlTools;
import com.toxicstoxm.LEDSuite.ui.LEDSuiteApplication;
import com.toxicstoxm.YAJSI.api.yaml.ConfigurationSection;
import org.gnome.adw.ExpanderRow;
import org.gnome.glib.Type;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
* @since 1.0.0
*/
Expand Down Expand Up @@ -54,6 +60,9 @@ public ExpanderRow deserialize(@NotNull DeserializableWidget deserializableWidge
ConfigurationSection contentSection = widgetSection.getConfigurationSection(Constants.Communication.YAML.Keys.Reply.MenuReply.CONTENT);
if (contentSection == null) throw new DeserializationException("Expander row without content is disallowed!", ErrorCode.ExpanderRowWithoutContent);

TreeMap<Integer, DeserializableWidget> widgets = new TreeMap<>();
List<DeserializableWidget> unsortedWidgets = new ArrayList<>();

for (String widgetKey : contentSection.getKeys(false)) {
ConfigurationSection widgetSection = contentSection.getConfigurationSection(widgetKey);
if (widgetSection == null)
Expand All @@ -62,21 +71,45 @@ public ExpanderRow deserialize(@NotNull DeserializableWidget deserializableWidge
ensureKeyExists(Constants.Communication.YAML.Keys.Reply.MenuReply.TYPE);
String type = widgetSection.getString(Constants.Communication.YAML.Keys.Reply.MenuReply.TYPE);

AnimationMenuManager animationMenuManager = getAnimationMenuManager(widgetKey, type);
try {
widget.addRow(
animationMenuManager.get(type).deserialize(
DeserializableWidget.builder()
.widgetSection(widgetSection)
.widgetKey(widgetKey)
.animationName(animationName)
.build()
)
);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize expander content!", e, e.getErrorCode());
int index = YamlTools.getIntIfAvailable(Constants.Communication.YAML.Keys.Reply.MenuReply.INDEX, -1, widgetSection);

DeserializableWidget expanderRowChild =
DeserializableWidget.builder()
.widgetType(type)
.widgetSection(widgetSection)
.widgetKey(widgetKey)
.animationName(animationName)
.build();

if (index < 0) {
unsortedWidgets.add(expanderRowChild);
} else {
if (widgets.containsKey(index)) {
unsortedWidgets.add(expanderRowChild);
} else {
widgets.put(index, expanderRowChild);
}
}
}

AtomicInteger highestIndex = new AtomicInteger(widgets.lastKey());
unsortedWidgets.forEach(entry -> {
widgets.put(highestIndex.incrementAndGet(), entry);
});

widgets.forEach((index, expanderRowChild) -> {
try {
AnimationMenuManager animationMenuManager = getAnimationMenuManager(expanderRowChild.widgetKey(), expanderRowChild.widgetType());
widget.addRow(
animationMenuManager.get(expanderRowChild.widgetType()).deserialize(
expanderRowChild
)
);
} catch (DeserializationException e) {
throw new DeserializationException("Failed to deserialize expander child widget '" + deserializableWidget.widgetKey() + "' with type '" + deserializableWidget.widgetType() + "' at index '" + index + "'!", e, e.getErrorCode());
}
});

if (widget.getShowEnableSwitch()) {
onChanged(() -> sendMenuChangeRequest(widget.getEnableExpansion()));
}
Expand Down

0 comments on commit 3f50205

Please sign in to comment.