Пример #1
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);
        }
Пример #2
0
        private static bool TryCreateBill(StreamReader sr, out Bill_Production bill)
        {
            bill = null;
            string[] kv = null;
            try
            {
                while (!sr.EndOfStream)
                {
                    if (ReadField(sr, out kv))
                    {
                        RecipeDef def;
                        switch (kv[0])
                        {
                        case BREAK:
                            return(true);

                        case "recipeDefName":
                            def = DefDatabase <RecipeDef> .GetNamed(kv[1]);

                            if (def == null)
                            {
                                string msg = "SaveStorageSettings.UnableToLoadRecipeDef".Translate().Replace("%s", kv[1]);
                                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);
                            }
                            bill = new Bill_Production(def);
                            break;

                        case "recipeDefNameUft":
                            def = DefDatabase <RecipeDef> .GetNamed(kv[1]);

                            if (def == null)
                            {
                                string msg = "SaveStorageSettings.UnableToLoadRecipeDef".Translate().Replace("%s", kv[1]);
                                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);
                            }
                            bill = new Bill_ProductionWithUft(def);
                            break;

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

                        case "suspended":
                            bill.suspended = bool.Parse(kv[1]);
                            break;

                        case "ingSearchRadius":
                            bill.ingredientSearchRadius = float.Parse(kv[1]);
                            break;

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

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

                        case "repeatCount":
                            bill.repeatCount = int.Parse(kv[1]);
                            break;

                        case "storeMode":
                            string[] storeSplit = kv[1].Split('/');

                            BillStoreModeDef storeMode = DefDatabase <BillStoreModeDef> .GetNamedSilentFail(storeSplit[0]);

                            if (storeMode == null)
                            {
                                Log.Message("Bill [" + bill.recipe.defName + "] storeMode [" + kv[1] + "] cannot be found. Defaulting to [" + BillStoreModeDefOf.BestStockpile.ToString() + "].");
                                storeMode = BillStoreModeDefOf.BestStockpile;
                            }

                            Zone_Stockpile storeZone = null;
                            if (storeMode == BillStoreModeDefOf.SpecificStockpile)
                            {
                                if (storeSplit.Length > 1)
                                {
                                    storeZone = (Zone_Stockpile)Find.CurrentMap?.zoneManager.AllZones.FirstOrFallback(z => z.GetUniqueLoadID() == storeSplit[1]);
                                }

                                if (storeZone == null)
                                {
                                    Log.Message("Bill [" + bill.recipe.defName + "] storeZone [" + kv[1] + "] cannot be found. Defaulting to storeMode [" + BillStoreModeDefOf.BestStockpile.ToString() + "].");
                                    storeMode = BillStoreModeDefOf.BestStockpile;
                                }
                            }

                            bill.SetStoreMode(storeMode, storeZone);
                            break;

                        case "targetCount":
                            bill.targetCount = int.Parse(kv[1]);
                            break;

                        case "includeEquipped":
                            bill.includeEquipped = bool.Parse(kv[1]);
                            break;

                        case "includeTainted":
                            bill.includeTainted = bool.Parse(kv[1]);
                            break;

                        case "includeFromZone":
                            Zone_Stockpile zone = (Zone_Stockpile)Find.CurrentMap?.zoneManager.AllZones.FirstOrFallback(z => z.GetUniqueLoadID() == kv[1]);

                            if (zone == null)
                            {
                                Log.Message("Bill [" + bill.recipe.defName + "] includeFromZone [" + kv[1] + "] cannot be found. Defaulting to Everywhere (null).");
                            }

                            bill.includeFromZone = zone;
                            break;

                        case "limitToAllowedStuff":
                            bill.limitToAllowedStuff = bool.Parse(kv[1]);
                            break;

                        case "pauseWhenSatisfied":
                            bill.pauseWhenSatisfied = bool.Parse(kv[1]);
                            break;

                        case "unpauseWhenYouHave":
                            bill.unpauseWhenYouHave = int.Parse(kv[1]);
                            break;

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

                        case "qualityRange":
                            bill.qualityRange = QualityRange.FromString(kv[1]);
                            break;

                        case "ingredientFilter":
                            ReadFiltersFromFile(bill.ingredientFilter, sr);
                            return(true);
                        }
                    }
                }
            }
            catch
            {
                string error = "";
                if (bill != null && bill.recipe != null)
                {
                    error = "Unable to load bill [" + bill.recipe.defName + "].";
                }
                else
                {
                    error = "Unable to load a bill.";
                }

                if (kv == null || kv.Length < 2)
                {
                    error += " Current line: [" + kv[0] + ":" + kv[1] + "]";
                }
                Log.Warning(error);
                bill = null;
                return(false);
            }
            return(true);
        }