1
- using System ;
1
+ using Newtonsoft . Json ;
2
+ using System ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
4
5
using System . Text ;
5
6
using System . Threading . Tasks ;
7
+ using ToolkitCore . Database ;
6
8
using ToolkitCore . Models ;
7
9
using ToolkitCore . Utilities ;
8
10
using Verse ;
9
11
10
12
namespace ToolkitCore . Controllers
11
13
{
14
+ [ StaticConstructorOnStartup ]
12
15
public static class ChatCommandController
13
16
{
14
- public static ToolkitChatCommand GetChatCommand ( string commandText )
17
+ static List < ToolkitChatCommandWrapper > toolkitChatCommandWrappers = new List < ToolkitChatCommandWrapper > ( ) ;
18
+ static ChatCommandController ( )
19
+ {
20
+ LoadCommandMethodSettings ( ) ;
21
+ LoadNewCommandMethods ( ) ;
22
+ }
23
+ public static ToolkitChatCommandWrapper GetChatCommand ( string commandText )
15
24
{
16
25
string baseCommand = CommandFilter . Parse ( commandText ) . FirstOrDefault ( ) ;
17
26
@@ -20,9 +29,101 @@ public static ToolkitChatCommand GetChatCommand(string commandText)
20
29
return null ;
21
30
}
22
31
32
+ return toolkitChatCommandWrappers . FirstOrDefault (
33
+ c => c . CommandText . EqualsIgnoreCase ( baseCommand )
34
+ ) ;
35
+ }
36
+
37
+ public static List < ToolkitChatCommandWrapper > GetAllChatCommands ( )
38
+ {
39
+ return toolkitChatCommandWrappers ;
40
+ }
41
+
42
+ public static ToolkitChatCommand GetChatCommandDef ( string commandText )
43
+ {
23
44
return DefDatabase < ToolkitChatCommand > . AllDefsListForReading . FirstOrDefault (
24
- c => c . commandText . EqualsIgnoreCase ( baseCommand )
45
+ c => c . commandText . EqualsIgnoreCase ( commandText )
25
46
) ;
26
- }
47
+ }
48
+
49
+ static void LoadCommandMethodSettings ( )
50
+ {
51
+ DatabaseController . LoadObject (
52
+ "CommandMethods" ,
53
+ LoadedModManager . GetMod < ToolkitCore > ( ) ,
54
+ out CommandMethodWrapper loadedCommands
55
+ ) ;
56
+ if ( loadedCommands == null ) return ;
57
+ foreach ( ToolkitChatCommandWrapper loadedCommand in loadedCommands . Commands )
58
+ {
59
+ ToolkitChatCommand def = DefDatabase < ToolkitChatCommand > . AllDefsListForReading . FirstOrDefault (
60
+ c => c . defName . EqualsIgnoreCase ( loadedCommand . DefName ) ) ;
61
+ if ( def == null )
62
+ {
63
+ continue ;
64
+ }
65
+ loadedCommand . DefName = def . defName ;
66
+ loadedCommand . Def = def ;
67
+ toolkitChatCommandWrappers . Add ( loadedCommand ) ;
68
+ }
69
+ }
70
+
71
+ static void LoadNewCommandMethods ( )
72
+ {
73
+ foreach ( ToolkitChatCommand toolkitChatCommand in DefDatabase < ToolkitChatCommand > . AllDefs )
74
+ {
75
+ ToolkitChatCommandWrapper wrapper =
76
+ toolkitChatCommandWrappers . Find ( ( c ) => c . DefName == toolkitChatCommand . defName ) ;
77
+ if ( wrapper == null )
78
+ {
79
+ toolkitChatCommandWrappers . Add ( new ToolkitChatCommandWrapper ( )
80
+ {
81
+ Def = toolkitChatCommand ,
82
+ DefName = toolkitChatCommand . defName ,
83
+ CommandText = toolkitChatCommand . commandText ,
84
+ Enabled = toolkitChatCommand . enabled ,
85
+ PermissionRole = toolkitChatCommand . permissionRole
86
+ } ) ;
87
+ }
88
+ else
89
+ {
90
+ Log . Message ( wrapper . CommandText ) ;
91
+ }
92
+ }
93
+ }
94
+
95
+ public static void SaveCommandMethodSettings ( )
96
+ {
97
+ DatabaseController . SaveObject (
98
+ new CommandMethodWrapper ( )
99
+ {
100
+ Commands = toolkitChatCommandWrappers
101
+ } ,
102
+ "CommandMethods" ,
103
+ LoadedModManager . GetMod < ToolkitCore > ( )
104
+ ) ; ;
105
+ }
106
+
107
+ public static void ResetCommandMethodToDefaultSettings ( )
108
+ {
109
+ toolkitChatCommandWrappers = new List < ToolkitChatCommandWrapper > ( ) ;
110
+ foreach ( ToolkitChatCommand def in DefDatabase < ToolkitChatCommand > . AllDefsListForReading )
111
+ {
112
+ toolkitChatCommandWrappers . Add ( new ToolkitChatCommandWrapper ( )
113
+ {
114
+ Def = def ,
115
+ DefName = def . defName ,
116
+ CommandText = def . commandText ,
117
+ Enabled = def . enabled ,
118
+ PermissionRole = def . permissionRole
119
+ } ) ;
120
+ }
121
+ }
122
+ }
123
+
124
+ [ Serializable ]
125
+ public class CommandMethodWrapper
126
+ {
127
+ public IEnumerable < ToolkitChatCommandWrapper > Commands { get ; set ; }
27
128
}
28
129
}
0 commit comments