1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Xml . Serialization ;
5+ using System . Linq ;
6+ using System . Text ;
7+ using VRage . Game ;
8+ using avaness . PluginLoader . Data ;
9+
10+ namespace avaness . PluginLoader . Config
11+ {
12+ public class PluginConfig
13+ {
14+ private const string fileName = "config.xml" ;
15+
16+ private string filePath ;
17+ private PluginList list ;
18+
19+ [ XmlArray ]
20+ [ XmlArrayItem ( "Id" ) ]
21+ public string [ ] Plugins
22+ {
23+ get { return enabledPlugins . Keys . ToArray ( ) ; }
24+ set
25+ {
26+ enabledPlugins . Clear ( ) ;
27+ foreach ( string id in value )
28+ enabledPlugins [ id ] = null ;
29+ }
30+ }
31+ public IEnumerable < PluginData > EnabledPlugins => enabledPlugins . Values ;
32+ private readonly Dictionary < string , PluginData > enabledPlugins = new Dictionary < string , PluginData > ( ) ;
33+
34+ [ XmlArray ]
35+ [ XmlArrayItem ( "Id" ) ]
36+ public string [ ] LocalFolderPlugins
37+ {
38+ get { return pluginFolders . ToArray ( ) ; }
39+ set
40+ {
41+ pluginFolders . Clear ( ) ;
42+ foreach ( string folder in value )
43+ pluginFolders . Add ( folder ) ;
44+ }
45+ }
46+ private readonly HashSet < string > pluginFolders = new HashSet < string > ( ) ;
47+
48+ [ XmlArray ]
49+ [ XmlArrayItem ( "Profile" ) ]
50+ public Profile [ ] Profiles
51+ {
52+ get { return ProfileMap . Values . ToArray ( ) ; }
53+ set
54+ {
55+ ProfileMap . Clear ( ) ;
56+ foreach ( var profile in value . Where ( x => x ? . Key != null ) )
57+ ProfileMap [ profile . Key ] = profile ;
58+ }
59+ }
60+
61+ [ XmlIgnore ]
62+ public readonly Dictionary < string , Profile > ProfileMap = new ( ) ;
63+
64+ [ XmlArray ]
65+ [ XmlArrayItem ( "Config" ) ]
66+ public PluginDataConfig [ ] PluginSettings
67+ {
68+ get { return pluginSettings . Values . ToArray ( ) ; }
69+ set
70+ {
71+ pluginSettings . Clear ( ) ;
72+ foreach ( PluginDataConfig config in value . Where ( x => x ? . Id != null ) )
73+ pluginSettings [ config . Id ] = config ;
74+ }
75+ }
76+ private readonly Dictionary < string , PluginDataConfig > pluginSettings = new Dictionary < string , PluginDataConfig > ( ) ;
77+
78+ public string ListHash { get ; set ; }
79+
80+ public int GameVersion { get ; set ; }
81+ [ XmlIgnore ]
82+ public bool GameVersionChanged { get ; private set ; }
83+
84+ // Base URL for the statistics server, change to http://localhost:5000 in config.xml for local development
85+ // ReSharper disable once UnassignedGetOnlyAutoProperty
86+ public string StatsServerBaseUrl { get ; }
87+
88+ // User consent to use the StatsServer
89+ public bool DataHandlingConsent { get ; set ; }
90+ public string DataHandlingConsentDate { get ; set ; }
91+
92+ private int networkTimeout = 5000 ;
93+ public int NetworkTimeout
94+ {
95+ get
96+ {
97+ return networkTimeout ;
98+ }
99+ set
100+ {
101+ if ( value < 100 )
102+ networkTimeout = 100 ;
103+ else if ( value > 60000 )
104+ networkTimeout = 60000 ;
105+ else
106+ networkTimeout = value ;
107+ }
108+ }
109+
110+ public int Count => enabledPlugins . Count ;
111+
112+ public bool AllowIPv6 { get ; set ; } = true ;
113+
114+ public PluginConfig ( )
115+ {
116+ }
117+
118+ public void Init ( PluginList plugins )
119+ {
120+ list = plugins ;
121+
122+ bool save = false ;
123+ StringBuilder sb = new StringBuilder ( "Enabled plugins: " ) ;
124+
125+ foreach ( PluginData plugin in plugins )
126+ {
127+ string id = plugin . Id ;
128+ bool enabled = IsEnabled ( id ) ;
129+
130+ if ( enabled )
131+ {
132+ sb . Append ( id ) . Append ( ", " ) ;
133+ enabledPlugins [ id ] = plugin ;
134+ }
135+
136+ if ( LoadPluginData ( plugin ) )
137+ save = true ;
138+ }
139+
140+ if ( enabledPlugins . Count > 0 )
141+ sb . Length -= 2 ;
142+ else
143+ sb . Append ( "None" ) ;
144+ LogFile . WriteLine ( sb . ToString ( ) ) ;
145+
146+ foreach ( KeyValuePair < string , PluginData > kv in enabledPlugins . Where ( x => x . Value == null ) . ToArray ( ) )
147+ {
148+ LogFile . WriteLine ( $ "{ kv . Key } was in the config but is no longer available") ;
149+ enabledPlugins . Remove ( kv . Key ) ;
150+ save = true ;
151+ }
152+
153+ foreach ( string id in pluginSettings . Keys . Where ( x => ! plugins . Contains ( x ) ) . ToArray ( ) )
154+ {
155+ LogFile . WriteLine ( $ "{ id } had settings in the config but is no longer available") ;
156+ pluginSettings . Remove ( id ) ;
157+ save = true ;
158+ }
159+
160+ if ( save )
161+ Save ( ) ;
162+ }
163+
164+ public void CheckGameVersion ( )
165+ {
166+ int currentGameVersion = MyFinalBuildConstants . APP_VERSION ? . Version ?? 0 ;
167+ int storedGameVersion = GameVersion ;
168+ if ( currentGameVersion != 0 )
169+ {
170+ if ( storedGameVersion == 0 )
171+ {
172+ GameVersion = currentGameVersion ;
173+ Save ( ) ;
174+ }
175+ else if ( storedGameVersion != currentGameVersion )
176+ {
177+ GameVersion = currentGameVersion ;
178+ GameVersionChanged = true ;
179+ Save ( ) ;
180+ }
181+ }
182+ }
183+
184+ public void Disable ( )
185+ {
186+ enabledPlugins . Clear ( ) ;
187+ }
188+
189+
190+ public void Save ( )
191+ {
192+ try
193+ {
194+ LogFile . WriteLine ( "Saving config" ) ;
195+ XmlSerializer serializer = new XmlSerializer ( typeof ( PluginConfig ) ) ;
196+ if ( File . Exists ( filePath ) )
197+ File . Delete ( filePath ) ;
198+ FileStream fs = File . OpenWrite ( filePath ) ;
199+ serializer . Serialize ( fs , this ) ;
200+ fs . Flush ( ) ;
201+ fs . Close ( ) ;
202+ }
203+ catch ( Exception e )
204+ {
205+ LogFile . WriteLine ( $ "An error occurred while saving plugin config: " + e ) ;
206+ }
207+ }
208+
209+ public static PluginConfig Load ( string mainDirectory )
210+ {
211+ string path = Path . Combine ( mainDirectory , fileName ) ;
212+ if ( File . Exists ( path ) )
213+ {
214+ try
215+ {
216+ XmlSerializer serializer = new XmlSerializer ( typeof ( PluginConfig ) ) ;
217+ PluginConfig config ;
218+ using ( FileStream fs = File . OpenRead ( path ) )
219+ config = ( PluginConfig ) serializer . Deserialize ( fs ) ;
220+ config . filePath = path ;
221+ return config ;
222+ }
223+ catch ( Exception e )
224+ {
225+ LogFile . WriteLine ( $ "An error occurred while loading plugin config: " + e ) ;
226+ }
227+ }
228+
229+ return new PluginConfig
230+ {
231+ filePath = path
232+ } ;
233+ }
234+
235+ public bool IsEnabled ( string id )
236+ {
237+ return enabledPlugins . ContainsKey ( id ) ;
238+ }
239+
240+ public void SetEnabled ( string id , bool enabled )
241+ {
242+ SetEnabled ( list [ id ] , enabled ) ;
243+ }
244+
245+ public void SetEnabled ( PluginData plugin , bool enabled )
246+ {
247+ string id = plugin . Id ;
248+ if ( IsEnabled ( id ) == enabled )
249+ return ;
250+
251+ if ( enabled )
252+ Enable ( plugin ) ;
253+ else
254+ Disable ( id ) ;
255+
256+ LoadPluginData ( plugin ) ; // Must be called because the enabled state has changed
257+ }
258+
259+ private void Enable ( PluginData plugin )
260+ {
261+ string id = plugin . Id ;
262+ enabledPlugins [ id ] = plugin ;
263+ list . SubscribeToItem ( id ) ;
264+ }
265+
266+ private void Disable ( string id )
267+ {
268+ enabledPlugins . Remove ( id ) ;
269+ }
270+
271+ /// <summary>
272+ /// Loads the stored user data into the plugin. Returns true if the config was modified.
273+ /// </summary>
274+ public bool LoadPluginData ( PluginData plugin )
275+ {
276+ PluginDataConfig settings ;
277+ if ( ! pluginSettings . TryGetValue ( plugin . Id , out settings ) )
278+ settings = null ;
279+ if ( plugin . LoadData ( ref settings , IsEnabled ( plugin . Id ) ) )
280+ {
281+ if ( settings == null )
282+ pluginSettings . Remove ( plugin . Id ) ;
283+ else
284+ pluginSettings [ plugin . Id ] = settings ;
285+ return true ;
286+ }
287+ return false ;
288+ }
289+
290+ /// <summary>
291+ /// Removes the stored user data for the plugin. Returns true if the config was modified.
292+ /// </summary>
293+ public bool RemovePluginData ( string id )
294+ {
295+ return pluginSettings . Remove ( id ) ;
296+ }
297+
298+ public void SavePluginData ( GitHubPluginConfig settings )
299+ {
300+ pluginSettings [ settings . Id ] = settings ;
301+ }
302+
303+ public void AddDevelopmentFolder ( string folder )
304+ {
305+ pluginFolders . Add ( folder ) ;
306+ }
307+
308+ public void RemoveDevelopmentFolder ( string folder )
309+ {
310+ pluginFolders . Remove ( folder ) ;
311+ }
312+ }
313+ }
0 commit comments