diff --git a/Defs/Actions.xml b/Defs/Actions.xml index f7dcfb7..b1d245d 100644 --- a/Defs/Actions.xml +++ b/Defs/Actions.xml @@ -5,28 +5,28 @@ LevelUpActionDef_Sound Plays a sound. - LevelUp.SoundAction + LevelUp.LevelingAction_Sound LevelUpActionDef_Animation Displays a simple, possibly animated, graphic on the colonist. - LevelUp.AnimationAction + LevelUp.LevelingAction_Animation LevelUpActionDef_Message Displays a text message in the top-left corner of the screen. - LevelUp.MessageAction + LevelUp.LevelingAction_Message LevelUpActionDef_OverheadMessage Displays a text message on the colonist. - LevelUp.OverheadMessageAction + LevelUp.LevelingAction_OverheadMessage diff --git a/LevelUp.csproj b/LevelUp.csproj index 37ca3b3..ed1e09a 100644 --- a/LevelUp.csproj +++ b/LevelUp.csproj @@ -20,7 +20,7 @@ - + diff --git a/Source/ActionMaker.cs b/Source/ActionMaker.cs index 721df76..142bb19 100644 --- a/Source/ActionMaker.cs +++ b/Source/ActionMaker.cs @@ -10,24 +10,6 @@ public sealed class ActionMaker : IExposable { internal List actions = []; private Vector2 scrollPosition; - private readonly List preparedActions = []; - - internal void Prepare() - { - preparedActions.Clear(); - - foreach (LevelingAction action in actions) - { - if (!action.active) - { - continue; - } - - preparedActions.Add(action); - - action.Prepare(); - } - } internal void ExecuteActions(LevelingInfo levelingInfo) { @@ -36,9 +18,12 @@ internal void ExecuteActions(LevelingInfo levelingInfo) return; } - for (int i = 0; i < preparedActions.Count; i++) + foreach (LevelingAction action in actions) { - preparedActions[i].Execute(levelingInfo); + if (action.active) + { + action.Execute(levelingInfo); + } } } diff --git a/Source/DefOfs.cs b/Source/DefOfs.cs index e6487c8..4ea433b 100644 --- a/Source/DefOfs.cs +++ b/Source/DefOfs.cs @@ -1,8 +1,9 @@ -using RimWorld; +using RimWorld; using Verse; namespace LevelUp; +#pragma warning disable IDE1006 // Naming Styles [DefOf] internal static class DefOfs { diff --git a/Source/LevelUpMod.cs b/Source/LevelUpMod.cs index f7b0bb6..8c03fb5 100644 --- a/Source/LevelUpMod.cs +++ b/Source/LevelUpMod.cs @@ -10,7 +10,12 @@ public LevelUpMod(ModContentPack content) : base(content) { Harmony harmony = new(content.PackageId); Patcher.ApplyPatches(harmony); - LongEventHandler.ExecuteWhenFinished(() => LoadedModManager.GetMod().GetSettings()); + LongEventHandler.ExecuteWhenFinished(static () => + { + // Retrieve settings to initialize values, so they're in place. + // We do this in LongEvent because defs aren't loaded in Mod constructor. + LoadedModManager.GetMod().GetSettings(); + }); } public override string SettingsCategory() diff --git a/Source/Actions/LevelingAction.cs b/Source/LevelingAction.cs similarity index 90% rename from Source/Actions/LevelingAction.cs rename to Source/LevelingAction.cs index 55d12f9..a6f6b46 100644 --- a/Source/Actions/LevelingAction.cs +++ b/Source/LevelingAction.cs @@ -8,9 +8,6 @@ public abstract class LevelingAction : IExposable internal ActionDef actionDef = null!; internal bool active; - internal virtual void Prepare() - { } - internal abstract void Execute(LevelingInfo levelingInfo); internal abstract void Draw(Rect rect); diff --git a/Source/Actions/AnimationAction.cs b/Source/LevelingAction_Animation.cs similarity index 85% rename from Source/Actions/AnimationAction.cs rename to Source/LevelingAction_Animation.cs index 6d1e7e0..4c26286 100644 --- a/Source/Actions/AnimationAction.cs +++ b/Source/LevelingAction_Animation.cs @@ -6,9 +6,9 @@ namespace LevelUp; -public sealed class AnimationAction : LevelingAction +public sealed class LevelingAction_Animation : LevelingAction { - private FleckDef fleckDef; + private FleckDef fleckDef = null!; private Graphic_Single graphic = null!; private Texture2D texture = null!; private FleckDefExtension defExtension = null!; @@ -19,14 +19,15 @@ internal FleckDef FleckDef set { fleckDef = value; - Prepare(); + defExtension = fleckDef.GetModExtension(); + graphic = (Graphic_Single)fleckDef.graphicData.Graphic; + texture = ContentFinder.Get(fleckDef.graphicData.texPath); } } - public AnimationAction() + public LevelingAction_Animation() { - fleckDef = DefOfs.Radiance; - Prepare(); + FleckDef = DefOfs.Radiance; } internal override void Execute(LevelingInfo levelingInfo) @@ -55,13 +56,6 @@ internal override void Execute(LevelingInfo levelingInfo) map.flecks.CreateFleck(fleckData); } - internal override void Prepare() - { - defExtension = fleckDef.GetModExtension(); - graphic = (Graphic_Single)fleckDef.graphicData.Graphic; - texture = ContentFinder.Get(fleckDef.graphicData.texPath); - } - internal override void Draw(Rect rect) { Rect rowRect = new(rect) { height = 24f }; diff --git a/Source/Actions/MessageAction.cs b/Source/LevelingAction_Message.cs similarity index 93% rename from Source/Actions/MessageAction.cs rename to Source/LevelingAction_Message.cs index 2cee33c..3dde618 100644 --- a/Source/Actions/MessageAction.cs +++ b/Source/LevelingAction_Message.cs @@ -4,7 +4,7 @@ namespace LevelUp; -public sealed class MessageAction : TextAction +public sealed class LevelingAction_Message : LevelingAction_Text { private bool historical; diff --git a/Source/Actions/OverheadMessageAction.cs b/Source/LevelingAction_OverheadMessage.cs similarity index 93% rename from Source/Actions/OverheadMessageAction.cs rename to Source/LevelingAction_OverheadMessage.cs index 0dba954..ed6b3cb 100644 --- a/Source/Actions/OverheadMessageAction.cs +++ b/Source/LevelingAction_OverheadMessage.cs @@ -4,7 +4,7 @@ namespace LevelUp; -public sealed class OverheadMessageAction : TextAction +public sealed class LevelingAction_OverheadMessage : LevelingAction_Text { private bool historical; diff --git a/Source/Actions/SoundAction.cs b/Source/LevelingAction_Sound.cs similarity index 96% rename from Source/Actions/SoundAction.cs rename to Source/LevelingAction_Sound.cs index 7c603e7..c2bf9a2 100644 --- a/Source/Actions/SoundAction.cs +++ b/Source/LevelingAction_Sound.cs @@ -6,7 +6,7 @@ namespace LevelUp; -public sealed class SoundAction : LevelingAction +public sealed class LevelingAction_Sound : LevelingAction { private const float MinVolume = 0f; private const float MaxVolume = 1.5f; diff --git a/Source/Actions/TextAction.cs b/Source/LevelingAction_Text.cs similarity index 98% rename from Source/Actions/TextAction.cs rename to Source/LevelingAction_Text.cs index 052fd7b..8b4740c 100644 --- a/Source/Actions/TextAction.cs +++ b/Source/LevelingAction_Text.cs @@ -6,7 +6,7 @@ namespace LevelUp; -public abstract class TextAction : LevelingAction +public abstract class LevelingAction_Text : LevelingAction { private static readonly StringBuilder stringBuilder = new(); internal string text = string.Empty; diff --git a/Source/Profile.cs b/Source/Profile.cs index cdbdbff..6d34c18 100644 --- a/Source/Profile.cs +++ b/Source/Profile.cs @@ -9,12 +9,6 @@ public sealed class Profile : IExposable internal ActionMaker levelDownActionMaker = new(); private LevelingAction? selected; - internal void Prepare() - { - levelUpActionMaker.Prepare(); - levelDownActionMaker.Prepare(); - } - internal void Draw(Rect rect) { Rect leftRect = new(rect) { width = rect.width / 3 }; diff --git a/Source/ProfileInitializer.cs b/Source/ProfileInitializer.cs deleted file mode 100644 index f51882e..0000000 --- a/Source/ProfileInitializer.cs +++ /dev/null @@ -1,79 +0,0 @@ -using System; -using Verse; - -namespace LevelUp; - -internal static class ProfileInitializer -{ - internal static void InitializeProfile(Profile profile) - { - // Level up - foreach (ActionDef actionDef in DefDatabase.AllDefs) - { - if (!profile.levelUpActionMaker.actions.Any(x => x.actionDef == actionDef)) - { - if (Activator.CreateInstance(actionDef.actionClass) is not LevelingAction action) - { - throw new InvalidOperationException("action is null"); - } - action.actionDef = actionDef; - profile.levelUpActionMaker.actions.Add(action); - - if (action is SoundAction soundAction) - { - soundAction.active = true; - soundAction.SoundDef = DefOfs.Ding; - } - else if (action is MessageAction messageAction) - { - messageAction.active = true; - messageAction.text = I18n.DefaultLevelUpMessage; - } - else if (action is OverheadMessageAction overheadMessageAction) - { - overheadMessageAction.text = I18n.DefaultLevelUpOverheadMessage; - } - else if (action is AnimationAction animationAction) - { - animationAction.active = true; - animationAction.FleckDef = DefOfs.Radiance; - } - } - } - - profile.levelUpActionMaker.actions.SortBy(x => x.actionDef.LabelCap.RawText); - - // Level down - foreach (ActionDef actionDef in DefDatabase.AllDefs) - { - if (!profile.levelDownActionMaker.actions.Any(x => x.actionDef == actionDef)) - { - if (Activator.CreateInstance(actionDef.actionClass) is not LevelingAction action) - { - throw new InvalidOperationException("action is null"); - } - action.actionDef = actionDef; - profile.levelDownActionMaker.actions.Add(action); - - if (action is SoundAction soundAction) - { - soundAction.SoundDef = DefOfs.Negative; - } - else if (action is MessageAction messageAction) - { - messageAction.text = I18n.DefaultLevelDownMessage; - } - else if (action is OverheadMessageAction overheadMessageAction) - { - overheadMessageAction.text = I18n.DefaultLevelDownOverheadMessage; - } - else if (action is AnimationAction animationAction) - { - animationAction.FleckDef = DefOfs.Drain; - } - } - } - - profile.levelDownActionMaker.actions.SortBy(x => x.actionDef.LabelCap.RawText); - } -} diff --git a/Source/Settings.cs b/Source/Settings.cs index dd05d6d..7c3c467 100644 --- a/Source/Settings.cs +++ b/Source/Settings.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using Verse; @@ -5,33 +6,115 @@ namespace LevelUp; public sealed class Settings : ModSettings { - internal static Dictionary timerCache = []; + internal static readonly Dictionary timerCache = []; internal static Profile profile = new(); + private int settingsVersion; public Settings() { if (Scribe.mode == LoadSaveMode.Inactive) { - EnsureInitialized(); + InitializeProfile(profile); } } public override void ExposeData() { - Scribe_Deep.Look(ref profile, "profile"); - if (Scribe.mode == LoadSaveMode.LoadingVars) + if (Scribe.mode == LoadSaveMode.Saving) { - EnsureInitialized(); + settingsVersion = 1; } - else if (Scribe.mode == LoadSaveMode.Saving) + + Scribe_Values.Look(ref settingsVersion, "settingsVersion", forceSave: true); + if (Scribe.mode == LoadSaveMode.LoadingVars) { - profile.Prepare(); + if (settingsVersion == 0) + { + Log.Warning("[Level Up!] Detected incompatible mod settings. Resetting to default settings."); + profile = new(); + } + else + { + Scribe_Deep.Look(ref profile, "profile"); + } + + InitializeProfile(profile); + return; } + + + Scribe_Deep.Look(ref profile, "profile"); } - private static void EnsureInitialized() + private static void InitializeProfile(Profile profile) { - ProfileInitializer.InitializeProfile(profile); - profile.Prepare(); + // Level up + foreach (ActionDef actionDef in DefDatabase.AllDefs) + { + if (!profile.levelUpActionMaker.actions.Any(x => x.actionDef == actionDef)) + { + if (Activator.CreateInstance(actionDef.actionClass) is not LevelingAction action) + { + throw new InvalidOperationException("action is null"); + } + action.actionDef = actionDef; + profile.levelUpActionMaker.actions.Add(action); + + if (action is LevelingAction_Sound soundAction) + { + soundAction.active = true; + soundAction.SoundDef = DefOfs.Ding; + } + else if (action is LevelingAction_Message messageAction) + { + messageAction.active = true; + messageAction.text = I18n.DefaultLevelUpMessage; + } + else if (action is LevelingAction_OverheadMessage overheadMessageAction) + { + overheadMessageAction.text = I18n.DefaultLevelUpOverheadMessage; + } + else if (action is LevelingAction_Animation animationAction) + { + animationAction.active = true; + animationAction.FleckDef = DefOfs.Radiance; + } + } + } + + profile.levelUpActionMaker.actions.SortBy(x => x.actionDef.LabelCap.RawText); + + // Level down + foreach (ActionDef actionDef in DefDatabase.AllDefs) + { + if (!profile.levelDownActionMaker.actions.Any(x => x.actionDef == actionDef)) + { + if (Activator.CreateInstance(actionDef.actionClass) is not LevelingAction action) + { + throw new InvalidOperationException("action is null"); + } + action.actionDef = actionDef; + profile.levelDownActionMaker.actions.Add(action); + + if (action is LevelingAction_Sound soundAction) + { + soundAction.SoundDef = DefOfs.Negative; + } + else if (action is LevelingAction_Message messageAction) + { + messageAction.text = I18n.DefaultLevelDownMessage; + } + else if (action is LevelingAction_OverheadMessage overheadMessageAction) + { + overheadMessageAction.text = I18n.DefaultLevelDownOverheadMessage; + } + else if (action is LevelingAction_Animation animationAction) + { + animationAction.FleckDef = DefOfs.Drain; + } + } + } + + profile.levelDownActionMaker.actions.SortBy(x => x.actionDef.LabelCap.RawText); } }