1
1
using NeosModLoader ;
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Linq ;
4
5
5
6
namespace NeosModConfigurationExample
6
7
{
7
8
public class NeosModConfigurationExample : NeosMod
8
9
{
9
10
public override string Name => "NeosModConfigurationExample" ;
10
11
public override string Author => "runtime" ;
11
- public override string Version => "1.0 .0" ;
12
+ public override string Version => "1.1 .0" ;
12
13
public override string Link => "https://github.com/zkxs/NeosModConfigurationExample" ;
13
14
15
+ private readonly ModConfigurationKey < bool > KEY_ENABLE = new ModConfigurationKey < bool > ( "enabled" , "Enables the NeosModConfigurationExample mod" , ( ) => true ) ;
14
16
private readonly ModConfigurationKey < int > KEY_COUNT = new ModConfigurationKey < int > ( "count" , "Example counter" , internalAccessOnly : true ) ;
15
17
16
18
public override ModConfigurationDefinition GetConfigurationDefinition ( )
17
19
{
18
20
List < ModConfigurationKey > keys = new List < ModConfigurationKey > ( ) ;
21
+ keys . Add ( KEY_ENABLE ) ;
19
22
keys . Add ( KEY_COUNT ) ;
20
23
return DefineConfiguration ( new Version ( 1 , 0 , 0 ) , keys ) ;
21
24
}
22
25
23
26
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 ( )
24
47
{
25
48
ModConfiguration config = GetConfiguration ( ) ;
26
49
int countValue = default ( int ) ;
@@ -37,5 +60,39 @@ public override void OnEngineInit()
37
60
config . Set ( KEY_COUNT , countValue ) ;
38
61
config . Save ( ) ;
39
62
}
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
+ }
40
97
}
41
98
}
0 commit comments