Пример #1
0
 public static object FromString(string str, Type itemType)
 {
     try
     {
         itemType = (Nullable.GetUnderlyingType(itemType) ?? itemType);
         if (itemType == typeof(string))
         {
             str = str.Replace("\\n", "\n");
             return(str);
         }
         if (itemType == typeof(int))
         {
             return(int.Parse(str, CultureInfo.InvariantCulture));
         }
         if (itemType == typeof(float))
         {
             return(float.Parse(str, CultureInfo.InvariantCulture));
         }
         if (itemType == typeof(bool))
         {
             return(bool.Parse(str));
         }
         if (itemType == typeof(long))
         {
             return(long.Parse(str, CultureInfo.InvariantCulture));
         }
         if (itemType == typeof(double))
         {
             return(double.Parse(str, CultureInfo.InvariantCulture));
         }
         if (itemType == typeof(sbyte))
         {
             return(sbyte.Parse(str, CultureInfo.InvariantCulture));
         }
         if (itemType.IsEnum)
         {
             try
             {
                 return(Enum.Parse(itemType, str));
             }
             catch (ArgumentException innerException)
             {
                 string str2 = "'" + str + "' is not a valid value for " + itemType + ". Valid values are: \n";
                 str2 += GenText.StringFromEnumerable(Enum.GetValues(itemType));
                 ArgumentException ex = new ArgumentException(str2, innerException);
                 throw ex;
             }
         }
         if (itemType == typeof(Type))
         {
             if (!(str == "null") && !(str == "Null"))
             {
                 Type typeInAnyAssembly = GenTypes.GetTypeInAnyAssembly(str);
                 if (typeInAnyAssembly == null)
                 {
                     Log.Error("Could not find a type named " + str);
                 }
                 return(typeInAnyAssembly);
             }
             return(null);
         }
         if (itemType == typeof(Action))
         {
             string[] array      = str.Split('.');
             string   methodName = array[array.Length - 1];
             string   empty      = string.Empty;
             empty = ((array.Length != 3) ? array[0] : (array[0] + "." + array[1]));
             Type       typeInAnyAssembly2 = GenTypes.GetTypeInAnyAssembly(empty);
             MethodInfo method             = typeInAnyAssembly2.GetMethods().First((MethodInfo m) => m.Name == methodName);
             return((Action)Delegate.CreateDelegate(typeof(Action), method));
         }
         if (itemType == typeof(Vector3))
         {
             return(ParseHelper.FromStringVector3(str));
         }
         if (itemType == typeof(Vector2))
         {
             return(ParseHelper.FromStringVector2(str));
         }
         if (itemType == typeof(Rect))
         {
             return(ParseHelper.FromStringRect(str));
         }
         if (itemType == typeof(Color))
         {
             str = str.TrimStart('(', 'R', 'G', 'B', 'A');
             str = str.TrimEnd(')');
             string[] array2 = str.Split(',');
             float    num    = (float)ParseHelper.FromString(array2[0], typeof(float));
             float    num2   = (float)ParseHelper.FromString(array2[1], typeof(float));
             float    num3   = (float)ParseHelper.FromString(array2[2], typeof(float));
             bool     flag   = num > 1.0 || num3 > 1.0 || num2 > 1.0;
             float    num4   = (float)((!flag) ? 1 : 255);
             if (array2.Length == 4)
             {
                 num4 = (float)ParseHelper.FromString(array2[3], typeof(float));
             }
             Color color = default(Color);
             if (!flag)
             {
                 color.r = num;
                 color.g = num2;
                 color.b = num3;
                 color.a = num4;
             }
             else
             {
                 color = GenColor.FromBytes(Mathf.RoundToInt(num), Mathf.RoundToInt(num2), Mathf.RoundToInt(num3), Mathf.RoundToInt(num4));
             }
             return(color);
         }
         if (itemType == typeof(PublishedFileId_t))
         {
             return(new PublishedFileId_t(ulong.Parse(str)));
         }
         if (itemType == typeof(IntVec2))
         {
             return(IntVec2.FromString(str));
         }
         if (itemType == typeof(IntVec3))
         {
             return(IntVec3.FromString(str));
         }
         if (itemType == typeof(Rot4))
         {
             return(Rot4.FromString(str));
         }
         if (itemType == typeof(CellRect))
         {
             return(CellRect.FromString(str));
         }
         if (itemType == typeof(CurvePoint))
         {
             return(CurvePoint.FromString(str));
         }
         if (itemType == typeof(NameTriple))
         {
             NameTriple nameTriple = NameTriple.FromString(str);
             nameTriple.ResolveMissingPieces(null);
         }
         else
         {
             if (itemType == typeof(FloatRange))
             {
                 return(FloatRange.FromString(str));
             }
             if (itemType == typeof(IntRange))
             {
                 return(IntRange.FromString(str));
             }
             if (itemType == typeof(QualityRange))
             {
                 return(QualityRange.FromString(str));
             }
             if (itemType == typeof(ColorInt))
             {
                 str = str.TrimStart('(', 'R', 'G', 'B', 'A');
                 str = str.TrimEnd(')');
                 string[] array3   = str.Split(',');
                 ColorInt colorInt = new ColorInt(255, 255, 255, 255);
                 colorInt.r = (int)ParseHelper.FromString(array3[0], typeof(int));
                 colorInt.g = (int)ParseHelper.FromString(array3[1], typeof(int));
                 colorInt.b = (int)ParseHelper.FromString(array3[2], typeof(int));
                 if (array3.Length == 4)
                 {
                     colorInt.a = (int)ParseHelper.FromString(array3[3], typeof(int));
                 }
                 else
                 {
                     colorInt.a = 255;
                 }
                 return(colorInt);
             }
         }
         throw new ArgumentException("Trying to parse to unknown data type " + itemType.Name + ". Content is '" + str + "'.");
     }
     catch (Exception innerException2)
     {
         ArgumentException ex2 = new ArgumentException("Exception parsing " + itemType + " from \"" + str + "\"", innerException2);
         throw ex2;
     }
 }
        public static float CalculateTagEfficiency(HediffSet diffSet, BodyPartTagDef tag, float maximum = 3.40282347E+38f, FloatRange lerp = default(FloatRange), List <PawnCapacityUtility.CapacityImpactor> impactors = null, float bestPartEfficiencySpecialWeight = -1f)
        {
            BodyDef body = diffSet.pawn.RaceProps.body;
            float   num  = 0f;
            int     num2 = 0;
            float   num3 = 0f;
            List <PawnCapacityUtility.CapacityImpactor> list = null;

            foreach (BodyPartRecord bodyPartRecord in body.GetPartsWithTag(tag))
            {
                BodyPartRecord part = bodyPartRecord;
                List <PawnCapacityUtility.CapacityImpactor> impactors2 = list;
                float num4 = PawnCapacityUtility.CalculatePartEfficiency(diffSet, part, false, impactors2);
                if (impactors != null && num4 != 1f && list == null)
                {
                    list       = new List <PawnCapacityUtility.CapacityImpactor>();
                    part       = bodyPartRecord;
                    impactors2 = list;
                    PawnCapacityUtility.CalculatePartEfficiency(diffSet, part, false, impactors2);
                }
                num += num4;
                num3 = Mathf.Max(num3, num4);
                num2++;
            }
            if (num2 == 0)
            {
                return(1f);
            }
            float num5;

            if (bestPartEfficiencySpecialWeight >= 0f && num2 >= 2)
            {
                num5 = num3 * bestPartEfficiencySpecialWeight + (num - num3) / (float)(num2 - 1) * (1f - bestPartEfficiencySpecialWeight);
            }
            else
            {
                num5 = num / (float)num2;
            }
            float num6 = num5;

            if (lerp != default(FloatRange))
            {
                num6 = lerp.LerpThroughRange(num6);
            }
            num6 = Mathf.Min(num6, maximum);
            if (impactors != null && list != null && (maximum != 1f || num5 <= 1f || num6 == 1f))
            {
                impactors.AddRange(list);
            }
            return(num6);
        }
