Skip to content

Commit 256548e

Browse files
authored
Merge pull request #26 from DekuDesu/development
refactored config code to comply with dependency injection model
2 parents 2bac2fc + 78b45a3 commit 256548e

9 files changed

Lines changed: 178 additions & 44 deletions

File tree

MiniMapLibrary/Interactible/InteractibleSetting.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using MiniMapLibrary.Interfaces;
2+
using MiniMapMod.wrappers;
3+
using System;
24
using System.Collections.Generic;
35
using System.Text;
46
using UnityEngine;
@@ -10,5 +12,7 @@ public class InteractibleSetting
1012
public Dimension2D Dimensions { get; set; }
1113
public Color ActiveColor { get; set; }
1214
public Color InactiveColor { get; set; }
15+
public string Description { get; set; }
16+
public ISettingConfigGroup Config;
1317
}
1418
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MiniMapLibrary.Interfaces
6+
{
7+
public interface IConfig
8+
{
9+
IConfigEntry<T> Bind<T>(string section, string key, T defaultValue, string description);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MiniMapLibrary.Interfaces
6+
{
7+
public interface IConfigEntry<T>
8+
{
9+
T Value { get; }
10+
}
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using MiniMapLibrary.Interfaces;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace MiniMapMod.wrappers
8+
{
9+
public interface ISettingConfigGroup
10+
{
11+
IConfigEntry<bool> Enabled { get; }
12+
IConfigEntry<float> Height { get; }
13+
IConfigEntry<float> Width { get; }
14+
IConfigEntry<Color> ActiveColor { get; }
15+
IConfigEntry<Color> InactiveColor { get; }
16+
}
17+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using MiniMapLibrary.Interfaces;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace MiniMapMod.wrappers
8+
{
9+
public class SettingConfigGroup : ISettingConfigGroup
10+
{
11+
public SettingConfigGroup(IConfigEntry<bool> enabled, IConfigEntry<float> height, IConfigEntry<float> width, IConfigEntry<Color> activeColor, IConfigEntry<Color> inactiveColor)
12+
{
13+
Enabled = enabled;
14+
Height = height;
15+
Width = width;
16+
ActiveColor = activeColor;
17+
InactiveColor = inactiveColor;
18+
}
19+
20+
public IConfigEntry<bool> Enabled { get; }
21+
public IConfigEntry<float> Height { get; }
22+
public IConfigEntry<float> Width { get; }
23+
public IConfigEntry<Color> ActiveColor { get; }
24+
public IConfigEntry<Color> InactiveColor { get; }
25+
}
26+
}

MiniMapLibrary/Settings.cs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using MiniMapLibrary.Interfaces;
2+
using MiniMapMod.wrappers;
3+
using System;
24
using System.Collections.Generic;
35
using System.Linq;
46
using System.Text;
@@ -30,7 +32,7 @@ static Settings()
3032

3133
private static void InitializeDefaultSettings()
3234
{
33-
static void AddSize(InteractableKind type, float width = -1, float height = -1, Color ActiveColor = default, Color InactiveColor = default)
35+
static void AddSize(InteractableKind type, float width = -1, float height = -1, Color ActiveColor = default, Color InactiveColor = default, string description = "")
3436
{
3537
ActiveColor = ActiveColor == default ? DefaultActiveColor : ActiveColor;
3638
InactiveColor = InactiveColor == default ? DefaultInactiveColor : InactiveColor;
@@ -49,20 +51,22 @@ static void AddSize(InteractableKind type, float width = -1, float height = -1,
4951
Dimensions = size
5052
};
5153

54+
setting.Description = description;
55+
5256
InteractibleSettings.Add(type, setting);
5357
}
5458

55-
AddSize(InteractableKind.Chest, 10, 8);
56-
AddSize(InteractableKind.Shrine);
57-
AddSize(InteractableKind.Teleporter, 15, 15, ActiveColor: Color.white, InactiveColor: Color.green);
58-
AddSize(InteractableKind.Player, 8, 8, ActiveColor: PlayerIconColor, InactiveColor: PlayerIconColor);
59-
AddSize(InteractableKind.Barrel, 5, 5);
60-
AddSize(InteractableKind.Drone, 7, 7);
61-
AddSize(InteractableKind.Special, 7, 7);
62-
AddSize(InteractableKind.Enemy, 3, 3, ActiveColor: Color.red);
63-
AddSize(InteractableKind.Utility);
64-
AddSize(InteractableKind.Printer, 10, 8);
65-
AddSize(InteractableKind.LunarPod, 7, 7);
59+
AddSize(InteractableKind.Chest, 10, 8, description: "Chests, including shops");
60+
AddSize(InteractableKind.Shrine, description: "All shrines (excluding Newt)");
61+
AddSize(InteractableKind.Teleporter, 15, 15, ActiveColor: Color.white, InactiveColor: Color.green, description: "Boss teleporters");
62+
AddSize(InteractableKind.Player, 8, 8, ActiveColor: PlayerIconColor, InactiveColor: PlayerIconColor, description: "");
63+
AddSize(InteractableKind.Barrel, 5, 5, description: "Barrels");
64+
AddSize(InteractableKind.Drone, 7, 7, description: "Drones");
65+
AddSize(InteractableKind.Special, 7, 7, description: "Special interactibles such as the landing pod");
66+
AddSize(InteractableKind.Enemy, 3, 3, ActiveColor: Color.red, description: "Enemies");
67+
AddSize(InteractableKind.Utility, description: "Scrappers");
68+
AddSize(InteractableKind.Printer, 10, 8, description: "Printers");
69+
AddSize(InteractableKind.LunarPod, 7, 7, description: "Lunar pods (chests)");
6670
}
6771

6872
public static InteractibleSetting GetSetting(InteractableKind type)
@@ -102,6 +106,25 @@ public static Dimension2D GetInteractableSize(InteractableKind type)
102106
return DefaultUIElementSize;
103107
}
104108

109+
public static void LoadConfigEntries(InteractableKind type, IConfig config)
110+
{
111+
InteractibleSetting setting = InteractibleSettings[type];
112+
113+
IConfigEntry<bool> enabled = config.Bind<bool>($"Icon.{type}", "enabled", true, $"Whether or not {setting.Description} should be shown on the minimap");
114+
IConfigEntry<Color> activeColor = config.Bind<Color>($"Icon.{type}", "activeColor", setting.ActiveColor, "The color the icon should be when it has not been interacted with");
115+
IConfigEntry<Color> inactiveColor = config.Bind<Color>($"Icon.{type}", "inactiveColor", setting.InactiveColor, "The color the icon should be when it has used/bought");
116+
IConfigEntry<float> width = config.Bind<float>($"Icon.{type}", "width", setting.Dimensions.Width, "Width of the icon");
117+
IConfigEntry<float> height = config.Bind<float>($"Icon.{type}", "height", setting.Dimensions.Height, "Width of the icon");
118+
119+
120+
InteractibleSettings[type].Config = new SettingConfigGroup(enabled, height, width, activeColor, inactiveColor);
121+
122+
setting.ActiveColor = activeColor.Value;
123+
setting.InactiveColor = inactiveColor.Value;
124+
setting.Dimensions.Height = height.Value;
125+
setting.Dimensions.Width = width.Value;
126+
}
127+
105128
public static void UpdateSetting(InteractableKind type, float width, float height, Color active, Color inactive)
106129
{
107130
if (InteractibleSettings.ContainsKey(type))
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using MiniMapLibrary.Interfaces;
5+
using BepInEx.Configuration;
6+
using BepInEx;
7+
8+
namespace MiniMapMod.Adapters
9+
{
10+
public class ConfigAdapter : IConfig
11+
{
12+
private readonly ConfigFile plugin;
13+
14+
public ConfigAdapter(ConfigFile plugin)
15+
{
16+
this.plugin = plugin;
17+
}
18+
19+
public IConfigEntry<T> Bind<T>(string section, string key, T defaultValue, string description) => new ConfigEntryAdapter<T>(plugin.Bind<T>(section, key, defaultValue, description));
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using BepInEx.Configuration;
5+
using MiniMapLibrary.Interfaces;
6+
7+
namespace MiniMapMod.Adapters
8+
{
9+
public class ConfigEntryAdapter<T> : IConfigEntry<T>
10+
{
11+
private readonly ConfigEntry<T> entry;
12+
13+
public ConfigEntryAdapter(ConfigEntry<T> entry)
14+
{
15+
this.entry = entry;
16+
}
17+
18+
public T Value => entry.Value;
19+
}
20+
}

MiniMapMod/MiniMapPlugin.cs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.Linq;
77
using System;
88
using BepInEx.Configuration;
9+
using MiniMapMod.Adapters;
10+
using MiniMapLibrary.Interfaces;
911

1012
namespace MiniMapMod
1113
{
@@ -24,41 +26,23 @@ public class MiniMapPlugin : BaseUnityPlugin
2426

2527
private bool ScannedStaticObjects = false;
2628

27-
private readonly Dictionary<InteractableKind, ConfigEntry<bool>> ScanOptions = new();
28-
29-
private readonly Dictionary<InteractableKind, string> InteractibleKindDescriptions = new();
29+
private IConfig config;
3030

3131
public void Awake()
3232
{
3333
Log.Init(Logger);
3434

35-
InteractibleKindDescriptions.Add(InteractableKind.Chest, "Chests, including shops");
36-
InteractibleKindDescriptions.Add(InteractableKind.Utility, "Scrappers");
37-
InteractibleKindDescriptions.Add(InteractableKind.Teleporter, "Boss teleporter");
38-
InteractibleKindDescriptions.Add(InteractableKind.Shrine, "All shrines (excluding Newt)");
39-
InteractibleKindDescriptions.Add(InteractableKind.Special, "Special interactibles such as the landing pod");
40-
InteractibleKindDescriptions.Add(InteractableKind.Player, "Players");
41-
InteractibleKindDescriptions.Add(InteractableKind.Drone, "Drones");
42-
InteractibleKindDescriptions.Add(InteractableKind.Barrel, "Barrels");
43-
InteractibleKindDescriptions.Add(InteractableKind.Enemy, "Enemies");
44-
InteractibleKindDescriptions.Add(InteractableKind.Printer, "Printers");
45-
InteractibleKindDescriptions.Add(InteractableKind.LunarPod, "Lunar pods (chests)");
35+
config = new ConfigAdapter(this.Config);
4636

4737
// bind options
4838
InteractableKind[] kinds = Enum.GetValues(typeof(InteractableKind)).Cast<InteractableKind>().Where(x=>x != InteractableKind.none && x != InteractableKind.All).ToArray();
4939

50-
for (int i = 0; i < kinds.Length; i++)
40+
foreach (var item in kinds)
5141
{
52-
InteractableKind kind = kinds[i];
53-
54-
ScanOptions.Add(kind, Config.Bind<bool>($"Icon.{kind}", "enabled", true, $"Whether or {InteractibleKindDescriptions[kind]} should be shown on the minimap."));
55-
56-
ConfigEntry<Color> activeColor = Config.Bind<Color>($"Icon.{kind}", "activeColor", Settings.GetColor(kind, true), "The color the icon should be when it has not been interacted with");
57-
ConfigEntry<Color> inactiveColor = Config.Bind<Color>($"Icon.{kind}", "inactiveColor", Settings.GetColor(kind, false), "The color the icon should be when it has used/bought");
58-
ConfigEntry<float> iconWidth = Config.Bind<float>($"Icon.{kind}", "width", Settings.GetInteractableSize(kind).Width, "Width of the icon");
59-
ConfigEntry<float> iconHeight = Config.Bind<float>($"Icon.{kind}", "height", Settings.GetInteractableSize(kind).Height, "Width of the icon");
60-
61-
Settings.UpdateSetting(kind, iconWidth.Value, iconHeight.Value, activeColor.Value, inactiveColor.Value);
42+
Settings.LoadConfigEntries(item, config);
43+
44+
InteractibleSetting setting = Settings.GetSetting(item);
45+
Log.LogInfo($"MINIMAP: Loaded {item} config [{(setting.Config.Enabled.Value ? "enabled" : "disabled")}, {setting.ActiveColor}, {setting.InactiveColor}, ({setting.Dimensions.Width}x{setting.Dimensions.Height})]");
6246
}
6347

6448
Log.LogInfo("MINIMAP: Creating scene scan hooks");
@@ -182,6 +166,8 @@ private void Reset()
182166
TrackedObjects.Clear();
183167
TrackedDimensions.Clear();
184168
Minimap.Destroy();
169+
170+
// mark the scene as scannable again so we scan for chests etc..
185171
ScannedStaticObjects = false;
186172
}
187173

@@ -196,17 +182,24 @@ private void ScanScene()
196182

197183
private void ScanStaticTypes()
198184
{
185+
// if we have alreadys scanned don't scan again until we die or the scene changes (this method has sever performance implications)
199186
if (ScannedStaticObjects)
200187
{
201188
return;
202189
}
203190

204-
RegisterMonobehaviorType<ChestBehavior>(InteractableKind.Chest, dynamicObject: false, selector: chest => chest.GetComponent<PurchaseInteraction>().contextToken != "LUNAR_CHEST_CONTEXT");
191+
// NON lunar pods
192+
RegisterMonobehaviorType<ChestBehavior>(InteractableKind.Chest, dynamicObject: false,
193+
selector: chest => chest.GetComponent<PurchaseInteraction>().contextToken != "LUNAR_CHEST_CONTEXT");
205194

206-
RegisterMonobehaviorType<ChestBehavior>(InteractableKind.LunarPod, dynamicObject: false, selector: chest => chest.GetComponent<PurchaseInteraction>().contextToken == "LUNAR_CHEST_CONTEXT");
195+
// lunar pods
196+
RegisterMonobehaviorType<ChestBehavior>(InteractableKind.LunarPod, dynamicObject: false,
197+
selector: chest => chest.GetComponent<PurchaseInteraction>().contextToken == "LUNAR_CHEST_CONTEXT");
207198

199+
// adapative chests
208200
RegisterMonobehaviorType<RouletteChestController>(InteractableKind.Chest, dynamicObject: false);
209201

202+
// shrines
210203
RegisterMonobehaviorType<ShrineBloodBehavior>(InteractableKind.Shrine, dynamicObject: false);
211204

212205
RegisterMonobehaviorType<ShrineChanceBehavior>(InteractableKind.Shrine, dynamicObject: false);
@@ -220,21 +213,29 @@ private void ScanStaticTypes()
220213
RegisterMonobehaviorType<ShrineRestackBehavior>(InteractableKind.Shrine, dynamicObject: false);
221214

222215
// normal shops
223-
RegisterMonobehaviorType<ShopTerminalBehavior>(InteractableKind.Chest, dynamicObject: false, selector: shop => shop.GetComponent<PurchaseInteraction>().contextToken != "DUPLICATOR_CONTEXT");
216+
RegisterMonobehaviorType<ShopTerminalBehavior>(InteractableKind.Chest, dynamicObject: false,
217+
selector: shop => shop.GetComponent<PurchaseInteraction>().contextToken != "DUPLICATOR_CONTEXT");
224218

225219
// duplicators
226-
RegisterMonobehaviorType<ShopTerminalBehavior>(InteractableKind.Printer, dynamicObject: false, selector: shop => shop.GetComponent<PurchaseInteraction>().contextToken == "DUPLICATOR_CONTEXT");
220+
RegisterMonobehaviorType<ShopTerminalBehavior>(InteractableKind.Printer, dynamicObject: false,
221+
selector: shop => shop.GetComponent<PurchaseInteraction>().contextToken == "DUPLICATOR_CONTEXT");
227222

223+
// barrels
228224
RegisterMonobehaviorType<BarrelInteraction>(InteractableKind.Barrel, barrel => !barrel.Networkopened, dynamicObject: false);
229225

226+
// scrapper
230227
RegisterMonobehaviorType<ScrapperController>(InteractableKind.Utility, dynamicObject: false);
231228

229+
// random stuff like the exploding backpack door on the landing pod
232230
RegisterMonobehaviorType<GenericInteraction>(InteractableKind.Special, dynamicObject: false);
233231

232+
// boss teleporter
234233
RegisterMonobehaviorType<TeleporterInteraction>(InteractableKind.Teleporter, (teleporter) => teleporter.activationState != TeleporterInteraction.ActivationState.Charged, dynamicObject: false);
235234

235+
// drones
236236
RegisterMonobehaviorType<SummonMasterBehavior>(InteractableKind.Drone, dynamicObject: false);
237237

238+
// make sure we only do this once per scene
238239
ScannedStaticObjects = true;
239240
}
240241

@@ -265,7 +266,7 @@ private void ClearDynamicTrackedObjects()
265266
private void RegisterMonobehaviorType<T>(InteractableKind kind, Func<T, bool> ActiveChecker = null, bool dynamicObject = true, Func<T, bool> selector = null) where T : MonoBehaviour
266267
{
267268
// check to see if it's enabled in the config
268-
if (ScanOptions[kind].Value == false)
269+
if (Settings.GetSetting(kind).Config.Enabled.Value == false)
269270
{
270271
return;
271272
}

0 commit comments

Comments
 (0)