From d90fe15a9c92ef5892be349dee06027e3a4aa2ef Mon Sep 17 00:00:00 2001
From: bd_ <bd_@nadena.dev>
Date: Sat, 19 Oct 2024 17:03:52 -0700
Subject: [PATCH] fix: issues when handling VRCExpressionMenus with an
 uninitialized type field

---
 Editor/Inspector/Menu/MenuItemGUI.cs | 7 +++----
 Editor/Menu/MenuExtractor.cs         | 4 +++-
 2 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/Editor/Inspector/Menu/MenuItemGUI.cs b/Editor/Inspector/Menu/MenuItemGUI.cs
index 30439a32..8f4ab313 100644
--- a/Editor/Inspector/Menu/MenuItemGUI.cs
+++ b/Editor/Inspector/Menu/MenuItemGUI.cs
@@ -301,10 +301,9 @@ namespace nadena.dev.modular_avatar.core.editor
                 EditorGUILayout.BeginVertical();
 
                 if (_type.hasMultipleDifferentValues) return;
-                VRCExpressionsMenu.Control.ControlType type =
-                    (VRCExpressionsMenu.Control.ControlType) Enum
-                        .GetValues(typeof(VRCExpressionsMenu.Control.ControlType))
-                        .GetValue(_type.enumValueIndex);
+                var controlTypeArray = Enum.GetValues(typeof(VRCExpressionsMenu.Control.ControlType));
+                var index = Math.Clamp(_type.enumValueIndex, 0, controlTypeArray.Length - 1);
+                var type = (VRCExpressionsMenu.Control.ControlType)controlTypeArray.GetValue(index);
 
                 switch (type)
                 {
diff --git a/Editor/Menu/MenuExtractor.cs b/Editor/Menu/MenuExtractor.cs
index 89ee232c..9f670382 100644
--- a/Editor/Menu/MenuExtractor.cs
+++ b/Editor/Menu/MenuExtractor.cs
@@ -119,9 +119,11 @@ namespace nadena.dev.modular_avatar.core.editor
 
         internal static VRCExpressionsMenu.Control CloneControl(VRCExpressionsMenu.Control c)
         {
+            var type = c.type != 0 ? c.type : VRCExpressionsMenu.Control.ControlType.Button;
+            
             return new VRCExpressionsMenu.Control()
             {
-                type = c.type,
+                type = type,
                 name = c.name,
                 icon = c.icon,
                 parameter = new VRCExpressionsMenu.Control.Parameter() { name = c.parameter?.name },