Пример #3
0
        public static float CalculateTagEfficiency(HediffSet diffSet, BodyPartTagDef tag, float maximum = float.MaxValue, FloatRange lerp = default(FloatRange), List <CapacityImpactor> impactors = null, float bestPartEfficiencySpecialWeight = -1f)
        {
            BodyDef body                 = diffSet.pawn.RaceProps.body;
            float   totalEfficiency      = 0f;
            int     partCount            = 0;
            float   bestPartEfficiency   = 0f;
            List <CapacityImpactor> list = null;

            foreach (BodyPartRecord item in body.GetPartsWithTag(tag))
            {
                BodyPartRecord          part       = item;
                List <CapacityImpactor> impactors2 = list;
                float partEfficiency = CalculatePartEfficiency(diffSet, part, ignoreAddedParts: false, impactors2);
                if (impactors != null && partEfficiency != 1f && list == null)
                {
                    list       = new List <CapacityImpactor>();
                    part       = item;
                    impactors2 = list;
                    CalculatePartEfficiency(diffSet, part, ignoreAddedParts: false, impactors2);
                }
                totalEfficiency   += partEfficiency;
                bestPartEfficiency = Mathf.Max(bestPartEfficiency, partEfficiency);
                partCount++;
            }
            if (partCount == 0)
            {
                return(1f);
            }
            float weightedEfficiency = (!(bestPartEfficiencySpecialWeight >= 0f) || partCount < 2) ? (totalEfficiency / (float)partCount) : (bestPartEfficiency * bestPartEfficiencySpecialWeight + (totalEfficiency - bestPartEfficiency) / (float)(partCount - 1) * (1f - bestPartEfficiencySpecialWeight));
            float num6 = weightedEfficiency;

            if (lerp != default(FloatRange))
            {
                num6 = lerp.LerpThroughRange(num6);
            }
            num6 = Mathf.Min(num6, maximum);
            if (impactors != null && list != null && (maximum != 1f || weightedEfficiency <= 1f || num6 == 1f))
            {
                impactors.AddRange(list);
            }
            return(num6);
        }
