using System;
using HarmonyLib;
using UnityEngine;
using Verse;
namespace BlaxZones
{
///
/// Global settings. Every feature has a toggle; zone storage has none, it is the save format.
/// Per-zone choices live on the zone (Zone_Delivery), not here.
///
public class BlaxZonesSettings : ModSettings
{
/// The delivery zone designators show in the Zone menu. Off hides them; existing zones stay and still route.
public bool deliveryZones = true;
/// What the player buys from orbital traders, and the silver from selling, lands in delivery zones.
public bool routeOrbital = true;
/// When every zone cell is taken, pods land next to the top zone. Off: overflow goes to the vanilla spot.
public bool overflowNearZone = true;
/// How far from the top zone overflow pods may land. Never below 5: the vanilla search can hang below it.
public int overflowRadius = DefaultOverflowRadius;
/// Trader caravans and tribute collectors camp next to a zone with "Caravans camp here" ticked.
public bool campAtZones = true;
public const int DefaultOverflowRadius = 16;
public const int MinOverflowRadius = 5;
public const int MaxOverflowRadius = 30;
/// The overflow radius as the router must use it, clamped to 5..30 whatever the file says.
public int OverflowRadiusClamped => Mathf.Clamp(overflowRadius, MinOverflowRadius, MaxOverflowRadius);
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref deliveryZones, "deliveryZones", true);
Scribe_Values.Look(ref routeOrbital, "routeOrbital", true);
Scribe_Values.Look(ref overflowNearZone, "overflowNearZone", true);
Scribe_Values.Look(ref overflowRadius, "overflowRadius", DefaultOverflowRadius);
Scribe_Values.Look(ref campAtZones, "campAtZones", true);
if (Scribe.mode == LoadSaveMode.PostLoadInit || Scribe.mode == LoadSaveMode.LoadingVars)
overflowRadius = Mathf.Clamp(overflowRadius, MinOverflowRadius, MaxOverflowRadius);
}
public void Reset()
{
var d = new BlaxZonesSettings();
deliveryZones = d.deliveryZones;
routeOrbital = d.routeOrbital;
overflowNearZone = d.overflowNearZone;
overflowRadius = d.overflowRadius;
campAtZones = d.campAtZones;
}
}
public class BlaxZonesMod : Mod
{
public static BlaxZonesSettings Settings;
/// The settings, or defaults before the mod class exists, so nothing reads null while loading.
public static BlaxZonesSettings S => Settings ?? (fallback ??= new BlaxZonesSettings());
private static BlaxZonesSettings fallback;
public BlaxZonesMod(ModContentPack content) : base(content)
{
Settings = GetSettings();
}
public override string SettingsCategory() => "Blax Zones";
public override void DoSettingsWindowContents(Rect inRect)
{
BlaxZonesSettings s = Settings;
var l = new Listing_Standard();
l.maxOneColumn = true;
l.Begin(inRect);
l.CheckboxLabeled("BlaxZones_SettingDeliveryZones".Translate(), ref s.deliveryZones, "BlaxZones_SettingDeliveryZonesTip".Translate());
l.CheckboxLabeled("BlaxZones_SettingRouteOrbital".Translate(), ref s.routeOrbital, "BlaxZones_SettingRouteOrbitalTip".Translate());
l.CheckboxLabeled("BlaxZones_SettingOverflowNearZone".Translate(), ref s.overflowNearZone, "BlaxZones_SettingOverflowNearZoneTip".Translate());
if (s.overflowNearZone)
{
s.overflowRadius = Mathf.RoundToInt(l.SliderLabeled("BlaxZones_SettingOverflowRadius".Translate(s.OverflowRadiusClamped),
s.OverflowRadiusClamped, BlaxZonesSettings.MinOverflowRadius, BlaxZonesSettings.MaxOverflowRadius, 0.6f,
"BlaxZones_SettingOverflowRadiusTip".Translate()));
s.overflowRadius = Mathf.Clamp(s.overflowRadius, BlaxZonesSettings.MinOverflowRadius, BlaxZonesSettings.MaxOverflowRadius);
}
l.GapLine();
l.CheckboxLabeled("BlaxZones_SettingCampAtZones".Translate(), ref s.campAtZones, "BlaxZones_SettingCampAtZonesTip".Translate());
l.Gap();
if (l.ButtonText("BlaxZones_SettingsReset".Translate()))
s.Reset();
l.End();
}
}
///
/// Applies the Harmony patches after every Mod class exists (so the Harmony mod has set up
/// its 1.7 backend), one class at a time so a single broken target can't take the rest down.
///
[StaticConstructorOnStartup]
public static class BlaxZonesStartup
{
public const string HarmonyId = "blax.zones";
public static readonly Harmony Harmony = new Harmony(HarmonyId);
/// Patch classes that failed to apply, by type name. Read by the smoke test.
public static readonly System.Collections.Generic.List FailedPatches = new System.Collections.Generic.List();
static BlaxZonesStartup()
{
int ok = 0;
foreach (Type type in typeof(BlaxZonesStartup).Assembly.GetTypes())
{
if (!type.IsDefined(typeof(HarmonyPatch), false))
continue;
try
{
Harmony.CreateClassProcessor(type).Patch();
ok++;
}
catch (Exception e)
{
FailedPatches.Add(type.Name);
Log.Error("[Blax Zones] Patch " + type.Name + " failed: " + e);
}
}
Log.Message("[Blax Zones] " + ok + " patches applied" + (FailedPatches.Count > 0 ? ", " + FailedPatches.Count + " failed" : ""));
}
}
}