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; /// Item rewards quests drop at the trade spot land in delivery zones (1.1). public bool routeRewards = 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; /// Goods bought from trader caravans and Hospitality guests appear in delivery zones (1.1). public bool routeCaravan = true; /// One message per trade names the delivery zone the caravan goods went to (1.1). public bool caravanMessage = true; /// /// 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. /// public bool tradeShipsHintShown; 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 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; /// 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; private static BlaxZonesMod instance; public BlaxZonesMod(ModContentPack content) : base(content) { instance = this; Settings = GetSettings(); } /// /// 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. /// 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(); } } /// /// 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" : "")); } } }