using System;
using System.Collections.Generic;
using Verse;
namespace BlaxDefaults
{
///
/// 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.
///
public abstract class DefaultsEntry : IExposable
{
public string key;
public virtual void ExposeData()
{
Scribe_Values.Look(ref key, "key");
}
/// A deep copy. The library hands out its live entries; edit a clone, then DefaultsLibrary.Set it.
public abstract DefaultsEntry Clone();
///
/// 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.
///
public abstract string Fingerprint();
/// Names in this entry that don't resolve now: from an inactive mod or a DLC that is off.
public virtual IEnumerable UnresolvedNames()
{
yield break;
}
public override string ToString() => GetType().Name + "(" + key + ")";
protected static string Join(IEnumerable names) => names == null ? "" : string.Join(",", names);
protected static List CopyList(List list) => list == null ? new List() : new List(list);
protected static string SortedJoin(List names)
{
if (names == null)
return "";
var copy = new List(names);
copy.Sort(StringComparer.Ordinal);
return string.Join(",", copy);
}
}
///
/// The library keys. One default per kind: "stockpile:Stockpile", "stockpile:Dumping",
/// "stockpile:<designator type name>", "storage:<ThingDef>" and "bill:*" in 1.0. Later releases
/// add their own prefixes (bill:<RecipeDef>, bench:, animal:, slaughter:, mech:, grower:, building:).
///
public static class DefaultsKeys
{
public const string StockpilePrefix = "stockpile:";
public const string StoragePrefix = "storage:";
public const string BillPrefix = "bill:";
/// The vanilla stockpile (DefaultStockpile preset): Designator_ZoneAddStockpile_Resources and _Expand.
public const string Stockpile = "stockpile:Stockpile";
/// The vanilla dumping stockpile: Designator_ZoneAddStockpile_Dumping.
public const string Dumping = "stockpile:Dumping";
/// "Every new bill", the values applied to every new production bill.
public const string EveryBill = "bill:*";
public static string ForStorage(ThingDef def) => StoragePrefix + def.defName;
public static string ForStorage(string defName) => StoragePrefix + defName;
/// A modded stockpile designator: its own type, keyed by type name.
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);
/// The part after the prefix: "Shelf" for "storage:Shelf".
public static string NameOf(string key)
{
int i = key?.IndexOf(':') ?? -1;
return i < 0 ? key : key.Substring(i + 1);
}
}
}