2026-11-30 21:33:42 +00:00
|
|
|
using System;
|
2026-11-29 18:26:50 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using BlaxDefaults;
|
|
|
|
|
using RimWorld;
|
2026-11-30 21:33:42 +00:00
|
|
|
using RimWorld.Planet;
|
2026-11-29 18:26:50 +00:00
|
|
|
using Verse;
|
|
|
|
|
|
|
|
|
|
namespace BlaxDefaultsTests
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2026-11-30 21:33:42 +00:00
|
|
|
/// Shared helpers for tests. Things, pawns, 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
|
2026-11-29 18:26:50 +00:00
|
|
|
/// 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>();
|
2026-11-30 21:33:42 +00:00
|
|
|
private static readonly List<Pawn> madePawns = new List<Pawn>();
|
|
|
|
|
private static readonly List<Action> afterTest = new List<Action>();
|
2026-11-29 18:26:50 +00:00
|
|
|
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,
|
2026-11-30 21:33:42 +00:00
|
|
|
/// inside a district (pawns spawned there can walk and work as in vanilla; the map centre can be rock), heavy-supporting
|
|
|
|
|
/// terrain, no zone, nothing but grass or filth, which is removed. Searches 40 cells around the centre,
|
|
|
|
|
/// then the whole map by distance. Tracked: buildings, blueprints, frames, items and zones in it are
|
|
|
|
|
/// removed after the test (kept ones stay).
|
2026-11-29 18:26:50 +00:00
|
|
|
/// </summary>
|
|
|
|
|
public static CellRect EmptyArea(int width, int height)
|
|
|
|
|
{
|
|
|
|
|
Map map = Map;
|
2026-11-30 21:33:42 +00:00
|
|
|
IEnumerable<IntVec3> near = GenRadial.RadialCellsAround(map.Center, 40f, useCenter: true);
|
|
|
|
|
IEnumerable<IntVec3> rest = map.AllCells.OrderBy(c => (c - map.Center).LengthHorizontalSquared);
|
|
|
|
|
foreach (IntVec3 origin in near.Concat(rest))
|
2026-11-29 18:26:50 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-30 21:33:42 +00:00
|
|
|
T.Fail("no empty " + width + "x" + height + " area on the map");
|
2026-11-29 18:26:50 +00:00
|
|
|
return default;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool Free(IntVec3 c, Map map) =>
|
2026-11-30 21:33:42 +00:00
|
|
|
c.InBounds(map) && !c.CloseToEdge(map, 5) && !c.Fogged(map) && c.Standable(map) && c.GetDistrict(map) != null
|
2026-11-29 18:26:50 +00:00
|
|
|
&& 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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-30 21:33:42 +00:00
|
|
|
/// <summary>Destroys the pawn (or its corpse) after the test unless kept.</summary>
|
|
|
|
|
public static Pawn TrackPawn(Pawn p)
|
|
|
|
|
{
|
|
|
|
|
if (p != null) madePawns.Add(p);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Runs after the test, last added first, before the things and zones are removed (restore a value the test changed).</summary>
|
|
|
|
|
public static void AfterTest(Action undo)
|
|
|
|
|
{
|
|
|
|
|
if (undo != null) afterTest.Add(undo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>A visible, non-player, humanlike faction that is not hostile to the player, or null.</summary>
|
|
|
|
|
public static Faction FriendlyFaction() =>
|
|
|
|
|
Find.FactionManager.AllFactionsVisible
|
|
|
|
|
.Where(f => !f.IsPlayer && !f.defeated && f.def.humanlikeFaction && !f.HostileTo(Faction.OfPlayer))
|
|
|
|
|
.OrderBy(f => f.loadID).FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
/// <summary>A visible, non-player, humanlike faction hostile to the player, or null.</summary>
|
|
|
|
|
public static Faction HostileFaction() =>
|
|
|
|
|
Find.FactionManager.AllFactionsVisible
|
|
|
|
|
.Where(f => !f.IsPlayer && !f.defeated && f.def.humanlikeFaction && f.HostileTo(Faction.OfPlayer))
|
|
|
|
|
.OrderBy(f => f.loadID).FirstOrDefault();
|
|
|
|
|
|
2026-11-29 18:26:50 +00:00
|
|
|
/// <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()
|
|
|
|
|
{
|
2026-11-30 21:33:42 +00:00
|
|
|
for (int i = afterTest.Count - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
try { afterTest[i](); }
|
|
|
|
|
catch (Exception e) { T.Info("after-test step threw: " + e.Message); }
|
|
|
|
|
}
|
|
|
|
|
afterTest.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (Pawn p in madePawns)
|
|
|
|
|
{
|
|
|
|
|
if (p == null || kept.Contains(p)) continue;
|
|
|
|
|
if (p.Corpse != null && !kept.Contains(p.Corpse) && !p.Corpse.Destroyed)
|
|
|
|
|
p.Corpse.Destroy(DestroyMode.Vanish);
|
|
|
|
|
else if (p.Spawned)
|
|
|
|
|
p.Destroy(DestroyMode.Vanish);
|
|
|
|
|
else if (!p.Destroyed && p.ParentHolder is Pawn_CarryTracker carrier)
|
|
|
|
|
carrier.innerContainer.Remove(p);
|
|
|
|
|
if (!p.Destroyed && !p.Spawned && p.IsWorldPawn())
|
|
|
|
|
Find.WorldPawns.RemoveAndDiscardPawnViaGC(p);
|
|
|
|
|
}
|
|
|
|
|
madePawns.Clear();
|
|
|
|
|
|
2026-11-29 18:26:50 +00:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|