rimworld-blax-zones/Source/BlaxZones/BlaxZonesMod.cs

174 lines
8.3 KiB
C#
Raw Permalink Normal View History

using System;
using HarmonyLib;
using UnityEngine;
using Verse;
namespace BlaxZones
{
/// <summary>
/// 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.
/// </summary>
public class BlaxZonesSettings : ModSettings
{
/// <summary>The delivery zone designators show in the Zone menu. Off hides them; existing zones stay and still route.</summary>
public bool deliveryZones = true;
/// <summary>What the player buys from orbital traders, and the silver from selling, lands in delivery zones.</summary>
public bool routeOrbital = true;
/// <summary>Item rewards quests drop at the trade spot land in delivery zones (1.1).</summary>
public bool routeRewards = true;
/// <summary>When every zone cell is taken, pods land next to the top zone. Off: overflow goes to the vanilla spot.</summary>
public bool overflowNearZone = true;
/// <summary>How far from the top zone overflow pods may land. Never below 5: the vanilla search can hang below it.</summary>
public int overflowRadius = DefaultOverflowRadius;
/// <summary>Trader caravans and tribute collectors camp next to a zone with "Caravans camp here" ticked.</summary>
public bool campAtZones = true;
/// <summary>Goods bought from trader caravans and Hospitality guests appear in delivery zones (1.1).</summary>
public bool routeCaravan = true;
/// <summary>One message per trade names the delivery zone the caravan goods went to (1.1).</summary>
public bool caravanMessage = true;
/// <summary>
/// The Trade Ships Drop Spot hint was shown (1.1). Not a setting the player sees: it makes the hint
/// one-time across saves and games without putting anything in a save. Reset to defaults keeps it.
/// </summary>
public bool tradeShipsHintShown;
public const int DefaultOverflowRadius = 16;
public const int MinOverflowRadius = 5;
public const int MaxOverflowRadius = 30;
/// <summary>The overflow radius as the router must use it, clamped to 5..30 whatever the file says.</summary>
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 routeRewards, "routeRewards", true);
Scribe_Values.Look(ref overflowNearZone, "overflowNearZone", true);
Scribe_Values.Look(ref overflowRadius, "overflowRadius", DefaultOverflowRadius);
Scribe_Values.Look(ref campAtZones, "campAtZones", true);
Scribe_Values.Look(ref routeCaravan, "routeCaravan", true);
Scribe_Values.Look(ref caravanMessage, "caravanMessage", true);
Scribe_Values.Look(ref tradeShipsHintShown, "tradeShipsHintShown", false);
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;
routeRewards = d.routeRewards;
overflowNearZone = d.overflowNearZone;
overflowRadius = d.overflowRadius;
campAtZones = d.campAtZones;
routeCaravan = d.routeCaravan;
caravanMessage = d.caravanMessage;
}
}
public class BlaxZonesMod : Mod
{
public static BlaxZonesSettings Settings;
/// <summary>The settings, or defaults before the mod class exists, so nothing reads null while loading.</summary>
public static BlaxZonesSettings S => Settings ?? (fallback ??= new BlaxZonesSettings());
private static BlaxZonesSettings fallback;
private static BlaxZonesMod instance;
public BlaxZonesMod(ModContentPack content) : base(content)
{
instance = this;
Settings = GetSettings<BlaxZonesSettings>();
}
/// <summary>
/// Writes the settings file now (the hint flag), as closing the settings window does. Skipped while
/// another Scribe session runs (the flag then waits in memory for the next write). Never throws.
/// </summary>
public static void SaveSettings()
{
try
{
if (Scribe.mode == LoadSaveMode.Inactive)
instance?.WriteSettings();
}
catch (Exception e)
{
ZonesLog.Once("Writing the mod settings", e);
}
}
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_SettingRouteRewards".Translate(), ref s.routeRewards, "BlaxZones_SettingRouteRewardsTip".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_SettingRouteCaravan".Translate(), ref s.routeCaravan, "BlaxZones_SettingRouteCaravanTip".Translate());
l.CheckboxLabeled("BlaxZones_SettingCaravanMessage".Translate(), ref s.caravanMessage, "BlaxZones_SettingCaravanMessageTip".Translate());
l.CheckboxLabeled("BlaxZones_SettingCampAtZones".Translate(), ref s.campAtZones, "BlaxZones_SettingCampAtZonesTip".Translate());
l.Gap();
if (l.ButtonText("BlaxZones_SettingsReset".Translate()))
s.Reset();
l.End();
}
}
/// <summary>
/// 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.
/// </summary>
[StaticConstructorOnStartup]
public static class BlaxZonesStartup
{
public const string HarmonyId = "blax.zones";
public static readonly Harmony Harmony = new Harmony(HarmonyId);
/// <summary>Patch classes that failed to apply, by type name. Read by the smoke test.</summary>
public static readonly System.Collections.Generic.List<string> FailedPatches = new System.Collections.Generic.List<string>();
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" : ""));
}
}
}