csharp/aelariane/Anarchy/Anarchy/Assembly/AoTTG/Hero/ClothFactory.cs

ClothFactory.cs
using System.Collections.Generic;
using UnityEngine;

public static clast ClothFactory
{
    public static void ClearClothCache()
    {
        ClothFactory.clothCache.Clear();
    }

    public static void DisposeObject(GameObject cachedObject)
    {
        bool flag = cachedObject != null;
        if (flag)
        {
            ParentFollow component = cachedObject.GetComponent();
            bool flag2 = component != null;
            if (flag2)
            {
                bool isActiveInScene = component.isActiveInScene;
                if (isActiveInScene)
                {
                    component.isActiveInScene = false;
                    cachedObject.transform.position = new Vector3(0f, -99999f, 0f);
                    cachedObject.GetComponent().RemoveParent();
                }
            }
            else
            {
                UnityEngine.Object.Destroy(cachedObject);
            }
        }
    }

    private static GameObject GenerateCloth(GameObject go, string res)
    {
        bool flag = go.GetComponent() == null;
        if (flag)
        {
            go.AddComponent();
        }
        Transform[] bones = go.GetComponent().bones;
        SkinnedMeshRenderer component = ((GameObject)UnityEngine.Object.Instantiate(Resources.Load(res))).GetComponent();
        component.transform.localScale = Vector3.one;
        component.bones = bones;
        component.quality = SkinQuality.Bone4;
        return component.gameObject;
    }

    public static GameObject GetCape(GameObject reference, string name, Material material)
    {
        List list;
        bool flag = ClothFactory.clothCache.TryGetValue(name, out list);
        GameObject result;
        if (flag)
        {
            for (int i = 0; i < list.Count; i++)
            {
                GameObject gameObject = list[i];
                bool flag2 = gameObject == null;
                if (flag2)
                {
                    list.RemoveAt(i);
                    i = Mathf.Max(i - 1, 0);
                }
                else
                {
                    ParentFollow component = gameObject.GetComponent();
                    bool flag3 = !component.isActiveInScene;
                    if (flag3)
                    {
                        component.isActiveInScene = true;
                        gameObject.renderer.material = material;
                        gameObject.GetComponent().enabled = true;
                        gameObject.GetComponent().enabled = true;
                        gameObject.GetComponent().SetParent(reference.transform);
                        ClothFactory.ReapplyClothBones(reference, gameObject);
                        return gameObject;
                    }
                }
            }
            GameObject gameObject2 = ClothFactory.GenerateCloth(reference, name);
            gameObject2.renderer.material = material;
            gameObject2.AddComponent().SetParent(reference.transform);
            list.Add(gameObject2);
            ClothFactory.clothCache[name] = list;
            result = gameObject2;
        }
        else
        {
            GameObject gameObject2 = ClothFactory.GenerateCloth(reference, name);
            gameObject2.renderer.material = material;
            gameObject2.AddComponent().SetParent(reference.transform);
            list = new List
            {
                gameObject2
            };
            ClothFactory.clothCache.Add(name, list);
            result = gameObject2;
        }
        return result;
    }

    public static string GetDebugInfo()
    {
        int num = 0;
        foreach (KeyValuePair keyValuePair in ClothFactory.clothCache)
        {
            num += ClothFactory.clothCache[keyValuePair.Key].Count;
        }
        int num2 = 0;
        foreach (Cloth cloth in UnityEngine.Object.FindObjectsOfType())
        {
            bool enabled = cloth.enabled;
            if (enabled)
            {
                num2++;
            }
        }
        return string.Format("{0} cached cloths, {1} active cloths, {2} types cached", num, num2, ClothFactory.clothCache.Keys.Count);
    }

    public static GameObject GetHair(GameObject reference, string name, Material material, Color color)
    {
        List list;
        bool flag = ClothFactory.clothCache.TryGetValue(name, out list);
        GameObject result;
        if (flag)
        {
            for (int i = 0; i < list.Count; i++)
            {
                GameObject gameObject = list[i];
                bool flag2 = gameObject == null;
                if (flag2)
                {
                    Debug.Log("Hair is null");
                    list.RemoveAt(i);
                    i = Mathf.Max(i - 1, 0);
                }
                else
                {
                    ParentFollow component = gameObject.GetComponent();
                    bool flag3 = !component.isActiveInScene;
                    if (flag3)
                    {
                        component.isActiveInScene = true;
                        gameObject.renderer.material = material;
                        gameObject.renderer.material.color = color;
                        gameObject.GetComponent().enabled = true;
                        gameObject.GetComponent().enabled = true;
                        gameObject.GetComponent().SetParent(reference.transform);
                        ClothFactory.ReapplyClothBones(reference, gameObject);
                        return gameObject;
                    }
                }
            }
            GameObject gameObject2 = ClothFactory.GenerateCloth(reference, name);
            gameObject2.renderer.material = material;
            gameObject2.renderer.material.color = color;
            gameObject2.AddComponent().SetParent(reference.transform);
            list.Add(gameObject2);
            ClothFactory.clothCache[name] = list;
            result = gameObject2;
        }
        else
        {
            GameObject gameObject2 = ClothFactory.GenerateCloth(reference, name);
            gameObject2.renderer.material = material;
            gameObject2.renderer.material.color = color;
            gameObject2.AddComponent().SetParent(reference.transform);
            list = new List
            {
                gameObject2
            };
            ClothFactory.clothCache.Add(name, list);
            result = gameObject2;
        }
        return result;
    }

    private static void ReapplyClothBones(GameObject reference, GameObject clothObject)
    {
        SkinnedMeshRenderer component = reference.GetComponent();
        SkinnedMeshRenderer component2 = clothObject.GetComponent();
        component2.bones = component.bones;
        component2.transform.localScale = Vector3.one;
    }

    private static Dictionary clothCache = new Dictionary(CostumeHair.hairsF.Length);
}