Skip to content

Commit 81e090c

Browse files
committed
Demo more APIs
1 parent da9458a commit 81e090c

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

NeosModConfigurationExample/NeosModConfigurationExample.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,49 @@
11
using NeosModLoader;
22
using System;
33
using System.Collections.Generic;
4+
using System.Linq;
45

56
namespace NeosModConfigurationExample
67
{
78
public class NeosModConfigurationExample : NeosMod
89
{
910
public override string Name => "NeosModConfigurationExample";
1011
public override string Author => "runtime";
11-
public override string Version => "1.0.0";
12+
public override string Version => "1.1.0";
1213
public override string Link => "https://github.com/zkxs/NeosModConfigurationExample";
1314

15+
private readonly ModConfigurationKey<bool> KEY_ENABLE = new ModConfigurationKey<bool>("enabled", "Enables the NeosModConfigurationExample mod", () => true);
1416
private readonly ModConfigurationKey<int> KEY_COUNT = new ModConfigurationKey<int>("count", "Example counter", internalAccessOnly: true);
1517

1618
public override ModConfigurationDefinition GetConfigurationDefinition()
1719
{
1820
List<ModConfigurationKey> keys = new List<ModConfigurationKey>();
21+
keys.Add(KEY_ENABLE);
1922
keys.Add(KEY_COUNT);
2023
return DefineConfiguration(new Version(1, 0, 0), keys);
2124
}
2225

2326
public override void OnEngineInit()
27+
{
28+
// disable the mod if the enabled config has been set to false
29+
ModConfiguration config = GetConfiguration();
30+
if (!config.GetValue(KEY_ENABLE)) // this is safe as the config has a default value
31+
{
32+
Debug("Mod disabled, returning early.");
33+
return;
34+
}
35+
36+
// register event handler
37+
ModConfiguration.OnAnyConfigurationChanged += OnConfigurationChanged;
38+
39+
// update our counter config
40+
UpdateCount();
41+
42+
// list all configs
43+
EnumerateConfigs();
44+
}
45+
46+
private void UpdateCount()
2447
{
2548
ModConfiguration config = GetConfiguration();
2649
int countValue = default(int);
@@ -37,5 +60,39 @@ public override void OnEngineInit()
3760
config.Set(KEY_COUNT, countValue);
3861
config.Save();
3962
}
63+
64+
private void EnumerateConfigs()
65+
{
66+
List<NeosModBase> mods = new List<NeosModBase>(ModLoader.Mods());
67+
List<NeosModBase> configuredMods = mods
68+
.Where(m => m.GetConfiguration() != null)
69+
.ToList();
70+
Debug($"{mods.Count} mods are loaded, {configuredMods.Count} of them have configurations");
71+
72+
foreach (NeosModBase mod in configuredMods)
73+
{
74+
ModConfiguration config = mod.GetConfiguration();
75+
foreach (ModConfigurationKey key in config.ConfigurationItemDefinitions)
76+
{
77+
if (!key.InternalAccessOnly)
78+
{
79+
80+
if (config.TryGetValue(key, out object value))
81+
{
82+
Msg($"{mod.Name} has configuration \"{key.Name}\" with type \"{key.ValueType()}\" and value \"{value}\"");
83+
}
84+
else
85+
{
86+
Msg($"{mod.Name} has configuration \"{key.Name}\" with type \"{key.ValueType()}\" and no value");
87+
}
88+
}
89+
}
90+
}
91+
}
92+
93+
private void OnConfigurationChanged(ConfigurationChangedEvent @event)
94+
{
95+
Debug($"ConfigurationChangedEvent fired for mod \"{@event.Config.Owner.Name}\" Config \"{@event.Key.Name}\"");
96+
}
4097
}
4198
}

NeosModConfigurationExample/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
// You can specify all the values or you can default the Build and Revision Numbers
3232
// by using the '*' as shown below:
3333
// [assembly: AssemblyVersion("1.0.*")]
34-
[assembly: AssemblyVersion("1.0.0.0")]
35-
[assembly: AssemblyFileVersion("1.0.0.0")]
34+
[assembly: AssemblyVersion("1.1.0.0")]
35+
[assembly: AssemblyFileVersion("1.1.0.0")]

0 commit comments

Comments
 (0)