Пример #4
0
 public static bool FactionOwnsPassableRoomInTemperatureRange(Faction faction, FloatRange tempRange, Map map)
 {
     if (faction == Faction.OfPlayer)
     {
         List <Room> allRooms = map.regionGrid.allRooms;
         for (int i = 0; i < allRooms.Count; i++)
         {
             Room room = allRooms[i];
             if (room.RegionType.Passable() && !room.Fogged && tempRange.Includes(room.Temperature))
             {
                 return(true);
             }
         }
         return(false);
     }
     return(false);
 }
Пример #5
0
        protected void ValueEditWidgetRight(TreeNode_Editor node, float leftX)
        {
            if (node.nodeType != EditTreeNodeType.TerminalValue)
            {
                throw new ArgumentException();
            }
            Rect   rect       = new Rect(leftX, this.curY, base.ColumnWidth - leftX, this.lineHeight);
            object obj        = node.Value;
            Type   objectType = node.ObjectType;

            if (objectType == typeof(string))
            {
                string text  = (string)obj;
                string text2 = text;
                if (text2 == null)
                {
                    text2 = string.Empty;
                }
                string b = text2;
                text2 = Widgets.TextField(rect, text2);
                if (text2 != b)
                {
                    text = text2;
                }
                obj = text;
            }
            else if (objectType == typeof(bool))
            {
                bool flag = (bool)obj;
                Widgets.Checkbox(new Vector2(rect.x, rect.y), ref flag, this.lineHeight, false);
                obj = flag;
            }
            else if (objectType == typeof(int))
            {
                rect.width = 100f;
                string s = Widgets.TextField(rect, obj.ToString());
                int    num;
                if (int.TryParse(s, out num))
                {
                    obj = num;
                }
            }
            else if (objectType == typeof(float))
            {
                EditSliderRangeAttribute[] array = (EditSliderRangeAttribute[])node.owningField.GetCustomAttributes(typeof(EditSliderRangeAttribute), true);
                if (array.Length > 0)
                {
                    float num2  = (float)obj;
                    Rect  rect2 = new Rect(this.LabelWidth + 60f + 4f, this.curY, base.EditAreaWidth - 60f - 8f, this.lineHeight);
                    num2 = Widgets.HorizontalSlider(rect2, num2, array[0].min, array[0].max, false, null, null, null, -1f);
                    obj  = num2;
                }
                rect.width = 60f;
                string text3 = obj.ToString();
                text3 = Widgets.TextField(rect, text3);
                float num3;
                if (float.TryParse(text3, out num3))
                {
                    obj = num3;
                }
            }
            else if (objectType.IsEnum)
            {
                if (Widgets.ButtonText(rect, obj.ToString(), true, false, true))
                {
                    List <FloatMenuOption> list = new List <FloatMenuOption>();
                    foreach (object current in Enum.GetValues(objectType))
                    {
                        object localVal = current;
                        list.Add(new FloatMenuOption(current.ToString(), delegate
                        {
                            node.Value = localVal;
                        }, MenuOptionPriority.Default, null, null, 0f, null, null));
                    }
                    Find.WindowStack.Add(new FloatMenu(list));
                }
            }
            else if (objectType == typeof(FloatRange))
            {
                float sliderMin = 0f;
                float sliderMax = 100f;
                EditSliderRangeAttribute[] array2 = (EditSliderRangeAttribute[])node.owningField.GetCustomAttributes(typeof(EditSliderRangeAttribute), true);
                if (array2.Length > 0)
                {
                    sliderMin = array2[0].min;
                    sliderMax = array2[0].max;
                }
                FloatRange floatRange = (FloatRange)obj;
                Widgets.FloatRangeWithTypeIn(rect, node.owningIndex, ref floatRange, sliderMin, sliderMax, ToStringStyle.FloatTwo, null);
                obj = floatRange;
            }
            else
            {
                GUI.color = new Color(1f, 1f, 1f, 0.4f);
                Widgets.Label(rect, "uneditable value type");
                GUI.color = Color.white;
            }
            node.Value = obj;
        }
        public static float CalculateTagEfficiency(HediffSet diffSet, BodyPartTagDef tag, float maximum = 3.40282347E+38f, FloatRange lerp = default(FloatRange), List <PawnCapacityUtility.CapacityImpactor> impactors = null)
        {
            BodyDef body = diffSet.pawn.RaceProps.body;
            float   num  = 0f;
            int     num2 = 0;
            List <PawnCapacityUtility.CapacityImpactor> list = null;

            foreach (BodyPartRecord bodyPartRecord in body.GetPartsWithTag(tag))
            {
                BodyPartRecord part = bodyPartRecord;
                List <PawnCapacityUtility.CapacityImpactor> impactors2 = list;
                float num3 = PawnCapacityUtility.CalculatePartEfficiency(diffSet, part, false, impactors2);
                if (impactors != null && num3 != 1f && list == null)
                {
                    list       = new List <PawnCapacityUtility.CapacityImpactor>();
                    part       = bodyPartRecord;
                    impactors2 = list;
                    PawnCapacityUtility.CalculatePartEfficiency(diffSet, part, false, impactors2);
                }
                num += num3;
                num2++;
            }
            float result;

            if (num2 == 0)
            {
                result = 1f;
            }
            else
            {
                float num4 = num / (float)num2;
                float num5 = num4;
                if (lerp != default(FloatRange))
                {
                    num5 = lerp.LerpThroughRange(num5);
                }
                num5 = Mathf.Min(num5, maximum);
                if (impactors != null && list != null && (maximum != 1f || num4 <= 1f || num5 == 1f))
                {
                    impactors.AddRange(list);
                }
                result = num5;
            }
            return(result);
        }