1+ using System . Reflection ;
2+ using System . Text . Json ;
3+ using CounterStrikeSharp . API ;
4+ using CounterStrikeSharp . API . Core ;
5+
6+ namespace CSSharpUtils . Extensions ;
7+
8+ /// <summary>
9+ /// Provides extension methods for <see cref="BasePluginConfig"/>.
10+ /// </summary>
11+ public static class ConfigExtensions
12+ {
13+ // Holds the name of the executing assembly, used in constructing the configuration file path.
14+ private static readonly string AssemblyName = Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ?? "" ;
15+
16+ // Defines the path to the configuration file, based on the game directory and assembly name.
17+ private static readonly string CfgPath =
18+ $ "{ Server . GameDirectory } /csgo/addons/counterstrikesharp/configs/plugins/{ AssemblyName } /{ AssemblyName } .json";
19+
20+ // Specifies the options for JSON serialization, including indentation for readability.
21+ private static readonly JsonSerializerOptions WriteSerializerOptions = new ( ) { WriteIndented = true } ;
22+
23+ // Specifies the options for JSON deserialization.
24+ private static readonly JsonSerializerOptions ReadSerializerOptions = new ( ) { ReadCommentHandling = JsonCommentHandling . Skip } ;
25+
26+
27+ /// <summary>
28+ /// Updates the version of the provided configuration object and serializes it back to JSON.
29+ /// This method ensures that the configuration file reflects the most recent version,
30+ /// including all properties of the configuration object, even those not initially set.
31+ /// </summary>
32+ /// <typeparam name="T">The type of the configuration object, must inherit from BasePluginConfig.</typeparam>
33+ /// <param name="config">The configuration object to update and serialize.</param>
34+ public static void Update < T > ( this T config ) where T : BasePluginConfig , new ( )
35+ {
36+ // get newest config version
37+ var newCfgVersion = new T ( ) . Version ;
38+
39+ // loaded config is up-to-date
40+ if ( config . Version == newCfgVersion )
41+ return ;
42+
43+ // update the version
44+ config . Version = newCfgVersion ;
45+
46+ // serialize the updated config back to json
47+ var updatedJsonContent = JsonSerializer . Serialize ( config , WriteSerializerOptions ) ;
48+ File . WriteAllText ( CfgPath , updatedJsonContent ) ;
49+ }
50+
51+ /// <summary>
52+ /// Reloads the configuration from disk, deserializing it back into the configuration object.
53+ /// </summary>
54+ /// <typeparam name="T">The type of the configuration object, must inherit from BasePluginConfig.</typeparam>
55+ /// <param name="config">The configuration object to reload.</param>
56+ /// <returns>The reloaded configuration object.</returns>
57+ /// <remarks>
58+ /// You should pass the result of this method to your plugins OnConfigParsed() method.
59+ /// </remarks>
60+ public static T Reload < T > ( this T config ) where T : BasePluginConfig , new ( )
61+ {
62+ // read the configuration file content
63+ var configContent = File . ReadAllText ( CfgPath ) ;
64+
65+ // deserialize the configuration content back to the object
66+ return JsonSerializer . Deserialize < T > ( configContent , ReadSerializerOptions ) ! ;
67+ }
68+ }
0 commit comments