212 lines
9.8 KiB
C#
212 lines
9.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using HarmonyLib;
|
|
using RimWorld;
|
|
using UnityEngine;
|
|
using Verse;
|
|
|
|
namespace BlaxLaws
|
|
{
|
|
/// <summary>Who enforces laws (setting "Enforcers").</summary>
|
|
public enum EnforcerMode
|
|
{
|
|
/// <summary>Wardens (Warden work active) first, then anyone able.</summary>
|
|
WardensFirst,
|
|
/// <summary>Only wardens; without one the case waits.</summary>
|
|
WardensOnly,
|
|
}
|
|
|
|
/// <summary>
|
|
/// Global settings (design: "Lawbook tab", settings page). The lawbook itself is per save and lives in
|
|
/// GameComponent_BlaxLaws, never here.
|
|
/// </summary>
|
|
public class BlaxLawsSettings : ModSettings
|
|
{
|
|
/// <summary>Wardens carry out punishments. Off: cases open and close, nothing is carried out.</summary>
|
|
public bool enforceLaws = true;
|
|
/// <summary>The Laws button shows in the bottom bar. Written live into MainButtonDef.buttonVisible.</summary>
|
|
public bool showLawsTab = true;
|
|
/// <summary>A message for every case (CaseMessages).</summary>
|
|
public bool caseMessages = true;
|
|
/// <summary>Letters when a colonist is jailed or the player has to act.</summary>
|
|
public bool jailLetters = true;
|
|
/// <summary>Who enforces.</summary>
|
|
public EnforcerMode enforcers = EnforcerMode.WardensFirst;
|
|
/// <summary>A subdued pawn loses the catharsis that ending its break gives.</summary>
|
|
public bool subdueRemovesCatharsis = true;
|
|
/// <summary>An act done while drafted or on a player-forced job opens no case.</summary>
|
|
public bool playerOrdersExempt = true;
|
|
/// <summary>Days between cases of one offender under one law for acts outside a break (0 to 7).</summary>
|
|
public int cooldownDays = DefaultCooldownDays;
|
|
/// <summary>Closed cases older than this are pruned on save (7 to 360).</summary>
|
|
public int keepClosedDays = DefaultKeepClosedDays;
|
|
|
|
public const int DefaultCooldownDays = 1;
|
|
public const int MinCooldownDays = 0;
|
|
public const int MaxCooldownDays = 7;
|
|
public const int DefaultKeepClosedDays = 60;
|
|
public const int MinKeepClosedDays = 7;
|
|
public const int MaxKeepClosedDays = 360;
|
|
|
|
public int CooldownDaysClamped => Mathf.Clamp(cooldownDays, MinCooldownDays, MaxCooldownDays);
|
|
|
|
public int KeepClosedDaysClamped => Mathf.Clamp(keepClosedDays, MinKeepClosedDays, MaxKeepClosedDays);
|
|
|
|
/// <summary>The cooldown window in ticks; 0 files every act.</summary>
|
|
public int CooldownTicks => CooldownDaysClamped * GenDate.TicksPerDay;
|
|
|
|
public int KeepClosedTicks => KeepClosedDaysClamped * GenDate.TicksPerDay;
|
|
|
|
public override void ExposeData()
|
|
{
|
|
base.ExposeData();
|
|
Scribe_Values.Look(ref enforceLaws, "enforceLaws", true);
|
|
Scribe_Values.Look(ref showLawsTab, "showLawsTab", true);
|
|
Scribe_Values.Look(ref caseMessages, "caseMessages", true);
|
|
Scribe_Values.Look(ref jailLetters, "jailLetters", true);
|
|
Scribe_Values.Look(ref enforcers, "enforcers", EnforcerMode.WardensFirst);
|
|
Scribe_Values.Look(ref subdueRemovesCatharsis, "subdueRemovesCatharsis", true);
|
|
Scribe_Values.Look(ref playerOrdersExempt, "playerOrdersExempt", true);
|
|
Scribe_Values.Look(ref cooldownDays, "cooldownDays", DefaultCooldownDays);
|
|
Scribe_Values.Look(ref keepClosedDays, "keepClosedDays", DefaultKeepClosedDays);
|
|
if (Scribe.mode == LoadSaveMode.LoadingVars || Scribe.mode == LoadSaveMode.PostLoadInit)
|
|
{
|
|
cooldownDays = CooldownDaysClamped;
|
|
keepClosedDays = KeepClosedDaysClamped;
|
|
}
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
var d = new BlaxLawsSettings();
|
|
enforceLaws = d.enforceLaws;
|
|
showLawsTab = d.showLawsTab;
|
|
caseMessages = d.caseMessages;
|
|
jailLetters = d.jailLetters;
|
|
enforcers = d.enforcers;
|
|
subdueRemovesCatharsis = d.subdueRemovesCatharsis;
|
|
playerOrdersExempt = d.playerOrdersExempt;
|
|
cooldownDays = d.cooldownDays;
|
|
keepClosedDays = d.keepClosedDays;
|
|
}
|
|
}
|
|
|
|
public class BlaxLawsMod : Mod
|
|
{
|
|
public static BlaxLawsSettings Settings;
|
|
|
|
/// <summary>The settings, or defaults before the mod class exists, so nothing reads null while loading.</summary>
|
|
public static BlaxLawsSettings S => Settings ?? (fallback ??= new BlaxLawsSettings());
|
|
private static BlaxLawsSettings fallback;
|
|
|
|
public BlaxLawsMod(ModContentPack content) : base(content)
|
|
{
|
|
Settings = GetSettings<BlaxLawsSettings>();
|
|
}
|
|
|
|
public override string SettingsCategory() => "Blax Laws";
|
|
|
|
/// <summary>
|
|
/// Writes "Show Laws tab" into the button def. The bottom bar counts buttonVisible for its width
|
|
/// (MainButtonsRoot.cs:15-27), so the change shows at once. Never throws.
|
|
/// </summary>
|
|
public static void ApplyTabVisibility()
|
|
{
|
|
try
|
|
{
|
|
MainButtonDef def = BlaxLawsDefOf.BlaxLaws_Laws ?? DefDatabase<MainButtonDef>.GetNamedSilentFail("BlaxLaws_Laws");
|
|
if (def != null)
|
|
def.buttonVisible = S.showLawsTab;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LawsLog.Once("Show Laws tab", e);
|
|
}
|
|
}
|
|
|
|
public override void DoSettingsWindowContents(Rect inRect)
|
|
{
|
|
BlaxLawsSettings s = Settings;
|
|
var l = new Listing_Standard();
|
|
l.maxOneColumn = true;
|
|
l.Begin(inRect);
|
|
|
|
l.CheckboxLabeled("BlaxLaws_SettingEnforceLaws".Translate(), ref s.enforceLaws, "BlaxLaws_SettingEnforceLawsTip".Translate());
|
|
bool tab = s.showLawsTab;
|
|
l.CheckboxLabeled("BlaxLaws_SettingShowTab".Translate(), ref s.showLawsTab, "BlaxLaws_SettingShowTabTip".Translate());
|
|
if (tab != s.showLawsTab)
|
|
ApplyTabVisibility();
|
|
l.CheckboxLabeled("BlaxLaws_SettingCaseMessages".Translate(), ref s.caseMessages, "BlaxLaws_SettingCaseMessagesTip".Translate());
|
|
l.CheckboxLabeled("BlaxLaws_SettingJailLetters".Translate(), ref s.jailLetters, "BlaxLaws_SettingJailLettersTip".Translate());
|
|
l.GapLine();
|
|
|
|
l.Label("BlaxLaws_SettingEnforcers".Translate());
|
|
if (l.RadioButton("BlaxLaws_EnforcersWardensFirst".Translate(), s.enforcers == EnforcerMode.WardensFirst, 12f, "BlaxLaws_EnforcersWardensFirstTip".Translate()))
|
|
s.enforcers = EnforcerMode.WardensFirst;
|
|
if (l.RadioButton("BlaxLaws_EnforcersWardensOnly".Translate(), s.enforcers == EnforcerMode.WardensOnly, 12f, "BlaxLaws_EnforcersWardensOnlyTip".Translate()))
|
|
s.enforcers = EnforcerMode.WardensOnly;
|
|
l.CheckboxLabeled("BlaxLaws_SettingSubdueCatharsis".Translate(), ref s.subdueRemovesCatharsis, "BlaxLaws_SettingSubdueCatharsisTip".Translate());
|
|
l.CheckboxLabeled("BlaxLaws_SettingPlayerOrdersExempt".Translate(), ref s.playerOrdersExempt, "BlaxLaws_SettingPlayerOrdersExemptTip".Translate());
|
|
l.GapLine();
|
|
|
|
s.cooldownDays = Mathf.RoundToInt(l.SliderLabeled("BlaxLaws_SettingCooldownDays".Translate(s.CooldownDaysClamped),
|
|
s.CooldownDaysClamped, BlaxLawsSettings.MinCooldownDays, BlaxLawsSettings.MaxCooldownDays, 0.6f,
|
|
"BlaxLaws_SettingCooldownDaysTip".Translate()));
|
|
s.cooldownDays = s.CooldownDaysClamped;
|
|
s.keepClosedDays = Mathf.RoundToInt(l.SliderLabeled("BlaxLaws_SettingKeepClosedDays".Translate(s.KeepClosedDaysClamped),
|
|
s.KeepClosedDaysClamped, BlaxLawsSettings.MinKeepClosedDays, BlaxLawsSettings.MaxKeepClosedDays, 0.6f,
|
|
"BlaxLaws_SettingKeepClosedDaysTip".Translate()));
|
|
s.keepClosedDays = s.KeepClosedDaysClamped;
|
|
|
|
l.Gap();
|
|
if (l.ButtonText("BlaxLaws_SettingsReset".Translate()))
|
|
{
|
|
s.Reset();
|
|
ApplyTabVisibility();
|
|
}
|
|
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, and logs
|
|
/// "[Blax Laws] N patches applied". A patch class is any class in this assembly with [HarmonyPatch].
|
|
/// </summary>
|
|
[StaticConstructorOnStartup]
|
|
public static class BlaxLawsStartup
|
|
{
|
|
public const string HarmonyId = "blax.laws";
|
|
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 List<string> FailedPatches = new List<string>();
|
|
|
|
/// <summary>Patch classes applied at startup.</summary>
|
|
public static int Applied { get; private set; }
|
|
|
|
static BlaxLawsStartup()
|
|
{
|
|
int ok = 0;
|
|
foreach (Type type in typeof(BlaxLawsStartup).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 Laws] Patch " + type.Name + " failed: " + e);
|
|
}
|
|
}
|
|
Applied = ok;
|
|
Log.Message("[Blax Laws] " + ok + " patches applied" + (FailedPatches.Count > 0 ? ", " + FailedPatches.Count + " failed" : ""));
|
|
BlaxLawsMod.ApplyTabVisibility();
|
|
}
|
|
}
|
|
}
|