Example #1
0
        public static bool LoadFilters(ThingFilter filter, FileInfo fi)
        {
            ThingFilterReflection tfr = new ThingFilterReflection(filter);
            bool changed = false;

            try
            {
                if (fi.Exists)
                {
                    // Load Data
                    using (StreamReader sr = new StreamReader(fi.FullName))
                    {
                        string[] kv = null;
                        if (sr.EndOfStream ||
                            !ReadField(sr, out kv))
                        {
                            throw new Exception("Trying to read from an empty file");
                        }

                        if (kv != null && "1".Equals(kv[1]))
                        {
                            while (!sr.EndOfStream)
                            {
                                if (ReadField(sr, out kv))
                                {
                                    switch (kv[0])
                                    {
                                    case "allowedDefs":
                                        try
                                        {
                                            HashSet <ThingDef> allowedDefs = tfr.AllowedDefs;
                                            allowedDefs.Clear();
                                            if (kv[1] != null && kv[1].Length > 0)
                                            {
                                                HashSet <string> expected = new HashSet <string>(kv[1].Split('/'));
                                                foreach (ThingDef thing in tfr.AllStorableThingDefs)
                                                {
                                                    if (expected.Contains(thing.defName))
                                                    {
                                                        allowedDefs.Add(thing);
                                                    }
                                                }
                                            }
                                            changed = true;
                                        }
                                        catch (Exception e)
                                        {
                                            LogException("Problem while loading Allowed Filters.", e);
                                        }
                                        break;

                                    case "allowedHitPointsPercents":
                                        if (kv[1] != null && kv[1].IndexOf(':') != -1)
                                        {
                                            kv = kv[1].Split(':');
                                            try
                                            {
                                                filter.AllowedHitPointsPercents = new FloatRange(
                                                    float.Parse(kv[0]), float.Parse(kv[1]));
                                            }
                                            catch (Exception e)
                                            {
                                                LogException("Problem while loading Hit Point Percents.", e);
                                            }
                                        }
                                        changed = true;
                                        break;

                                    case "allowedQualities":
                                        if (kv[1] != null && kv[1].IndexOf(':') != -1)
                                        {
                                            kv = kv[1].Split(':');
                                            try
                                            {
                                                filter.AllowedQualityLevels = new QualityRange(
                                                    (QualityCategory)Enum.Parse(typeof(QualityCategory), kv[0], true),
                                                    (QualityCategory)Enum.Parse(typeof(QualityCategory), kv[1], true));
                                                changed = true;
                                            }
                                            catch (Exception e)
                                            {
                                                LogException("Problem while loading Allowed Qualities.", e);
                                            }
                                        }
                                        break;

                                    case "disallowedSpecialFilters":
                                        if (kv[1] != null)
                                        {
                                            try
                                            {
                                                if (kv[1].Length > 0)
                                                {
                                                    HashSet <string>             expected = new HashSet <string>(kv[1].Split('/'));
                                                    List <SpecialThingFilterDef> l        = new List <SpecialThingFilterDef>();
                                                    foreach (SpecialThingFilterDef def in DefDatabase <SpecialThingFilterDef> .AllDefs)
                                                    {
                                                        if (def != null && def.configurable && expected.Contains(def.defName))
                                                        {
                                                            l.Add(def);
                                                        }
                                                    }

                                                    tfr.DisallowedSpecialFilters = l;
                                                }
                                                else
                                                {
                                                    tfr.DisallowedSpecialFilters.Clear();
                                                }
                                                changed = true;
                                            }
                                            catch (Exception e)
                                            {
                                                LogException("Problem while loading Disallowed Special Filters.", e);
                                            }
                                        }
                                        break;

                                        /*case "specialFiltersToAllow":
                                         *  try
                                         *  {
                                         *      if (kv[1] != null && kv[1].Length > 0)
                                         *      {
                                         *          tfr.SpecialFiltersToAllow = new List<string>(kv[1].Split('/'));
                                         *      }
                                         *  }
                                         *  catch (Exception e)
                                         *  {
                                         *      LogException("Problem while loading Allowed Filters.", e);
                                         *  }
                                         *  break;
                                         * case "specialFiltersToDisallow":
                                         *  try
                                         *  {
                                         *      if (kv[1] != null && kv[1].Length > 0)
                                         *      {
                                         *          tfr.SpecialFiltersToDisallow = new List<string>(kv[1].Split('/'));
                                         *      }
                                         *  }
                                         *  catch (Exception e)
                                         *  {
                                         *      LogException("Problem while loading Allowed Filters.", e);
                                         *  }
                                         *  break;*/
                                    }
                                }
                            }
                        }
                        else
                        {
                            throw new Exception("Unsupported version: " + kv[1]);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                LogException("Problem loading storage settings file '" + fi.Name + "'.", e);
                return(false);
            }
            finally
            {
                if (changed)
                {
                    tfr.SettingsChangedCallback();
                }
            }
            return(true);
        }
Example #2
0
        private bool TryCreateBill(Dictionary <string, string> fields, out Bill_Production bill)
        {
            bill = null;
            RecipeDef             def;
            ThingFilter           filter     = null;
            ThingFilterReflection reflection = null;
            bool changed = false;

            foreach (var field in fields)
            {
                string key   = field.Key;
                string value = field.Value;

                switch (key)
                {
                case BREAK:
                    return(true);

                case "recipeDefName":
                case "recipeDefNameUft":
                    def = DefDatabase <RecipeDef> .GetNamed(value);

                    if (def == null)
                    {
                        string msg = "SaveStorageSettings.UnableToLoadRecipeDef".Translate().Replace("%s", value);
                        Messages.Message(msg, MessageTypeDefOf.SilentInput);
                        Log.Warning(msg);
                        return(false);
                    }
                    if (def.researchPrerequisite != null && !def.researchPrerequisite.IsFinished)
                    {
                        string msg = "SaveStorageSettings.ResearchNotDoneForRecipeDef".Translate().Replace("%s", def.label);
                        Messages.Message(msg, MessageTypeDefOf.SilentInput);
                        Log.Warning(msg);
                        return(false);
                    }

                    if (key == "recipeDefName")
                    {
                        bill = new Bill_Production(def);
                    }
                    else
                    {
                        bill = new Bill_ProductionWithUft(def);
                    }

                    filter     = bill.ingredientFilter;
                    reflection = new ThingFilterReflection(filter);

                    break;

                case "suspended":
                    bill.suspended = bool.Parse(value);
                    break;

                case "countEquipped":
                    bill.includeEquipped = bool.Parse(value);
                    break;

                case "countTainted":
                    bill.includeTainted = bool.Parse(value);
                    break;

                case "skillRange":
                    string[] skillRange = value.Split('~');
                    bill.allowedSkillRange = new IntRange(int.Parse(skillRange[0]), int.Parse(skillRange[1]));
                    break;

                case "ingSearchRadius":
                    bill.ingredientSearchRadius = float.Parse(value);
                    break;

                case "repeatMode":
                    bill.repeatMode = null;
                    if (BillRepeatModeDefOf.Forever.defName.Equals(value))
                    {
                        bill.repeatMode = BillRepeatModeDefOf.Forever;
                    }
                    else if (BillRepeatModeDefOf.RepeatCount.defName.Equals(value))
                    {
                        bill.repeatMode = BillRepeatModeDefOf.RepeatCount;
                    }
                    else if (BillRepeatModeDefOf.TargetCount.defName.Equals(value))
                    {
                        bill.repeatMode = BillRepeatModeDefOf.TargetCount;
                    }
                    else if ("TD_ColonistCount".Equals(value))
                    {
                        EverybodyGetsOneUtil.TryGetRepeatModeDef("TD_ColonistCount", out bill.repeatMode);
                    }
                    else if ("TD_XPerColonist".Equals(value))
                    {
                        EverybodyGetsOneUtil.TryGetRepeatModeDef("TD_XPerColonist", out bill.repeatMode);
                    }
                    else if ("TD_WithSurplusIng".Equals(value))
                    {
                        EverybodyGetsOneUtil.TryGetRepeatModeDef("TD_WithSurplusIng", out bill.repeatMode);
                    }

                    if (bill.repeatMode == null)
                    {
                        Log.Warning("Unknown repeatMode of [" + value + "] for bill " + bill.recipe.defName);
                        bill = null;
                        return(false);
                    }
                    break;

                case "repeatCount":
                    bill.repeatCount = int.Parse(value);
                    break;

                case "targetCount":
                    bill.targetCount = int.Parse(value);
                    break;

                case "pauseWhenSatisfied":
                    bill.pauseWhenSatisfied = bool.Parse(value);
                    break;

                case "unpauseWhenYouHave":
                    bill.unpauseWhenYouHave = int.Parse(value);
                    break;

                case "hpRange":
                    string[] hpRange = value.Split('~');
                    bill.hpRange = new FloatRange(float.Parse(hpRange[0]), float.Parse(hpRange[1]));
                    break;

                case "qualityRange":
                    if (!string.IsNullOrEmpty(value) && value.IndexOf('~') != -1)
                    {
                        string[] qualityRange = value.Split('~');
                        bill.qualityRange = new QualityRange(
                            (QualityCategory)Enum.Parse(typeof(QualityCategory), qualityRange[0], true),
                            (QualityCategory)Enum.Parse(typeof(QualityCategory), qualityRange[1], true));
                    }
                    break;

                case "onlyAllowedIngredients":
                    bill.limitToAllowedStuff = bool.Parse(value);
                    break;

                case "storeMode":
                    BillStoreModeDef storeMode = DefDatabase <BillStoreModeDef> .GetNamedSilentFail(value);

                    if (storeMode == null || value == BillStoreModeDefOf.SpecificStockpile.ToString())
                    {
                        storeMode = BillStoreModeDefOf.BestStockpile;
                    }

                    bill.SetStoreMode(storeMode);
                    break;

                case "storeZone":
                    var destinationZone = (Zone_Stockpile)Current.Game.CurrentMap.zoneManager.AllZones.Find(z => z.label == value);

                    if (destinationZone != null)
                    {
                        bill.SetStoreMode(BillStoreModeDefOf.SpecificStockpile, destinationZone);
                    }
                    else
                    {
                        bill.SetStoreMode(BillStoreModeDefOf.BestStockpile);
                    }
                    break;

                case "lookIn":
                    var ingredientZone = (Zone_Stockpile)Current.Game.CurrentMap.zoneManager.AllZones.Find(z => z.label == value);

                    bill.includeFromZone = ingredientZone;
                    break;

                case "allowedDefs":
                    reflection.AllowedDefs.Clear();

                    if (value != null)
                    {
                        HashSet <string>       expected = new HashSet <string>(value.Split('/'));
                        IEnumerable <ThingDef> all      = reflection.AllStorableThingDefs;

                        var expectedContained = from thing in all
                                                where expected.Contains(thing.defName)
                                                select thing;

                        reflection.AllowedDefs.AddRange(expectedContained.ToList());
                    }

                    changed = true;
                    break;

                case "allowedHitPointsPercents":
                    if (!string.IsNullOrEmpty(value) && value.IndexOf(':') != -1)
                    {
                        string[] values = value.Split(':');
                        float    min    = float.Parse(values[0]);
                        float    max    = float.Parse(values[1]);
                        filter.AllowedHitPointsPercents = new FloatRange(min, max);
                        changed = true;
                    }
                    break;

                case "allowedQualities":
                    if (!string.IsNullOrEmpty(value) && value.IndexOf(':') != -1)
                    {
                        string[] values = value.Split(':');
                        filter.AllowedQualityLevels = new QualityRange(
                            (QualityCategory)Enum.Parse(typeof(QualityCategory), values[0], true),
                            (QualityCategory)Enum.Parse(typeof(QualityCategory), values[1], true));
                        changed = true;
                    }
                    break;

                case "disallowedSpecialFilters":
                    reflection.DisallowedSpecialFilters.Clear();

                    if (!string.IsNullOrEmpty(value))
                    {
                        HashSet <string>             expected = new HashSet <string>(value.Split('/'));
                        List <SpecialThingFilterDef> l        = new List <SpecialThingFilterDef>();

                        foreach (SpecialThingFilterDef specialDef in DefDatabase <SpecialThingFilterDef> .AllDefs)
                        {
                            if (specialDef != null && specialDef.configurable && expected.Contains(specialDef.defName))
                            {
                                l.Add(specialDef);
                            }
                        }

                        reflection.DisallowedSpecialFilters = l;
                    }

                    changed = true;
                    break;
                }
            }

            if (changed)
            {
                reflection.SettingsChangedCallback();
            }
            return(false);
        }
Example #3
0
        private static void ReadFiltersFromFile(ThingFilter filter, StreamReader sr)
        {
            ThingFilterReflection tfr = new ThingFilterReflection(filter);
            bool changed = false;

            try
            {
                string[] kv;
                while (!sr.EndOfStream)
                {
                    if (ReadField(sr, out kv))
                    {
                        switch (kv[0])
                        {
                        case BREAK:
                            return;

                        case "allowedDefs":
                            try
                            {
                                HashSet <ThingDef> allowedDefs = tfr.AllowedDefs;
                                allowedDefs.Clear();
                                if (kv[1] != null && kv[1].Length > 0)
                                {
                                    HashSet <string> expected = new HashSet <string>(kv[1].Split('/'));
                                    foreach (ThingDef thing in tfr.AllStorableThingDefs)
                                    {
                                        if (expected.Contains(thing.defName))
                                        {
                                            allowedDefs.Add(thing);
                                        }
                                    }
                                }
                                changed = true;
                            }
                            catch (Exception e)
                            {
                                LogException("Problem while loading Allowed Filters.", e);
                            }
                            break;

                        case "allowedHitPointsPercents":
                            if (kv[1] != null && kv[1].IndexOf(':') != -1)
                            {
                                kv = kv[1].Split(':');
                                try
                                {
                                    filter.AllowedHitPointsPercents = new FloatRange(
                                        float.Parse(kv[0]), float.Parse(kv[1]));
                                }
                                catch (Exception e)
                                {
                                    LogException("Problem while loading Hit Point Percents.", e);
                                }
                            }
                            changed = true;
                            break;

                        case "allowedQualities":
                            if (kv[1] != null && kv[1].IndexOf(':') != -1)
                            {
                                kv = kv[1].Split(':');
                                try
                                {
                                    filter.AllowedQualityLevels = new QualityRange(
                                        (QualityCategory)Enum.Parse(typeof(QualityCategory), kv[0], true),
                                        (QualityCategory)Enum.Parse(typeof(QualityCategory), kv[1], true));
                                    changed = true;
                                }
                                catch (Exception e)
                                {
                                    LogException("Problem while loading Allowed Qualities.", e);
                                }
                            }
                            break;

                        case "disallowedSpecialFilters":
                            if (kv[1] != null)
                            {
                                try
                                {
                                    if (kv[1].Length > 0)
                                    {
                                        HashSet <string>             expected = new HashSet <string>(kv[1].Split('/'));
                                        List <SpecialThingFilterDef> l        = new List <SpecialThingFilterDef>();
                                        foreach (SpecialThingFilterDef def in DefDatabase <SpecialThingFilterDef> .AllDefs)
                                        {
                                            if (def != null && def.configurable && expected.Contains(def.defName))
                                            {
                                                l.Add(def);
                                            }
                                        }

                                        tfr.DisallowedSpecialFilters = l;
                                    }
                                    else
                                    {
                                        tfr.DisallowedSpecialFilters.Clear();
                                    }
                                    changed = true;
                                }
                                catch (Exception e)
                                {
                                    LogException("Problem while loading Disallowed Special Filters.", e);
                                }
                            }
                            break;
                        }
                    }
                }
            }
            finally
            {
                if (changed)
                {
                    tfr.SettingsChangedCallback();
                }
            }
        }