@@ -5,7 +5,6 @@ Copyright 2016 Zoey (Zoryn)
55using System ;
66using System . IO ;
77using System . Linq ;
8- using System . Reflection ;
98using Newtonsoft . Json ;
109using Newtonsoft . Json . Linq ;
1110
@@ -22,23 +21,15 @@ public class Config
2221 public virtual Config Instance < T > ( ) where T : Config => Activator . CreateInstance < T > ( ) ;
2322
2423 /// <summary>
25- /// Should never be used for anything.
26- /// </summary>
27- public Config ( )
28- {
29-
30- }
31-
32- /// <summary>
33- /// Loads the config from the json blob on disk, updating and re-writing to the disk if needed.
24+ /// Loads the config from the json blob on disk, updating and re-writing to the disk if needed.
3425 /// </summary>
3526 /// <typeparam name="T"></typeparam>
3627 /// <returns></returns>
3728 public virtual T LoadConfig < T > ( ) where T : Config
3829 {
3930 if ( string . IsNullOrEmpty ( ConfigLocation ) )
4031 {
41- Log . Error ( "A config tried to load without specifying a location on the disk." ) ;
32+ Log . AsyncR ( "A config tried to load without specifying a location on the disk." ) ;
4233 return null ;
4334 }
4435
@@ -47,7 +38,7 @@ public virtual T LoadConfig<T>() where T : Config
4738 if ( ! File . Exists ( ConfigLocation ) )
4839 {
4940 //no config exists, generate default values
50- var c = this . GenerateDefaultConfig < T > ( ) ;
41+ var c = GenerateDefaultConfig < T > ( ) ;
5142 c . ConfigLocation = ConfigLocation ;
5243 ret = c ;
5344 }
@@ -56,7 +47,7 @@ public virtual T LoadConfig<T>() where T : Config
5647 try
5748 {
5849 //try to load the config from a json blob on disk
59- T c = JsonConvert . DeserializeObject < T > ( File . ReadAllText ( ConfigLocation ) ) ;
50+ var c = JsonConvert . DeserializeObject < T > ( File . ReadAllText ( ConfigLocation ) , new JsonSerializerSettings { ContractResolver = new JsonResolver ( ) } ) ;
6051
6152 c . ConfigLocation = ConfigLocation ;
6253
@@ -67,7 +58,7 @@ public virtual T LoadConfig<T>() where T : Config
6758 }
6859 catch ( Exception ex )
6960 {
70- Log . Error ( "Invalid JSON ({0 }): {1 } \n {2}" , GetType ( ) . Name , ConfigLocation , ex ) ;
61+ Log . AsyncR ( $ "Invalid JSON ({ GetType ( ) . Name } ): { ConfigLocation } \n { ex } " ) ;
7162 return GenerateDefaultConfig < T > ( ) ;
7263 }
7364 }
@@ -77,15 +68,15 @@ public virtual T LoadConfig<T>() where T : Config
7768 }
7869
7970 /// <summary>
80- /// MUST be implemented in inheriting class!
71+ /// MUST be implemented in inheriting class!
8172 /// </summary>
8273 public virtual T GenerateDefaultConfig < T > ( ) where T : Config
8374 {
8475 return null ;
8576 }
8677
8778 /// <summary>
88- /// Merges a default-value config with the user-config on disk.
79+ /// Merges a default-value config with the user-config on disk.
8980 /// </summary>
9081 /// <typeparam name="T"></typeparam>
9182 /// <returns></returns>
@@ -94,16 +85,16 @@ public virtual T UpdateConfig<T>() where T : Config
9485 try
9586 {
9687 //default config
97- var b = JObject . FromObject ( Instance < T > ( ) . GenerateDefaultConfig < T > ( ) ) ;
88+ var b = JObject . FromObject ( Instance < T > ( ) . GenerateDefaultConfig < T > ( ) , new JsonSerializer { ContractResolver = new JsonResolver ( ) } ) ;
9889
9990 //user config
100- var u = JObject . FromObject ( this ) ;
91+ var u = JObject . FromObject ( this , new JsonSerializer { ContractResolver = new JsonResolver ( ) } ) ;
10192
10293 //overwrite default values with user values
10394 b . Merge ( u , new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling . Replace } ) ;
10495
10596 //cast json object to config
106- T c = b . ToObject < T > ( ) ;
97+ var c = b . ToObject < T > ( ) ;
10798
10899 //re-write the location on disk to the object
109100 c . ConfigLocation = ConfigLocation ;
@@ -112,7 +103,7 @@ public virtual T UpdateConfig<T>() where T : Config
112103 }
113104 catch ( Exception ex )
114105 {
115- Log . Error ( "An error occured when updating a config: " + ex ) ;
106+ Log . AsyncR ( "An error occured when updating a config: " + ex ) ;
116107 return this as T ;
117108 }
118109 }
@@ -121,10 +112,10 @@ public virtual T UpdateConfig<T>() where T : Config
121112 public static class ConfigExtensions
122113 {
123114 /// <summary>
124- /// Initializes an instance of any class that inherits from Config.
125- /// This method performs the loading, saving, and merging of the config on the disk and in memory at a default state.
126- /// This method should not be used to re-load or to re-save a config.
127- /// NOTE: You MUST set your config EQUAL to the return of this method!
115+ /// Initializes an instance of any class that inherits from Config.
116+ /// This method performs the loading, saving, and merging of the config on the disk and in memory at a default state.
117+ /// This method should not be used to re-load or to re-save a config.
118+ /// NOTE: You MUST set your config EQUAL to the return of this method!
128119 /// </summary>
129120 /// <typeparam name="T"></typeparam>
130121 /// <param name="baseConfig"></param>
@@ -136,35 +127,35 @@ public static T InitializeConfig<T>(this T baseConfig, string configLocation) wh
136127 {
137128 baseConfig = Activator . CreateInstance < T > ( ) ;
138129 /*
139- Log.Error ("A config tried to initialize whilst being null.");
130+ Log.AsyncR ("A config tried to initialize whilst being null.");
140131 return null;
141132 */
142133 }
143134
144135 if ( string . IsNullOrEmpty ( configLocation ) )
145136 {
146- Log . Error ( "A config tried to initialize without specifying a location on the disk." ) ;
137+ Log . AsyncR ( "A config tried to initialize without specifying a location on the disk." ) ;
147138 return null ;
148139 }
149140
150141 baseConfig . ConfigLocation = configLocation ;
151- T c = baseConfig . LoadConfig < T > ( ) ;
142+ var c = baseConfig . LoadConfig < T > ( ) ;
152143
153144 return c ;
154145 }
155146
156147 /// <summary>
157- /// Writes a config to a json blob on the disk specified in the config's properties.
148+ /// Writes a config to a json blob on the disk specified in the config's properties.
158149 /// </summary>
159150 public static void WriteConfig < T > ( this T baseConfig ) where T : Config
160151 {
161152 if ( string . IsNullOrEmpty ( baseConfig ? . ConfigLocation ) || string . IsNullOrEmpty ( baseConfig . ConfigDir ) )
162153 {
163- Log . Error ( "A config attempted to save when it itself or it's location were null." ) ;
154+ Log . AsyncR ( "A config attempted to save when it itself or it's location were null." ) ;
164155 return ;
165156 }
166157
167- string s = JsonConvert . SerializeObject ( baseConfig , typeof ( T ) , Formatting . Indented , new JsonSerializerSettings ( ) ) ;
158+ var s = JsonConvert . SerializeObject ( baseConfig , typeof ( T ) , Formatting . Indented , new JsonSerializerSettings { ContractResolver = new JsonResolver ( ) } ) ;
168159
169160 if ( ! Directory . Exists ( baseConfig . ConfigDir ) )
170161 Directory . CreateDirectory ( baseConfig . ConfigDir ) ;
@@ -174,7 +165,8 @@ public static void WriteConfig<T>(this T baseConfig) where T : Config
174165 }
175166
176167 /// <summary>
177- /// Re-reads the json blob on the disk and merges its values with a default config
168+ /// Re-reads the json blob on the disk and merges its values with a default config
169+ /// NOTE: You MUST set your config EQUAL to the return of this method!
178170 /// </summary>
179171 public static T ReloadConfig < T > ( this T baseConfig ) where T : Config
180172 {
0 commit comments