using System;
using HarmonyLib;
using UnityEngine;
using Verse;
namespace BlaxCastes
{
public class BlaxCastesSettings : ModSettings
{
/// Off: castes never write a pawn's policies. Castes, rows and memberships stay in the save.
public bool enabled = true;
/// Castes set the work priorities they list. Off: work priorities are never written.
public bool manageWorkPriorities = true;
/// With Weapons Tab Reborn, castes set the weapon loadout. Off: loadouts are never written.
public bool manageWeaponLoadout = true;
/// Joining pawns are put into the caste their join row picks.
public bool enableJoinDefaults = true;
/// A message names the caste a joining pawn was put into.
public bool joinMessages = true;
///
/// A join row set to No caste takes a pawn out of a caste a join row put it in, and resets the
/// managed policies it still has from that caste. Only governs that revert.
///
public bool revertOnLeave = true;
/// A pawn that comes back within formerCasteDays with the same status gets its old caste back.
public bool returnToPreviousCaste = true;
/// How long a former-caste record lasts, in days.
public int formerCasteDays = DefaultFormerCasteDays;
/// A new game, or a save without castes, gets the Colonist, Slave and Prisoner castes.
public bool seedStarterCastes = true;
/// The settings page and a message per save name active mods whose join defaults overlap.
public bool warnOverlaps = true;
/// The Caste column in the Assign, Schedule and Work tabs. Read once at startup.
public bool showAssignColumn = true;
public const int DefaultFormerCasteDays = 60;
public const int MinFormerCasteDays = 1;
public const int MaxFormerCasteDays = 120;
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref enabled, "enabled", true);
Scribe_Values.Look(ref manageWorkPriorities, "manageWorkPriorities", true);
Scribe_Values.Look(ref manageWeaponLoadout, "manageWeaponLoadout", true);
Scribe_Values.Look(ref enableJoinDefaults, "enableJoinDefaults", true);
Scribe_Values.Look(ref joinMessages, "joinMessages", true);
Scribe_Values.Look(ref revertOnLeave, "revertOnLeave", true);
Scribe_Values.Look(ref returnToPreviousCaste, "returnToPreviousCaste", true);
Scribe_Values.Look(ref formerCasteDays, "formerCasteDays", DefaultFormerCasteDays);
Scribe_Values.Look(ref seedStarterCastes, "seedStarterCastes", true);
Scribe_Values.Look(ref warnOverlaps, "warnOverlaps", true);
Scribe_Values.Look(ref showAssignColumn, "showAssignColumn", true);
if (Scribe.mode == LoadSaveMode.PostLoadInit)
formerCasteDays = Mathf.Clamp(formerCasteDays, MinFormerCasteDays, MaxFormerCasteDays);
}
public void Reset()
{
var d = new BlaxCastesSettings();
enabled = d.enabled;
manageWorkPriorities = d.manageWorkPriorities;
manageWeaponLoadout = d.manageWeaponLoadout;
enableJoinDefaults = d.enableJoinDefaults;
joinMessages = d.joinMessages;
revertOnLeave = d.revertOnLeave;
returnToPreviousCaste = d.returnToPreviousCaste;
formerCasteDays = d.formerCasteDays;
seedStarterCastes = d.seedStarterCastes;
warnOverlaps = d.warnOverlaps;
showAssignColumn = d.showAssignColumn;
}
}
public class BlaxCastesMod : Mod
{
public static BlaxCastesSettings Settings;
///
/// Raised after the settings window closes and the settings are written. Features that read
/// a setting once (the Assign column) can react here.
///
public static event Action SettingsChanged;
/// The "enabled" setting. True before the settings exist, so nothing is lost while loading.
public static bool Enabled => Settings == null || Settings.enabled;
public BlaxCastesMod(ModContentPack content) : base(content)
{
Settings = GetSettings();
}
public override string SettingsCategory() => "Blax Castes";
public override void WriteSettings()
{
base.WriteSettings();
try
{
SettingsChanged?.Invoke();
}
catch (Exception e)
{
Log.Error("[Blax Castes] Re-applying settings failed: " + e);
}
}
public override void DoSettingsWindowContents(Rect inRect)
{
BlaxCastesSettings s = Settings;
var l = new Listing_Standard();
l.maxOneColumn = true;
l.Begin(inRect);
l.CheckboxLabeled("BlaxCastes_SettingEnabled".Translate(), ref s.enabled, "BlaxCastes_SettingEnabledTip".Translate());
l.CheckboxLabeled("BlaxCastes_SettingWorkPriorities".Translate(), ref s.manageWorkPriorities, "BlaxCastes_SettingWorkPrioritiesTip".Translate());
string workManager = WorkSlot.ActiveManager();
if (workManager != null)
{
using (new TextBlock(ColorLibrary.Gold))
l.Label("BlaxCastes_WorkManagerActive".Translate(workManager));
}
if (WeaponsTab.ModActive)
l.CheckboxLabeled("BlaxCastes_SettingWeaponLoadout".Translate(), ref s.manageWeaponLoadout, "BlaxCastes_SettingWeaponLoadoutTip".Translate());
l.GapLine();
l.CheckboxLabeled("BlaxCastes_SettingJoinDefaults".Translate(), ref s.enableJoinDefaults, DlcSupport.IdeologyKey("BlaxCastes_SettingJoinDefaultsTip").Translate());
l.CheckboxLabeled("BlaxCastes_SettingJoinMessages".Translate(), ref s.joinMessages, "BlaxCastes_SettingJoinMessagesTip".Translate());
l.CheckboxLabeled("BlaxCastes_SettingRevertOnLeave".Translate(), ref s.revertOnLeave, "BlaxCastes_SettingRevertOnLeaveTip".Translate());
l.CheckboxLabeled("BlaxCastes_SettingReturnToPrevious".Translate(), ref s.returnToPreviousCaste, DlcSupport.IdeologyKey("BlaxCastes_SettingReturnToPreviousTip").Translate());
if (s.returnToPreviousCaste)
{
s.formerCasteDays = Mathf.RoundToInt(l.SliderLabeled("BlaxCastes_SettingFormerCasteDays".Translate(s.formerCasteDays),
s.formerCasteDays, BlaxCastesSettings.MinFormerCasteDays, BlaxCastesSettings.MaxFormerCasteDays, 0.6f,
"BlaxCastes_SettingFormerCasteDaysTip".Translate()));
}
l.CheckboxLabeled(DlcSupport.IdeologyKey("BlaxCastes_SettingSeedStarter").Translate(), ref s.seedStarterCastes,
DlcSupport.IdeologyKey("BlaxCastes_SettingSeedStarterTip").Translate());
string seedNote = SeedStarterNote();
if (seedNote != null)
{
using (new TextBlock(GameFont.Tiny, ColoredText.SubtleGrayColor))
l.Label(seedNote);
}
l.GapLine();
l.CheckboxLabeled("BlaxCastes_SettingWarnOverlaps".Translate(), ref s.warnOverlaps, "BlaxCastes_SettingWarnOverlapsTip".Translate());
string overlaps = OverlapWarnings.SettingsText();
if (!overlaps.NullOrEmpty())
{
using (new TextBlock(ColorLibrary.Gold))
l.Label(overlaps);
}
l.GapLine();
l.CheckboxLabeled("BlaxCastes_SettingShowAssignColumn".Translate(), ref s.showAssignColumn, "BlaxCastes_SettingShowAssignColumnTip".Translate());
if (Current.Game != null && GameComponent_BlaxCastes.Instance != null)
{
// The way in while the Caste column is off.
l.Gap(6f);
l.Label("BlaxCastes_SettingOpenWindows".Translate());
if (l.ButtonText("BlaxCastes_ManageCastesButton".Translate()))
Find.WindowStack.Add(new Dialog_ManageCastes());
if (l.ButtonText("BlaxCastes_JoinDefaultsTitle".Translate()))
Find.WindowStack.Add(new Dialog_JoinDefaults());
}
if (ShowsNoIdeologyNote)
{
l.GapLine();
l.Label("BlaxCastes_SettingNoIdeology".Translate());
}
l.Gap();
if (l.ButtonText("BlaxCastes_SettingsReset".Translate()))
s.Reset();
l.End();
}
///
/// Every setting works with any DLC combination, so none is greyed out. Without Ideology the settings
/// page says that slaves need it, and the starter castes setting names the part it leaves out.
///
public static bool ShowsNoIdeologyNote => !ModsConfig.IdeologyActive;
/// The note under the starter castes setting: "Slave caste: needs Ideology" without it, otherwise null.
public static string SeedStarterNote() => ModsConfig.IdeologyActive ? null : (string)"BlaxCastes_SettingSeedStarterSlaveNote".Translate();
}
///
/// 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 BlaxCastesStartup
{
public const string HarmonyId = "blax.castes";
public static readonly Harmony Harmony = new Harmony(HarmonyId);
static BlaxCastesStartup()
{
int ok = 0, failed = 0;
foreach (Type type in typeof(BlaxCastesStartup).Assembly.GetTypes())
{
if (!type.IsDefined(typeof(HarmonyPatch), false))
continue;
try
{
Harmony.CreateClassProcessor(type).Patch();
ok++;
}
catch (Exception e)
{
failed++;
Log.Error("[Blax Castes] Patch " + type.Name + " failed: " + e);
}
}
Log.Message("[Blax Castes] " + ok + " patches applied" + (failed > 0 ? ", " + failed + " failed" : ""));
}
}
}