// Token: 0x06000038 RID: 56 RVA: 0x0000227B File Offset: 0x0000047B
        private static bool Prefix(EntityCreationController __instance, BlueprintUnit unit, UnitEntityView prefab, Vector3 position, Quaternion rotation, SceneEntitiesState state, ref UnitEntityData __result)
        {
            /*  if (!Main.first)
             * {
             *    return true;
             * }*/

            if (unit == null)
            {
                Main.DebugLog("Trying to spawn null unit.");
                // Main.OrigTexName = null;
                //   Main.ReadableText = null;


                Main.preset    = null;
                Main.notRandom = false;
                return(true);
            }
            if (prefab == null)
            {
                Main.DebugLog("Trying to spawn unit without prefab");
                // Main.OrigTexName = null;
                // Main.ReadableText = null;


                Main.preset    = null;
                Main.notRandom = false;
                return(true);
            }


            Main.DebugLog("SpawnUnit() -  " + unit.CharacterName + " - " + unit.name + " - " + unit.AssetGuid);


            prefab.UniqueId = Guid.NewGuid().ToString();

            bool isprefab = false;

            //if (prefab.Renderers !=null && prefab.Renderers.Count() > 0)
            if (prefab.GetComponentsInChildren <Component>().Count() > 0)
            {
                //prefab.Renderers.ForEach(x => Main.DebugLog("tex? "+x.material?.mainTexture?.name));
                isprefab = true;

                //prefab.GetComponentsInChildren<SkinnedMeshRenderer>().ToList().ForEach(x => Main.DebugLog("tex? " + x.material?.mainTexture?.name));

                Tuple <string, string> result2 = new Tuple <string, string>("", "");
                //if (!string.IsNullOrEmpty(__instance.Descriptor.CustomPrefabGuid))
                // if (unit.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(unit.name))
                // {

                result2 = Main.randomPool(unit, prefab);
                //   }

                prefab = Main.unitEntityViewTexReplacer(prefab, result2.Item1, result2.Item2);
            }
            else
            {
                Main.DebugLog("noren");
            }

            //var named = (first: "one", second: "two");



            UnitEntityView unitEntityView = UnityEngine.Object.Instantiate <UnitEntityView>(prefab, position, rotation);

            if (!isprefab)
            {
                Tuple <string, string> result = new Tuple <string, string>("", "");
                //if (!string.IsNullOrEmpty(__instance.Descriptor.CustomPrefabGuid))
                //   if (unit.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(unit.name))
                //   {

                result = Main.randomPool(unit, unitEntityView);
                //    }

                unitEntityView = Main.unitEntityViewTexReplacer(unitEntityView, result.Item1, result.Item2);
            }


            //  unitEntityView.Destroy();



#if DEBUG
            Main.DebugLog("SpawnUnit(): Main.OrigTexName: " + Main.OrigTexName);
            if (unit.AddFacts.Length > 0)
            {
                Array.Clear(unit.AddFacts, 0, unit.AddFacts.Length);
            }
#endif



            unitEntityView.Blueprint = unit;

            __result = (UnitEntityData)__instance.SpawnEntityWithView(unitEntityView, state);

            /*
             * if (__result.View.CharacterAvatar != null)
             * {
             *
             *  if (__result.View.CharacterAvatar != null && __result.View.CharacterAvatar.BakedCharacter != null && __result.View.CharacterAvatar.BakedCharacter.RendererDescriptions.Count() > 0 && __result.View.CharacterAvatar.BakedCharacter.RendererDescriptions.Any(x => (x.Material?.mainTexture?.width < 500 || x.Material?.mainTexture?.height < 500) && x.Material?.mainTexture?.name == Main.OrigTexName))
             *  {
             *      Main.DebugLog("SpawnUnit/ after SpawnEntityView CHARAVATAR TRIGGERED");
             *      foreach (RendererDescription rd in __result.View.CharacterAvatar.BakedCharacter.RendererDescriptions)
             *      {
             *          if (rd.Material?.mainTexture?.name == Main.OrigTexName)
             *          {
             *              rd.Material.mainTexture = Main.ReadableText;
             *              rd.Material.mainTexture.name = Main.OrigTexName;
             *          }
             *
             *      }
             *
             *  }
             *
             * }
             * Main.OrigTexName = null;
             */



            // Main.ReadableText = null;


            Main.preset    = null;
            Main.notRandom = false;

            return(false);
        }
        // Token: 0x06000003 RID: 3 RVA: 0x000027B4 File Offset: 0x000009B4
        private static bool Prefix(UnitEntityData __instance, ref UnitEntityView __result)
        {
            if (!Main.modEnabled)
            {
                return(true);
            }

            if (Main.customPrefabUnits == null || Main.customPrefabUnits.Count() == 0)
            {
                Main.Init();
            }


            Main.DebugLog("NEW CreateView() -  " + __instance.Blueprint.CharacterName + " - " + __instance.Blueprint.name + " - " + __instance.Blueprint.AssetGuid);
            Polymorph activePolymorph = __instance.GetActivePolymorph();

            if (activePolymorph != null)
            {
                UnitEntityView unitEntityView = activePolymorph.Prefab.Load(false);
                if (unitEntityView)
                {
                    Quaternion rotation = (!unitEntityView.ForbidRotation) ? Quaternion.Euler(0f, __instance.Orientation, 0f) : Quaternion.identity;



                    bool isprefab2 = false;
                    if (unitEntityView.GetComponentsInChildren <Component>().Count() > 0)
                    {
                        isprefab2 = true;

                        Tuple <string, string> result2 = new Tuple <string, string>("", "");
                        // if (__instance.Blueprint.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(__instance.Blueprint.name))
                        // {

                        result2 = Main.randomPool(__instance.Blueprint, unitEntityView);
                        //   }

                        unitEntityView = Main.unitEntityViewTexReplacer(unitEntityView, result2.Item1, result2.Item2);
                    }


                    UnitEntityView unitEntityView2 = UnityEngine.Object.Instantiate <UnitEntityView>(unitEntityView, __instance.Position, rotation);



                    if (!isprefab2)
                    {
                        Tuple <string, string> result = new Tuple <string, string>("", "");


                        //  if (__instance.Blueprint.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(__instance.Blueprint.name))
                        // {

                        result = Main.randomPool(__instance.Blueprint, unitEntityView2);
                        // }

                        unitEntityView2 = Main.unitEntityViewTexReplacer(unitEntityView2, result.Item1, result.Item2);
                    }

                    /*
                     *
                     * string texfullpath = "";
                     * if (!string.IsNullOrEmpty(__instance.Descriptor.CustomPrefabGuid)&& __instance.Blueprint != null)
                     * {
                     *  //texfullpath = Main.randomPool(__instance.Blueprint, __instance.Descriptor.CustomPrefabGuid);
                     *
                     *  unitEntityView2
                     * }
                     *
                     * unitEntityView2 = Main.unitEntityViewTexReplacer(unitEntityView2, texfullpath);
                     */
                    activePolymorph.SetReplacementViewOnLoad(unitEntityView2);
                    unitEntityView2.DisableSizeScaling = true;
                    //                  Main.DebugLog("1 " + unitEntityView2.name);
                    __result = unitEntityView2;

                    // Main.OrigTexName = null;
                    // Main.ReadableText = null;


                    Main.preset    = null;
                    Main.notRandom = false;
                    return(false);
                }
            }
            if (__instance.Descriptor.Doll != null)
            {
                UnitEntityView unitEntityView3 = __instance.Descriptor.Doll.CreateUnitView(false);
                unitEntityView3.transform.position = __instance.Position;
                unitEntityView3.transform.rotation = Quaternion.Euler(0f, __instance.Orientation, 0f);

                /*
                 * string texfullpath = "";
                 * if (!string.IsNullOrEmpty(__instance.Descriptor.CustomPrefabGuid) && __instance.Blueprint != null)
                 * {
                 *  texfullpath = Main.randomPool(__instance.Blueprint, __instance.Descriptor.CustomPrefabGuid);
                 * }
                 *
                 * unitEntityView3 = Main.unitEntityViewTexReplacer(unitEntityView3, texfullpath);
                 * //                Main.DebugLog("2 " + unitEntityView3.name);
                 */


                Tuple <string, string> result = new Tuple <string, string>("", "");
                //if (!string.IsNullOrEmpty(__instance.Descriptor.CustomPrefabGuid))
                // if (__instance.Blueprint.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(__instance.Blueprint.name))
                // {

                result = Main.randomPool(__instance.Blueprint, unitEntityView3);
                // }

                unitEntityView3 = Main.unitEntityViewTexReplacer(unitEntityView3, result.Item1, result.Item2);



                __result = unitEntityView3;
                // Main.OrigTexName = null;
                // Main.ReadableText = null;


                Main.preset    = null;
                Main.notRandom = false;
                return(false);
            }
            UnitEntityView unitEntityView4 = (!string.IsNullOrEmpty(__instance.Descriptor.CustomPrefabGuid)) ? ResourcesLibrary.TryGetResource <UnitEntityView>(__instance.Descriptor.CustomPrefabGuid, false) : __instance.Blueprint.Prefab.Load(false);


            if (unitEntityView4 == null)
            {
                UberDebug.LogError(__instance.Blueprint, "Cannot find prefab for unit", Array.Empty <object>());

                __result = null;
                // Main.OrigTexName = null;
                //  Main.ReadableText = null;


                Main.preset    = null;
                Main.notRandom = false;
                return(false);
            }



            bool isprefab = false;

            if (unitEntityView4.GetComponentsInChildren <Component>().Count() > 0)
            {
                isprefab = true;

                Tuple <string, string> result2 = new Tuple <string, string>("", "");
                // if (__instance.Blueprint.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(__instance.Blueprint.name))
                // {

                result2 = Main.randomPool(__instance.Blueprint, unitEntityView4);
                // }

                unitEntityView4 = Main.unitEntityViewTexReplacer(unitEntityView4, result2.Item1, result2.Item2);
            }



            Quaternion rotation2 = (!unitEntityView4.ForbidRotation) ? Quaternion.Euler(0f, __instance.Orientation, 0f) : Quaternion.identity;


            UnitEntityView resultView = UnityEngine.Object.Instantiate <UnitEntityView>(unitEntityView4, __instance.Position, rotation2);


            if (!isprefab)
            {
                Tuple <string, string> result = new Tuple <string, string>("", "");


                // if (__instance.Blueprint.CustomizationPreset != null || Main.customPrefabUnits.ContainsKey(__instance.Blueprint.name))
                // {

                result = Main.randomPool(__instance.Blueprint, resultView);
                // }

                resultView = Main.unitEntityViewTexReplacer(resultView, result.Item1, result.Item2);
            }


            __result = resultView;
            //   Main.OrigTexName = null;
            // Main.ReadableText = null;


            Main.preset    = null;
            Main.notRandom = false;

            return(false);
        }