using System;
using System.Collections.Generic;
using HarmonyLib;
using RimWorld;
using UnityEngine;
using Verse;
namespace BlaxLaws
{
/// Who enforces laws (setting "Enforcers").
public enum EnforcerMode
{
/// Wardens (Warden work active) first, then anyone able.
WardensFirst,
/// Only wardens; without one the case waits.
WardensOnly,
}
///
/// Global settings (design: "Lawbook tab", settings page). The lawbook itself is per save and lives in
/// GameComponent_BlaxLaws, never here.
///
public class BlaxLawsSettings : ModSettings
{
/// Wardens carry out punishments. Off: cases open and close, nothing is carried out.
public bool enforceLaws = true;
/// The Laws button shows in the bottom bar. Written live into MainButtonDef.buttonVisible.
public bool showLawsTab = true;
/// A message for every case (CaseMessages).
public bool caseMessages = true;
/// Letters when a colonist is jailed or the player has to act.
public bool jailLetters = true;
/// Who enforces.
public EnforcerMode enforcers = EnforcerMode.WardensFirst;
/// A subdued pawn loses the catharsis that ending its break gives.
public bool subdueRemovesCatharsis = true;
/// An act done while drafted or on a player-forced job opens no case.
public bool playerOrdersExempt = true;
/// Days between cases of one offender under one law for acts outside a break (0 to 7).
public int cooldownDays = DefaultCooldownDays;
/// Closed cases older than this are pruned on save (7 to 360).
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);
/// The cooldown window in ticks; 0 files every act.
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;
/// The settings, or defaults before the mod class exists, so nothing reads null while loading.
public static BlaxLawsSettings S => Settings ?? (fallback ??= new BlaxLawsSettings());
private static BlaxLawsSettings fallback;
public BlaxLawsMod(ModContentPack content) : base(content)
{
Settings = GetSettings();
}
public override string SettingsCategory() => "Blax Laws";
///
/// 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.
///
public static void ApplyTabVisibility()
{
try
{
MainButtonDef def = BlaxLawsDefOf.BlaxLaws_Laws ?? DefDatabase.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();
}
}
///
/// 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].
///
[StaticConstructorOnStartup]
public static class BlaxLawsStartup
{
public const string HarmonyId = "blax.laws";
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 List FailedPatches = new List();
/// Patch classes applied at startup.
public static int Applied { get; private set; }
static BlaxLawsStartup()
{
int ok = PatchAll(FailedPatches);
Applied = ok;
Log.Message("[Blax Laws] " + ok + " patches applied" + (FailedPatches.Count > 0 ? ", " + FailedPatches.Count + " failed" : ""));
BlaxLawsMod.ApplyTabVisibility();
}
/// Applies every patch class; returns how many applied and adds the failed ones to failed.
internal static int PatchAll(List failed)
{
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)
{
failed.Add(type.Name);
Log.Error("[Blax Laws] Patch " + type.Name + " failed: " + e);
}
}
return ok;
}
}
}