public StorageContainer(StorageSettings settings) : base(new Version(1, 0)) { Settings = settings; Priority = settings.Priority; Reflection = new ThingFilterReflection(settings.filter); // Allowed things var allowed = from thing in Reflection.AllStorableThingDefs where settings.filter.Allows(thing) select thing; AllowedThings = new List <ThingDef>(allowed); // Can configure HP if (settings.filter.allowedHitPointsConfigurable) { AllowedHitPoints = settings.filter.AllowedHitPointsPercents; } // Can configure Qualities if (settings.filter.allowedQualitiesConfigurable) { AllowedQualities = settings.filter.AllowedQualityLevels; } // Disallowed Specials DisallowedSpecialThings = Reflection.DisallowedSpecialFilters; }
public FoodRestrictionContainer(FoodRestriction restriction) : base(new Version(1, 0)) { Restriction = restriction; Reflection = new ThingFilterReflection(restriction.filter); // Allowed things var allowed = from thing in Reflection.AllStorableThingDefs where restriction.filter.Allows(thing) select thing; AllowedThings = new List <ThingDef>(allowed); // Can configure HP if (restriction.filter.allowedHitPointsConfigurable) { AllowedHitPoints = restriction.filter.AllowedHitPointsPercents; } // Can configure Qualities if (restriction.filter.allowedQualitiesConfigurable) { AllowedQualities = restriction.filter.AllowedQualityLevels; } // Disallowed Specials DisallowedSpecialThings = Reflection.DisallowedSpecialFilters; }
public OutfitContainer(Outfit outfit) : base(new Version(1, 0)) { Outfit = outfit; Reflection = new ThingFilterReflection(outfit.filter); // Allowed things var allowed = from thing in Reflection.AllStorableThingDefs where outfit.filter.Allows(thing) select thing; AllowedThings = new List <ThingDef>(allowed); // Can configure HP if (outfit.filter.allowedHitPointsConfigurable) { AllowedHitPoints = outfit.filter.AllowedHitPointsPercents; } // Can configure Qualities if (outfit.filter.allowedQualitiesConfigurable) { AllowedQualities = outfit.filter.AllowedQualityLevels; } // Disallowed Specials DisallowedSpecialThings = Reflection.DisallowedSpecialFilters; }
public ZoneContainer(Zone_Stockpile stockpile) : base(new Version(1, 0)) { ThingFilter filter = stockpile.settings.filter; Stockpile = stockpile; Color = stockpile.color; Priority = stockpile.settings.Priority; Reflection = new ThingFilterReflection(filter); // Allowed things var allowed = from thing in Reflection.AllStorableThingDefs where filter.Allows(thing) select thing; AllowedThings = new List <ThingDef>(allowed); // Can configure HP if (filter.allowedHitPointsConfigurable) { AllowedHitPoints = filter.AllowedHitPointsPercents; } // Can configure Qualities if (filter.allowedQualitiesConfigurable) { AllowedQualities = filter.AllowedQualityLevels; } // Disallowed Specials DisallowedSpecialThings = Reflection.DisallowedSpecialFilters; }
private static void WriteFiltersToFile(ThingFilter filter, StreamWriter sw) { ThingFilterReflection tfr = new ThingFilterReflection(filter); StringBuilder sb = new StringBuilder(); foreach (ThingDef thing in tfr.AllStorableThingDefs) { if (filter.Allows(thing)) { if (sb.Length > 0) { sb.Append("/"); } sb.Append(thing.defName); } } WriteField(sw, "allowedDefs", sb.ToString()); sb = null; if (filter.allowedHitPointsConfigurable) { sb = new StringBuilder(filter.AllowedHitPointsPercents.min.ToString("N4")); sb.Append(":"); sb.Append(filter.AllowedHitPointsPercents.max.ToString("N4")); WriteField(sw, "allowedHitPointsPercents", sb.ToString()); sb = null; } if (filter.allowedQualitiesConfigurable) { sb = new StringBuilder(filter.AllowedQualityLevels.min.ToString()); sb.Append(":"); sb.Append(filter.AllowedQualityLevels.max.ToString()); WriteField(sw, "allowedQualities", sb.ToString()); sb = null; } sb = new StringBuilder(); foreach (SpecialThingFilterDef def in tfr.DisallowedSpecialFilters) { if (sb.Length > 0) { sb.Append("/"); } sb.Append(def.defName); } WriteField(sw, "disallowedSpecialFilters", sb.ToString()); sb = null; }
protected override void SaveFields(StreamWriter writer) { foreach (Bill bill in Bills) { if (bill is Bill_Production productionBill) { ThingFilter filter = productionBill.ingredientFilter; ThingFilterReflection reflection = new ThingFilterReflection(filter); string defName = "recipeDefName"; if (bill is Bill_ProductionWithUft) { defName = "recipeDefNameUft"; } WriteField(writer, defName, productionBill.recipe.defName); WriteField(writer, "suspended", productionBill.suspended.ToString()); WriteField(writer, "countEquipped", productionBill.includeEquipped.ToString()); WriteField(writer, "countTainted", productionBill.includeTainted.ToString()); WriteField(writer, "skillRange", productionBill.allowedSkillRange.ToString()); WriteField(writer, "ingSearchRadius", productionBill.ingredientSearchRadius.ToString()); WriteField(writer, "repeatMode", productionBill.repeatMode.defName); WriteField(writer, "repeatCount", productionBill.repeatCount.ToString()); WriteField(writer, "targetCount", productionBill.targetCount.ToString()); WriteField(writer, "pauseWhenSatisfied", productionBill.pauseWhenSatisfied.ToString()); WriteField(writer, "unpauseWhenYouHave", productionBill.unpauseWhenYouHave.ToString()); WriteField(writer, "hpRange", productionBill.hpRange.ToString()); WriteField(writer, "qualityRange", productionBill.qualityRange.ToString()); WriteField(writer, "onlyAllowedIngredients", productionBill.limitToAllowedStuff.ToString()); BillStoreModeDef storeMode = productionBill.GetStoreMode(); WriteField(writer, "storeMode", storeMode.ToString()); if (storeMode == BillStoreModeDefOf.SpecificStockpile) { WriteField(writer, "storeZone", productionBill.GetStoreZone().label); } if (productionBill.includeFromZone != null) { WriteField(writer, "lookIn", productionBill.includeFromZone.label); } StringBuilder builder = new StringBuilder(); foreach (ThingDef thing in filter.AllowedThingDefs) { if (builder.Length > 0) { builder.Append("/"); } builder.Append(thing.defName); } WriteField(writer, "allowedDefs", builder.ToString()); if (filter.allowedHitPointsConfigurable) { builder = new StringBuilder(); builder.Append(Math.Round(filter.AllowedHitPointsPercents.min, 2).ToString()); builder.Append(":"); builder.Append(Math.Round(filter.AllowedHitPointsPercents.max, 2).ToString()); WriteField(writer, "allowedHitPointsPercents", builder.ToString()); } if (filter.allowedQualitiesConfigurable) { builder = new StringBuilder(); builder.Append(filter.AllowedQualityLevels.min.ToString()); builder.Append(":"); builder.Append(filter.AllowedQualityLevels.max.ToString()); WriteField(writer, "allowedQualities", builder.ToString()); } builder = new StringBuilder(); foreach (SpecialThingFilterDef def in reflection.DisallowedSpecialFilters) { if (builder.Length > 0) { builder.Append("/"); } builder.Append(def.defName); } WriteField(writer, "disallowedSpecialFilters", builder.ToString()); writer.WriteLine(BREAK); } } }
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); }
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); }
public static bool SaveStorageSettings(ThingFilter filter, FileInfo fi) { try { // Write Data using (FileStream fileStream = File.Open(fi.FullName, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fileStream)) { ThingFilterReflection tfr = new ThingFilterReflection(filter); WriteField(sw, "Version", "1"); StringBuilder sb = new StringBuilder(); foreach (ThingDef thing in tfr.AllStorableThingDefs) { if (filter.Allows(thing)) { if (sb.Length > 0) { sb.Append("/"); } sb.Append(thing.defName); } } WriteField(sw, "allowedDefs", sb.ToString()); if (filter.allowedHitPointsConfigurable) { sb = new StringBuilder(filter.AllowedHitPointsPercents.min.ToString("N4")); sb.Append(":"); sb.Append(filter.AllowedHitPointsPercents.max.ToString("N4")); WriteField(sw, "allowedHitPointsPercents", sb.ToString()); } if (filter.allowedQualitiesConfigurable) { sb = new StringBuilder(filter.AllowedQualityLevels.min.ToString()); sb.Append(":"); sb.Append(filter.AllowedQualityLevels.max.ToString()); WriteField(sw, "allowedQualities", sb.ToString()); } sb = new StringBuilder(); foreach (SpecialThingFilterDef def in tfr.DisallowedSpecialFilters) { if (sb.Length > 0) { sb.Append("/"); } sb.Append(def.defName); } WriteField(sw, "disallowedSpecialFilters", sb.ToString()); /*if (tfr.SpecialFiltersToAllow != null && tfr.SpecialFiltersToAllow.Count > 0) * { * sb = new StringBuilder(); * foreach (string s in tfr.SpecialFiltersToAllow) * { * if (sb.Length > 0) * sb.Append("/"); * sb.Append(s); * } * WriteField(sw, "specialFiltersToAllow", sb.ToString()); * } * * if (tfr.SpecialFiltersToDisallow != null && tfr.SpecialFiltersToDisallow.Count > 0) * { * sb = new StringBuilder(); * foreach (string s in tfr.SpecialFiltersToDisallow) * { * if (sb.Length > 0) * sb.Append("/"); * sb.Append(s); * } * WriteField(sw, "specialFiltersToDisallow", sb.ToString()); * }*/ } } } catch (Exception e) { LogException("Problem saving storage settings file '" + fi.Name + "'.", e); return(false); } return(true); }
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(); } } }