rimworld-blax-defaults/Source/BlaxDefaults/DefaultsEntry.cs

101 lines
4.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using Verse;
namespace BlaxDefaults
{
/// <summary>
/// One saved default, keyed by kind (see DefaultsKeys). Stored in the defaults file as strings only:
/// defNames, enum names, numbers. Loading never resolves a def, so it can never throw over a missing
/// one; names resolve at apply time with GetNamedSilentFail, and a name that doesn't resolve is skipped
/// and stays in the file. Changes to the format are additive only.
/// </summary>
public abstract class DefaultsEntry : IExposable
{
public string key;
public virtual void ExposeData()
{
Scribe_Values.Look(ref key, "key");
}
/// <summary>A deep copy. The library hands out its live entries; edit a clone, then DefaultsLibrary.Set it.</summary>
public abstract DefaultsEntry Clone();
/// <summary>
/// Every value of the entry as one string, key included. The file write compares the read-back
/// entries with the library through this, so it must cover every field ExposeData writes.
/// </summary>
public abstract string Fingerprint();
/// <summary>Names in this entry that don't resolve now: from an inactive mod or a DLC that is off.</summary>
public virtual IEnumerable<string> UnresolvedNames()
{
yield break;
}
public override string ToString() => GetType().Name + "(" + key + ")";
protected static string Join(IEnumerable<string> names) => names == null ? "" : string.Join(",", names);
protected static List<string> CopyList(List<string> list) => list == null ? new List<string>() : new List<string>(list);
protected static string SortedJoin(List<string> names)
{
if (names == null)
return "";
var copy = new List<string>(names);
copy.Sort(StringComparer.Ordinal);
return string.Join(",", copy);
}
}
/// <summary>
/// The library keys. One default per kind: "stockpile:Stockpile", "stockpile:Dumping",
/// "stockpile:&lt;designator type name&gt;", "storage:&lt;ThingDef&gt;" and "bill:*" in 1.0, "forbid:colony" and
/// "forbid:enemies" (the forbid rules' filters) in 1.1. Later releases
/// add their own prefixes (bill:&lt;RecipeDef&gt;, bench:, animal:, slaughter:, mech:, grower:, building:).
/// </summary>
public static class DefaultsKeys
{
public const string StockpilePrefix = "stockpile:";
public const string StoragePrefix = "storage:";
public const string BillPrefix = "bill:";
public const string ForbidPrefix = "forbid:";
/// <summary>The filter of "Gear your colony drops" (1.1).</summary>
public const string ForbidColony = "forbid:colony";
/// <summary>The filter of "Gear enemies drop" (1.1).</summary>
public const string ForbidEnemies = "forbid:enemies";
/// <summary>The vanilla stockpile (DefaultStockpile preset): Designator_ZoneAddStockpile_Resources and _Expand.</summary>
public const string Stockpile = "stockpile:Stockpile";
/// <summary>The vanilla dumping stockpile: Designator_ZoneAddStockpile_Dumping.</summary>
public const string Dumping = "stockpile:Dumping";
/// <summary>"Every new bill", the values applied to every new production bill.</summary>
public const string EveryBill = "bill:*";
public static string ForStorage(ThingDef def) => StoragePrefix + def.defName;
public static string ForStorage(string defName) => StoragePrefix + defName;
/// <summary>A modded stockpile designator: its own type, keyed by type name.</summary>
public static string ForStockpileDesignator(Type designatorType) => StockpilePrefix + designatorType.Name;
public static bool IsStockpile(string key) => key != null && key.StartsWith(StockpilePrefix, StringComparison.Ordinal);
public static bool IsStorage(string key) => key != null && key.StartsWith(StoragePrefix, StringComparison.Ordinal);
public static bool IsBill(string key) => key != null && key.StartsWith(BillPrefix, StringComparison.Ordinal);
public static bool IsForbid(string key) => key != null && key.StartsWith(ForbidPrefix, StringComparison.Ordinal);
/// <summary>The part after the prefix: "Shelf" for "storage:Shelf".</summary>
public static string NameOf(string key)
{
int i = key?.IndexOf(':') ?? -1;
return i < 0 ? key : key.Substring(i + 1);
}
}
}