rimworld-blax-castes/Source/BlaxCastes/BlaxCastesMod.cs

146 lines
5.7 KiB
C#

using System;
using HarmonyLib;
using UnityEngine;
using Verse;
namespace BlaxCastes
{
public class BlaxCastesSettings : ModSettings
{
/// <summary>Off: castes never write a pawn's policies. Castes, rows and memberships stay in the save.</summary>
public bool enabled = true;
/// <summary>Joining pawns are put into the caste their join row picks.</summary>
public bool enableJoinDefaults = true;
/// <summary>A message names the caste a joining pawn was put into.</summary>
public bool joinMessages = true;
/// <summary>A join row set to No caste takes a pawn out of its caste and resets the managed policies.</summary>
public bool revertOnLeave = true;
/// <summary>The Caste column in the Assign tab. Read once at startup.</summary>
public bool showAssignColumn = true;
public override void ExposeData()
{
base.ExposeData();
Scribe_Values.Look(ref enabled, "enabled", 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 showAssignColumn, "showAssignColumn", true);
}
public void Reset()
{
var d = new BlaxCastesSettings();
enabled = d.enabled;
enableJoinDefaults = d.enableJoinDefaults;
joinMessages = d.joinMessages;
revertOnLeave = d.revertOnLeave;
showAssignColumn = d.showAssignColumn;
}
}
public class BlaxCastesMod : Mod
{
public static BlaxCastesSettings Settings;
/// <summary>
/// Raised after the settings window closes and the settings are written. Features that read
/// a setting once (the Assign column) can react here.
/// </summary>
public static event Action SettingsChanged;
/// <summary>The "enabled" setting. True before the settings exist, so nothing is lost while loading.</summary>
public static bool Enabled => Settings == null || Settings.enabled;
public BlaxCastesMod(ModContentPack content) : base(content)
{
Settings = GetSettings<BlaxCastesSettings>();
}
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.GapLine();
l.CheckboxLabeled("BlaxCastes_SettingJoinDefaults".Translate(), ref s.enableJoinDefaults, "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.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 (!ModsConfig.IdeologyActive)
{
l.GapLine();
l.Label("BlaxCastes_SettingNoIdeology".Translate());
}
l.Gap();
if (l.ButtonText("BlaxCastes_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 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" : ""));
}
}
}