From ff12b37431114f44e1a9094203780e1f9447fed1 Mon Sep 17 00:00:00 2001 From: RndName Date: Wed, 23 Feb 2022 00:06:34 +0100 Subject: [PATCH] Load TgoLayoutGroups in correct order Add handling to insert the layout group at the correct index according to the order used in the layout yaml --- game/layout/layout.py | 7 +++++++ game/layout/layoutloader.py | 6 +++--- game/layout/layoutmapping.py | 6 +++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/game/layout/layout.py b/game/layout/layout.py index 206dd720..f81a3136 100644 --- a/game/layout/layout.py +++ b/game/layout/layout.py @@ -168,6 +168,13 @@ class TgoLayout: # campaign initialization. For each generic layout a new Group will be created. self.generic: bool = False + def add_layout_group( + self, name: str, group: TgoLayoutGroup, index: int = 0 + ) -> None: + """Adds the layout group to the group dict at the given index""" + # If the index is greater than the actual len it will add it after the last item + self.groups[name].insert(min(len(self.groups[name]), index), group) + def usable_by_faction(self, faction: Faction) -> bool: # Special handling for Buildings if ( diff --git a/game/layout/layoutloader.py b/game/layout/layoutloader.py index 502ebb86..c6548695 100644 --- a/game/layout/layoutloader.py +++ b/game/layout/layoutloader.py @@ -127,7 +127,7 @@ class LayoutLoader: ): try: - group_name, group_mapping = mapping.group_for_name( + g_id, group_name, group_mapping = mapping.group_for_name( dcs_group.name ) except KeyError: @@ -166,8 +166,8 @@ class LayoutLoader: group_mapping.fallback_classes, ) group_layout.optional = group_mapping.optional - # Add the group at the correct position - layout.groups[group_name].append(group_layout) + # Add the group at the correct index + layout.add_layout_group(group_name, group_layout, g_id) layout_unit = LayoutUnit.from_unit(unit) if i == 0 and layout.name not in template_position: template_position[layout.name] = unit.position diff --git a/game/layout/layoutmapping.py b/game/layout/layoutmapping.py index b82a895f..a4dea739 100644 --- a/game/layout/layoutmapping.py +++ b/game/layout/layoutmapping.py @@ -118,9 +118,9 @@ class LayoutMapping: layout_file, ) - def group_for_name(self, name: str) -> tuple[str, GroupLayoutMapping]: + def group_for_name(self, name: str) -> tuple[int, str, GroupLayoutMapping]: for group_name, group_mappings in self.groups.items(): - for group_mapping in group_mappings: + for g_id, group_mapping in enumerate(group_mappings): if group_mapping.name == name or name in group_mapping.statics: - return group_name, group_mapping + return g_id, group_name, group_mapping raise KeyError