static void Postfix(UnitViewHandsEquipment __instance)
 {
     try
     {
         if (!Main.enabled)
         {
             return;
         }
         if (!__instance.Owner.IsPlayerFaction)
         {
             return;
         }
         Settings.CharacterSettings characterSettings = Main.settings.GetCharacterSettings(__instance.Owner);
         if (characterSettings == null)
         {
             return;
         }
         if (characterSettings.hideWeapons)
         {
             foreach (var kv in __instance.Sets)
             {
                 if (kv.Key.PrimaryHand.Active)
                 {
                     continue;
                 }
                 kv.Value.MainHand.ShowItem(false);
                 kv.Value.OffHand.ShowItem(false);
             }
         }
     }
     catch (Exception ex)
     {
         Main.Error(ex);
     }
 }
 static void Postfix(UnitViewHandsEquipment __instance, GameObject[] ___m_ConsumableSlots)
 {
     try
     {
         if (!Main.enabled)
         {
             return;
         }
         if (!__instance.Owner.IsPlayerFaction)
         {
             return;
         }
         Settings.CharacterSettings characterSettings = Main.settings.GetCharacterSettings(__instance.Owner);
         if (characterSettings == null)
         {
             return;
         }
         if (characterSettings.hideBeltSlots)
         {
             foreach (var go in ___m_ConsumableSlots)
             {
                 go?.SetActive(false);
             }
         }
         if (characterSettings.overrideScale && !__instance.Character.PeacefulMode)
         {
             foreach (var go in ___m_ConsumableSlots)
             {
                 if (go == null)
                 {
                     continue;
                 }
                 go.transform.localScale *= ViewManager.GetRealSizeScale(__instance.Owner.View, characterSettings);
             }
         }
     }
     catch (Exception ex)
     {
         Main.Error(ex);
     }
 }
        public static float GetRealSizeScale(UnitEntityView __instance, Settings.CharacterSettings characterSettings)
        {
            var originalScale = __instance.GetSizeScale();
            int originalSize  = (int)__instance.EntityData.Descriptor.OriginalSize;

            if (__instance.DisableSizeScaling)
            {
                originalSize = (int)__instance.EntityData.Descriptor.State.Size;
            }
            float sizeScale = 1;

            if (characterSettings.overrideScaleAdditive)
            {
                sizeScale = originalScale * Mathf.Pow(1 / 0.66f, characterSettings.additiveScaleFactor);
            }
            else
            {
                sizeScale = Mathf.Pow(1 / 0.66f, characterSettings.overrideScaleFactor - originalSize);
            }
            return(sizeScale);
        }
        /*
         * The unitEntityView stores the current scale in m_Scale, and smoothly adjusts to until it is equal to
         * sizeScale from unitEntityView.GetSizeScale()
         * sizeScale is defined as
         * Math.Pow(1 / 0.66, Descriptor.State.Size - Descriptor.OriginalSize;);
         * The actual size is change defined as
         * base.transform.localScale = UnitEntityView.m_OriginalScale * sizeScale;
         */
        static void OverrideSize(UnitEntityView __instance, Settings.CharacterSettings characterSettings)
        {
            var sizeScale       = GetRealSizeScale(__instance, characterSettings);
            var m_OriginalScale = m_OriginalScaleRef(__instance);
            var m_Scale         = __instance.transform.localScale.x / m_OriginalScale.x;

            if (!sizeScale.Equals(m_Scale) && !__instance.DoNotAdjustScale)
            {
                /*float scaleDelta = sizeScale - m_Scale;
                 * float deltaTime = Game.Instance.TimeController.DeltaTime;
                 * float scaleStep = scaleDelta * deltaTime * 2f;
                 * m_Scale = (scaleDelta <= 0f) ? Math.Max(sizeScale, m_Scale + scaleStep) : Math.Min(sizeScale, m_Scale + scaleStep);*/
                m_Scale = sizeScale; //Skip animating
                __instance.transform.localScale = m_OriginalScale * m_Scale;
            }

            if (__instance.ParticlesSnapMap)
            {
                //Is this necessary?
                __instance.ParticlesSnapMap.AdditionalScale = __instance.transform.localScale.x / m_OriginalScale.x;
            }
            //Prevent fighting m_Scale to set transform scale
            m_ScaleRef(__instance) = __instance.GetSizeScale();
        }
        public static void UpdateModel(UnitEntityView view)
        {
            if (view.CharacterAvatar == null || view.EntityData == null)
            {
                return;
            }
            if (!view.EntityData.IsPlayerFaction)
            {
                return;
            }
            Settings.CharacterSettings characterSettings = Main.settings.GetCharacterSettings(view.EntityData);
            if (characterSettings == null)
            {
                return;
            }
            bool dirty = view.CharacterAvatar.IsDirty;

            if (view.EntityData.Descriptor.Doll == null && characterSettings.classOutfit.Name != "Default")
            {
                ChangeCompanionOutfit(view, characterSettings);
            }
            if (characterSettings.classOutfit.Name == "None")
            {
                NoClassOutfit(view);
            }
            if (characterSettings.hideHelmet)
            {
                HideSlot(view, view.EntityData.Body.Head, ref dirty);
            }

            /* if (characterSettings.hideItemCloak)
             * {
             *   HideSlot(view, view.EntityData.Body.Shoulders, ref dirty);
             * }*/
            if (characterSettings.hideArmor)
            {
                HideSlot(view, view.EntityData.Body.Armor, ref dirty);
            }
            if (characterSettings.hideGloves)
            {
                HideSlot(view, view.EntityData.Body.Gloves, ref dirty);
            }
            if (characterSettings.hideBracers)
            {
                HideSlot(view, view.EntityData.Body.Wrist, ref dirty);
            }
            if (characterSettings.hideBoots)
            {
                HideSlot(view, view.EntityData.Body.Feet, ref dirty);
            }
            if (characterSettings.hideHorns)
            {
                foreach (var ee in view.CharacterAvatar.EquipmentEntities.ToArray())
                {
                    if (ee.BodyParts.Exists((bodypart) => bodypart.Type == BodyPartType.Horns))
                    {
                        view.CharacterAvatar.EquipmentEntities.Remove(ee);
                        dirty = true;
                    }
                }
            }
            if (characterSettings.hideTail)
            {
                foreach (var ee in view.CharacterAvatar.EquipmentEntities.ToArray())
                {
                    if (ee.name.StartsWith("Tail"))
                    {
                        view.CharacterAvatar.EquipmentEntities.Remove(ee);
                        dirty = true;
                    }
                }
            }
            if (characterSettings.hideClassCloak)
            {
                foreach (var ee in view.CharacterAvatar.EquipmentEntities.ToArray())
                {
                    if (ee.OutfitParts.Exists((outfit) => {
                        return(outfit.Special == EquipmentEntity.OutfitPartSpecialType.Backpack ||
                               outfit.Special == EquipmentEntity.OutfitPartSpecialType.Backpack);
                    }) && !view.ExtractEquipmentEntities(view.EntityData.Body.Shoulders).Contains(ee))
                    {
                        view.CharacterAvatar.EquipmentEntities.Remove(ee);
                        dirty = true;
                    }
                }
            }
            if (characterSettings.hideCap)
            {
                foreach (var ee in view.CharacterAvatar.EquipmentEntities.ToArray())
                {
                    if (ee.BodyParts.Exists((bodypart) => bodypart.Type == BodyPartType.Helmet) &&
                        !view.ExtractEquipmentEntities(view.EntityData.Body.Head).Contains(ee))
                    {
                        view.CharacterAvatar.EquipmentEntities.Remove(ee);
                        dirty = true;
                    }
                }
            }
            if (characterSettings.overrideHelm != null && !characterSettings.hideHelmet)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Head, characterSettings.overrideHelm, ref dirty))
                {
                    characterSettings.overrideHelm = null;
                }
            }

            /*            if (characterSettings.overrideCloak
             *            != null && !characterSettings.hideItemCloak)
             *          {
             *              if (!OverrideEquipment(view, view.EntityData.Body.Shoulders, characterSettings.overrideCloak, ref dirty))
             *              {
             *                  characterSettings.overrideCloak = null;
             *              }
             *          }*/
            if (characterSettings.overrideArmor != null && !characterSettings.hideArmor)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Armor, characterSettings.overrideArmor, ref dirty))
                {
                    characterSettings.overrideArmor = null;
                }
            }
            if (characterSettings.overrideBracers != null && !characterSettings.hideBracers)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Wrist, characterSettings.overrideBracers, ref dirty))
                {
                    characterSettings.overrideBracers = null;
                }
            }
            if (characterSettings.overrideGloves != null && !characterSettings.hideGloves)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Gloves, characterSettings.overrideGloves, ref dirty))
                {
                    characterSettings.overrideGloves = null;
                }
            }
            if (characterSettings.overrideBoots != null && !characterSettings.hideBoots)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Feet, characterSettings.overrideBoots, ref dirty))
                {
                    characterSettings.overrideBoots = null;
                }
            }
            if (characterSettings.overrideGlasses != null && !characterSettings.hideGlasses)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Glasses, characterSettings.overrideGlasses, ref dirty))
                {
                    characterSettings.overrideGlasses = null;
                }
            }
            if (characterSettings.overrideShirt != null && !characterSettings.hideShirt)
            {
                if (!OverrideEquipment(view, view.EntityData.Body.Shirt, characterSettings.overrideShirt, ref dirty))
                {
                    characterSettings.overrideShirt = null;
                }
            }
            if (characterSettings.overrideTattoo != null)
            {
                foreach (var assetId in EquipmentResourcesManager.Tattoos.Keys)
                {
                    var ee = ResourcesLibrary.TryGetResource <EquipmentEntity>(assetId);
                    if (ee != null)
                    {
                        view.CharacterAvatar.RemoveEquipmentEntity(ee);
                    }
                }
                var tattoo = ResourcesLibrary.TryGetResource <EquipmentEntity>(characterSettings.overrideTattoo);
                if (tattoo != null)
                {
                    view.CharacterAvatar.AddEquipmentEntity(tattoo);
                }
            }

            /*if (view.EntityData.Descriptor.Progression?.GetEquipmentClass().Name == "Ranger")
             * {
             *  FixRangerCloak(view);
             * }*/
            view.CharacterAvatar.IsDirty = dirty;
        }
        public static void OnGUI(UnityModManager.ModEntry modEntry)
        {
            try
            {
                if (!enabled)
                {
                    return;
                }
                Asd();
                ///Main.logger.Log(classes.Count.ToString());

                /*foreach(CharInfo s in classes)
                 * {
                 *  Main.logger.Log(s.Name + s.GUID);
                 * }*/

                if (Game.Instance.Player.PartyCharacters != null)
                {
                    foreach (UnitEntityData unitEntityData in Game.Instance.Player.PartyCharacters)
                    {
                        Settings.CharacterSettings characterSettings = settings.GetCharacterSettings(unitEntityData);
                        if (characterSettings == null)
                        {
                            characterSettings = new CharacterSettings();
                            characterSettings.characterName = unitEntityData.CharacterName;
                            var chinf = new CharInfo();
                            chinf.Name = "Default";
                            chinf.GUID = unitEntityData.Descriptor.Progression.GetEquipmentClass().AssetGuid;
                            characterSettings.classOutfit = chinf;
                            settings.AddCharacterSettings(unitEntityData, characterSettings);
                        }
                        GUILayout.Space(4f);
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(string.Format("{0}", unitEntityData.CharacterName), "box", GUILayout.Width(DefaultLabelWidth));
                        characterSettings.showClassSelection = GUILayout.Toggle(characterSettings.showClassSelection, "Select Outfit", GUILayout.ExpandWidth(false));
                        if (unitEntityData.Descriptor.Doll != null)
                        {
                            characterSettings.showDollSelection = GUILayout.Toggle(characterSettings.showDollSelection, "Select Doll", GUILayout.ExpandWidth(false));
                        }
                        else
                        {
                            characterSettings.showDollSelection = GUILayout.Toggle(characterSettings.showDollSelection, "Select Doll", GUILayout.ExpandWidth(false));
                        }
                        characterSettings.showEquipmentSelection = GUILayout.Toggle(characterSettings.showEquipmentSelection, "Select Equipment", GUILayout.ExpandWidth(false));
                        characterSettings.showOverrideSelection  = GUILayout.Toggle(characterSettings.showOverrideSelection, "Select Overrides", GUILayout.ExpandWidth(false));
                        ///characterSettings.ReloadStuff = GUILayout.Toggle(characterSettings.ReloadStuff, "Reload", GUILayout.ExpandWidth(false));
#if (DEBUG)
                        characterSettings.showInfo = GUILayout.Toggle(characterSettings.showInfo, "Show Info", GUILayout.ExpandWidth(false));
#endif
                        GUILayout.EndHorizontal();

                        /* if (characterSettings.ReloadStuff == true)
                         * {
                         *   CharacterManager.UpdateModel(unitEntityData.View);
                         * }*/
                        if (characterSettings.showClassSelection)
                        {
                            ChooseClassOutfit(characterSettings, unitEntityData);
                            GUILayout.Space(5f);
                        }
                        if (unitEntityData.Descriptor.Doll != null && characterSettings.showDollSelection)
                        {
                            ChooseDoll(unitEntityData);
                            GUILayout.Space(5f);
                        }
                        if (unitEntityData.Descriptor.Doll == null && characterSettings.showDollSelection)
                        {
                            ChooseCompanionColor(characterSettings, unitEntityData);
                            GUILayout.Space(5f);
                        }
                        if (characterSettings.showEquipmentSelection)
                        {
                            ChooseEquipment(unitEntityData, characterSettings);
                            GUILayout.Space(5f);
                        }
                        if (characterSettings.showOverrideSelection)
                        {
                            ChooseEquipmentOverride(unitEntityData, characterSettings);
                            GUILayout.Space(5f);
                        }
#if (DEBUG)
                        if (characterSettings.showInfo)
                        {
                            InfoManager.ShowInfo(unitEntityData);
                            GUILayout.Space(5f);
                        }
#endif
                    }
                }
            }
            catch (Exception e)
            {
                Log(e.ToString() + " " + e.StackTrace);
            }
        }
        static void OnGUI(UnityModManager.ModEntry modEntry)
        {
            try
            {
                if (!enabled)
                {
                    return;
                }
                if (Game.Instance.Player.ControllableCharacters == null)
                {
                    return;
                }
                foreach (UnitEntityData unitEntityData in Game.Instance.Player.ControllableCharacters)
                {
                    Settings.CharacterSettings characterSettings = settings.GetCharacterSettings(unitEntityData);
                    if (characterSettings == null)
                    {
                        characterSettings = new CharacterSettings();
                        characterSettings.characterName = unitEntityData.CharacterName;
                        settings.AddCharacterSettings(unitEntityData, characterSettings);
                    }
                    if (unitEntityData.Descriptor.IsPet)
                    {
                        GUILayout.BeginHorizontal();
                        GUILayout.Label(string.Format("{0}", unitEntityData.CharacterName), "box", GUILayout.Width(DefaultLabelWidth));
                        characterSettings.showOverrideSelection = GUILayout.Toggle(characterSettings.showOverrideSelection, "Show Override Selection", GUILayout.ExpandWidth(false));
                        GUILayout.EndHorizontal();
                        if (characterSettings.showOverrideSelection)
                        {
                            ChooseEquipmentOverride(unitEntityData, characterSettings);
                        }
                        continue;
                    }
                    GUILayout.BeginHorizontal();
                    GUILayout.Label(string.Format("{0}", unitEntityData.CharacterName), "box", GUILayout.Width(DefaultLabelWidth));
                    characterSettings.showClassSelection = GUILayout.Toggle(characterSettings.showClassSelection, "Select Outfit", GUILayout.ExpandWidth(false));
                    if (unitEntityData.Descriptor.Doll != null)
                    {
                        characterSettings.showDollSelection = GUILayout.Toggle(characterSettings.showDollSelection, "Select Doll", GUILayout.ExpandWidth(false));
                    }
                    else
                    {
                        characterSettings.showDollSelection = GUILayout.Toggle(characterSettings.showDollSelection, "Select Doll", GUILayout.ExpandWidth(false));
                    }
                    characterSettings.showEquipmentSelection = GUILayout.Toggle(characterSettings.showEquipmentSelection, "Select Equipment", GUILayout.ExpandWidth(false));
                    characterSettings.showOverrideSelection  = GUILayout.Toggle(characterSettings.showOverrideSelection, "Select Overrides", GUILayout.ExpandWidth(false));
#if (DEBUG)
                    characterSettings.showInfo = GUILayout.Toggle(characterSettings.showInfo, "Show Info", GUILayout.ExpandWidth(false));
#endif
                    GUILayout.EndHorizontal();
                    if (characterSettings.showClassSelection)
                    {
                        ChooseClassOutfit(characterSettings, unitEntityData);
                    }
                    if (unitEntityData.Descriptor.Doll != null && characterSettings.showDollSelection)
                    {
                        ChooseDoll(unitEntityData);
                    }
                    if (unitEntityData.Descriptor.Doll == null && characterSettings.showDollSelection)
                    {
                        ChooseCompanionColor(characterSettings, unitEntityData);
                    }
                    if (characterSettings.showEquipmentSelection)
                    {
                        ChooseEquipment(unitEntityData, characterSettings);
                    }
                    if (characterSettings.showOverrideSelection)
                    {
                        ChooseEquipmentOverride(unitEntityData, characterSettings);
                    }
#if (DEBUG)
                    if (characterSettings.showInfo)
                    {
                        InfoManager.ShowInfo(unitEntityData);
                    }
#endif
                }
                settings.rebuildCharacters = GUILayout.Toggle(settings.rebuildCharacters, "Rebuild character model on loadscreen (Fix visual gitches)");
            }
            catch (Exception e)
            {
                DebugLog(e.ToString() + " " + e.StackTrace);
            }
        }