@@ -11,7 +11,7 @@ Copyright 2016 Zoey (Zoryn)
1111
1212namespace StardewModdingAPI
1313{
14- public partial class Config
14+ public class Config
1515 {
1616 [ JsonIgnore ]
1717 public virtual string ConfigLocation { get ; protected internal set ; }
@@ -47,7 +47,7 @@ public virtual T LoadConfig<T>() where T : Config
4747 if ( ! File . Exists ( ConfigLocation ) )
4848 {
4949 //no config exists, generate default values
50- var c = this . GenerateBaseConfig < T > ( ) ;
50+ var c = this . GenerateDefaultConfig < T > ( ) ;
5151 c . ConfigLocation = ConfigLocation ;
5252 ret = c ;
5353 }
@@ -67,8 +67,8 @@ public virtual T LoadConfig<T>() where T : Config
6767 }
6868 catch ( Exception ex )
6969 {
70- Log . Error ( "Invalid JSON Config : {0 } \n {1}" , ConfigLocation , ex ) ;
71- return GenerateBaseConfig < T > ( ) ;
70+ Log . Error ( "Invalid JSON ({0}) : {1 } \n {2}" , GetType ( ) . Name , ConfigLocation , ex ) ;
71+ return GenerateDefaultConfig < T > ( ) ;
7272 }
7373 }
7474
@@ -84,15 +84,6 @@ public virtual T GenerateDefaultConfig<T>() where T : Config
8484 return null ;
8585 }
8686
87- /// <summary>
88- /// Use the public GenerateDefaultConfig insteaad
89- /// </summary>
90- [ Obsolete ]
91- protected virtual T GenerateBaseConfig < T > ( ) where T : Config
92- {
93- return GenerateDefaultConfig < T > ( ) ;
94- }
95-
9687 /// <summary>
9788 /// Merges a default-value config with the user-config on disk.
9889 /// </summary>
@@ -103,7 +94,7 @@ public virtual T UpdateConfig<T>() where T : Config
10394 try
10495 {
10596 //default config
106- var b = JObject . FromObject ( Instance < T > ( ) . GenerateBaseConfig < T > ( ) ) ;
97+ var b = JObject . FromObject ( Instance < T > ( ) . GenerateDefaultConfig < T > ( ) ) ;
10798
10899 //user config
109100 var u = JObject . FromObject ( this ) ;
@@ -133,7 +124,12 @@ public static class ConfigExtensions
133124 /// Initializes an instance of any class that inherits from Config.
134125 /// This method performs the loading, saving, and merging of the config on the disk and in memory at a default state.
135126 /// 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!
136128 /// </summary>
129+ /// <typeparam name="T"></typeparam>
130+ /// <param name="baseConfig"></param>
131+ /// <param name="configLocation"></param>
132+ /// <returns></returns>
137133 public static T InitializeConfig < T > ( this T baseConfig , string configLocation ) where T : Config
138134 {
139135 if ( baseConfig == null )
@@ -184,122 +180,5 @@ public static T ReloadConfig<T>(this T baseConfig) where T : Config
184180 {
185181 return baseConfig . UpdateConfig < T > ( ) ;
186182 }
187-
188- [ Obsolete ]
189- public static void WriteConfig ( this Config baseConfig )
190- {
191- Log . Error ( "A config has been written through an obsolete way.\n \t This method of writing configs will not be supported in future versions." ) ;
192- WriteConfig < Config > ( baseConfig ) ;
193- }
194-
195- [ Obsolete ]
196- public static Config ReloadConfig ( this Config baseConfig )
197- {
198- Log . Error ( "A config has been reloaded through an obsolete way.\n \t This method of loading configs will not be supported in future versions." ) ;
199- return baseConfig . ReloadConfig < Config > ( ) ;
200- }
201- }
202-
203- public partial class Config
204- {
205- [ Obsolete ] public static int invalids = 0 ;
206-
207- [ JsonIgnore ]
208- [ Obsolete ]
209- public virtual JObject JObject { get ; protected set ; }
210-
211- [ Obsolete ]
212- public static Config InitializeConfig ( string configLocation , Config baseConfig )
213- {
214- invalids ++ ;
215-
216- if ( string . IsNullOrEmpty ( configLocation ) )
217- {
218- Log . Verbose ( "The location to save the config to must not be empty." ) ;
219- return null ;
220- }
221-
222- if ( baseConfig == null )
223- {
224- Log . Verbose ( "A config must be instantiated before being passed to Initialize.\n \t " + configLocation ) ;
225- return null ;
226- }
227-
228- baseConfig . ConfigLocation = configLocation ;
229- return baseConfig . LoadConfig ( baseConfig ) ;
230- }
231-
232- [ Obsolete ]
233- public virtual Config GenerateBaseConfig ( Config baseConfig )
234- {
235- //Must be implemented in sub-class
236- return null ;
237- }
238-
239- [ Obsolete ]
240- public virtual Config LoadConfig ( Config baseConfig )
241- {
242- if ( ! File . Exists ( baseConfig . ConfigLocation ) )
243- {
244- var v = ( Config ) baseConfig . GetType ( ) . GetMethod ( "GenerateBaseConfig" , BindingFlags . Public | BindingFlags . Instance ) . Invoke ( baseConfig , new object [ ] { baseConfig } ) ;
245- v . WriteConfig ( ) ;
246- }
247- else
248- {
249- var p = baseConfig . ConfigLocation ;
250-
251- try
252- {
253- var j = JObject . Parse ( File . ReadAllText ( baseConfig . ConfigLocation ) ) ;
254- baseConfig = ( Config ) j . ToObject ( baseConfig . GetType ( ) ) ;
255- baseConfig . ConfigLocation = p ;
256- baseConfig . JObject = j ;
257-
258- baseConfig = UpdateConfig ( baseConfig ) ;
259- baseConfig . ConfigLocation = p ;
260- baseConfig . JObject = j ;
261-
262- baseConfig . WriteConfig ( ) ;
263- }
264- catch
265- {
266- Log . Verbose ( "Invalid JSON: " + p ) ;
267- }
268- }
269-
270- return baseConfig ;
271- }
272-
273- [ Obsolete ]
274- public virtual Config UpdateConfig ( Config baseConfig )
275- {
276- try
277- {
278- //default config with all standard values
279- var b = JObject . FromObject ( baseConfig . GetType ( ) . GetMethod ( "GenerateBaseConfig" , BindingFlags . Public | BindingFlags . Instance ) . Invoke ( baseConfig , new object [ ] { baseConfig } ) ) ;
280- //user config with their values
281- var u = baseConfig . JObject ;
282-
283- b . Merge ( u , new JsonMergeSettings { MergeArrayHandling = MergeArrayHandling . Replace } ) ;
284-
285- return ( Config ) b . ToObject ( baseConfig . GetType ( ) ) ;
286- }
287- catch ( Exception ex )
288- {
289- Log . Error ( ex . ToString ( ) ) ;
290- }
291- return baseConfig ;
292- }
293-
294- /// <summary>
295- /// NOTICE: THIS IS OBSOLETE AND WILL BE REMOVED IN THE FUTURE. 'BaseConfigPath' IS NOW A PROPERTY IN A MOD
296- /// </summary>
297- /// <param name="theMod"></param>
298- /// <returns></returns>
299- [ Obsolete ]
300- public static string GetBasePath ( Mod theMod )
301- {
302- return theMod . BaseConfigPath ;
303- }
304183 }
305184}
0 commit comments