242 lines
9.5 KiB
C#
242 lines
9.5 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Reflection;
|
||
|
|
using BlaxDefaults;
|
||
|
|
using RimWorld;
|
||
|
|
using Verse;
|
||
|
|
|
||
|
|
namespace BlaxDefaultsTests
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Shared helpers for tests. Things, zones and areas made here are removed after each test unless passed
|
||
|
|
/// to Keep() or KeepZone(). Mod settings and the defaults library are restored after each test (the
|
||
|
|
/// library in memory only, the file is not rewritten), so tests may change them freely. Feature-specific
|
||
|
|
/// helpers belong in the feature's own test file.
|
||
|
|
/// </summary>
|
||
|
|
public static class Fixtures
|
||
|
|
{
|
||
|
|
private static readonly List<Thing> made = new List<Thing>();
|
||
|
|
private static readonly HashSet<Thing> kept = new HashSet<Thing>();
|
||
|
|
private static readonly List<Zone> madeZones = new List<Zone>();
|
||
|
|
private static readonly HashSet<Zone> keptZones = new HashSet<Zone>();
|
||
|
|
private static readonly List<CellRect> areas = new List<CellRect>();
|
||
|
|
private static List<DefaultsEntry> librarySnapshot;
|
||
|
|
private static bool keepLibrary;
|
||
|
|
|
||
|
|
public static Map Map => Find.CurrentMap;
|
||
|
|
|
||
|
|
public static BlaxDefaultsSettings Settings => BlaxDefaultsMod.Settings;
|
||
|
|
|
||
|
|
// ---- defaults library ------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>Saves the entry with DefaultsLibrary.Set and returns it. Restored after the test.</summary>
|
||
|
|
public static TE Save<TE>(TE entry) where TE : DefaultsEntry
|
||
|
|
{
|
||
|
|
DefaultsLibrary.Set(entry);
|
||
|
|
return entry;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>A storage template under this key that allows exactly these defs (unlisted Game), not saved.</summary>
|
||
|
|
public static StorageTemplate Template(StorageKind kind, StoragePriority priority, params ThingDef[] allow)
|
||
|
|
{
|
||
|
|
StorageSettings s = kind.GameDefault();
|
||
|
|
s.filter.SetDisallowAll();
|
||
|
|
foreach (ThingDef d in allow)
|
||
|
|
s.filter.SetAllow(d, true);
|
||
|
|
s.Priority = priority;
|
||
|
|
return kind.Capture(s);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Leave the library as this test set it (phase-1 setup for phase-2 checks).</summary>
|
||
|
|
public static void KeepLibrary() => keepLibrary = true;
|
||
|
|
|
||
|
|
public static StorageKind Kind(string thingDefName) =>
|
||
|
|
StorageKinds.ForDef(NeedDef(DefDatabase<ThingDef>.GetNamedSilentFail(thingDefName), "ThingDef " + thingDefName));
|
||
|
|
|
||
|
|
public static StorageKind StockpileKind => StorageKinds.ForStockpile(StorageKinds.VanillaStockpile);
|
||
|
|
|
||
|
|
public static StorageKind DumpingKind => StorageKinds.ForStockpile(StorageKinds.VanillaDumping);
|
||
|
|
|
||
|
|
// ---- defs ------------------------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>A def that must exist; fails the test otherwise.</summary>
|
||
|
|
public static TD NeedDef<TD>(TD def, string what) where TD : Def
|
||
|
|
{
|
||
|
|
T.NotNull(def, what);
|
||
|
|
return def;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static ThingDef Thing(string defName) => NeedDef(DefDatabase<ThingDef>.GetNamedSilentFail(defName), "ThingDef " + defName);
|
||
|
|
|
||
|
|
/// <summary>Skips the test unless the DLC is active: "needs Biotech".</summary>
|
||
|
|
public static void NeedDlc(string dlcPackageId)
|
||
|
|
{
|
||
|
|
if (!DlcSupport.IsActive(dlcPackageId))
|
||
|
|
T.Skip("needs " + DlcSupport.DlcName(dlcPackageId));
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- map, things, zones ----------------------------------------------------------
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// A free area of this size near the map centre: in bounds, away from the edge, unfogged, standable,
|
||
|
|
/// heavy-supporting terrain, no zone, nothing but grass or filth, which is removed. Tracked: buildings,
|
||
|
|
/// blueprints, frames, items and zones in it are removed after the test (kept ones stay).
|
||
|
|
/// </summary>
|
||
|
|
public static CellRect EmptyArea(int width, int height)
|
||
|
|
{
|
||
|
|
Map map = Map;
|
||
|
|
foreach (IntVec3 origin in GenRadial.RadialCellsAround(map.Center, 40f, useCenter: true))
|
||
|
|
{
|
||
|
|
var rect = new CellRect(origin.x, origin.z, width, height);
|
||
|
|
if (rect.Cells.All(c => Free(c, map)))
|
||
|
|
{
|
||
|
|
foreach (IntVec3 c in rect.Cells)
|
||
|
|
foreach (Thing t in map.thingGrid.ThingsListAt(c).ToList())
|
||
|
|
t.Destroy(DestroyMode.Vanish);
|
||
|
|
areas.Add(rect);
|
||
|
|
return rect;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
T.Fail("no empty " + width + "x" + height + " area near the map centre");
|
||
|
|
return default;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static bool Free(IntVec3 c, Map map) =>
|
||
|
|
c.InBounds(map) && !c.CloseToEdge(map, 5) && !c.Fogged(map) && c.Standable(map)
|
||
|
|
&& c.SupportsStructureType(map, TerrainAffordanceDefOf.Heavy) && c.GetZone(map) == null
|
||
|
|
&& map.thingGrid.ThingsListAt(c).All(t => (t is Plant plant && !plant.def.plant.IsTree) || t is Filth)
|
||
|
|
&& !areas.Any(r => r.Contains(c));
|
||
|
|
|
||
|
|
/// <summary>Removes the thing after the test unless kept.</summary>
|
||
|
|
public static TT Track<TT>(TT thing) where TT : Thing
|
||
|
|
{
|
||
|
|
if (thing != null) made.Add(thing);
|
||
|
|
return thing;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static TZ TrackZone<TZ>(TZ zone) where TZ : Zone
|
||
|
|
{
|
||
|
|
if (zone != null) madeZones.Add(zone);
|
||
|
|
return zone;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>Exclude a thing from the per-test cleanup (phase-1 setup for phase-2 checks).</summary>
|
||
|
|
public static TT Keep<TT>(TT thing) where TT : Thing
|
||
|
|
{
|
||
|
|
kept.Add(thing);
|
||
|
|
return thing;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static TZ KeepZone<TZ>(TZ zone) where TZ : Zone
|
||
|
|
{
|
||
|
|
keptZones.Add(zone);
|
||
|
|
return zone;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>A spawned free colonist of the quick-test colony, to complete frames with.</summary>
|
||
|
|
public static Pawn Worker()
|
||
|
|
{
|
||
|
|
Pawn p = Map.mapPawns.FreeColonistsSpawned.FirstOrDefault();
|
||
|
|
T.NotNull(p, "a spawned free colonist");
|
||
|
|
return p;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Thing FindThingById(string thingId)
|
||
|
|
{
|
||
|
|
if (thingId == null) return null;
|
||
|
|
foreach (Map m in Find.Maps)
|
||
|
|
foreach (Thing t in m.listerThings.AllThings)
|
||
|
|
if (t.ThingID == thingId) return t;
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static Zone FindZoneById(int id)
|
||
|
|
{
|
||
|
|
foreach (Map m in Find.Maps)
|
||
|
|
foreach (Zone z in m.zoneManager.AllZones)
|
||
|
|
if (z.ID == id) return z;
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- time ------------------------------------------------------------------------
|
||
|
|
|
||
|
|
public static void Tick(int ticks)
|
||
|
|
{
|
||
|
|
for (int i = 0; i < ticks; i++) Find.TickManager.DoSingleTick();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void QuietStoryteller()
|
||
|
|
{
|
||
|
|
Find.Storyteller.incidentQueue.Clear();
|
||
|
|
Find.Storyteller.storytellerComps.Clear();
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---- settings and cleanup (used by the runner) -----------------------------------
|
||
|
|
|
||
|
|
public static Dictionary<FieldInfo, object> SnapshotSettings()
|
||
|
|
{
|
||
|
|
keepLibrary = false;
|
||
|
|
librarySnapshot = DefaultsLibrary.Snapshot();
|
||
|
|
var d = new Dictionary<FieldInfo, object>();
|
||
|
|
if (Settings == null) return d;
|
||
|
|
foreach (FieldInfo f in typeof(BlaxDefaultsSettings).GetFields(BindingFlags.Public | BindingFlags.Instance))
|
||
|
|
{
|
||
|
|
object v = f.GetValue(Settings);
|
||
|
|
d[f] = v is List<string> list ? new List<string>(list) : v;
|
||
|
|
}
|
||
|
|
return d;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void RestoreSettings(Dictionary<FieldInfo, object> snapshot)
|
||
|
|
{
|
||
|
|
if (Settings != null)
|
||
|
|
{
|
||
|
|
foreach (var kv in snapshot)
|
||
|
|
kv.Key.SetValue(Settings, kv.Value is List<string> list ? new List<string>(list) : kv.Value);
|
||
|
|
}
|
||
|
|
if (!keepLibrary && librarySnapshot != null)
|
||
|
|
DefaultsLibrary.RestoreForTests(librarySnapshot);
|
||
|
|
keepLibrary = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void Cleanup()
|
||
|
|
{
|
||
|
|
foreach (Thing t in made)
|
||
|
|
{
|
||
|
|
if (t != null && !kept.Contains(t) && !t.Destroyed && t.Spawned)
|
||
|
|
t.Destroy(DestroyMode.Vanish);
|
||
|
|
}
|
||
|
|
made.Clear();
|
||
|
|
|
||
|
|
foreach (Zone z in madeZones)
|
||
|
|
{
|
||
|
|
if (z != null && !keptZones.Contains(z) && z.zoneManager != null && z.zoneManager.AllZones.Contains(z))
|
||
|
|
z.Delete(playSound: false);
|
||
|
|
}
|
||
|
|
madeZones.Clear();
|
||
|
|
|
||
|
|
Map map = Map;
|
||
|
|
if (map != null)
|
||
|
|
{
|
||
|
|
foreach (CellRect rect in areas)
|
||
|
|
{
|
||
|
|
foreach (IntVec3 c in rect.Cells)
|
||
|
|
{
|
||
|
|
if (!c.InBounds(map)) continue;
|
||
|
|
Zone z = c.GetZone(map);
|
||
|
|
if (z != null && !keptZones.Contains(z))
|
||
|
|
z.Delete(playSound: false);
|
||
|
|
foreach (Thing t in map.thingGrid.ThingsListAt(c).ToList())
|
||
|
|
{
|
||
|
|
if (t is Pawn || kept.Contains(t) || t.Destroyed) continue;
|
||
|
|
if (t is Building || t is Blueprint || t is Frame || t.def.EverHaulable)
|
||
|
|
t.Destroy(DestroyMode.Vanish);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
areas.Clear();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|