Terraria ModLoader  0.11.4
A framework for Terraria mods
ModConfig.cs
Go to the documentation of this file.
1 using Newtonsoft.Json;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Linq;
6 using Terraria.ModLoader.Config.UI;
7 
8 namespace Terraria.ModLoader.Config
9 {
10  // TODO: Enforce no statics allowed.
11 
18  public abstract class ModConfig
19  {
20  [JsonIgnore]
21  public Mod mod { get; internal set; }
22 
23  [JsonIgnore]
24  public string Name { get; internal set; }
25 
26  [JsonIgnore]
27  public abstract ConfigScope Mode { get; }
28 
29  // TODO: Does non-autoloaded ModConfigs have a use-case?
30  public virtual bool Autoload(ref string name) => mod.Properties.Autoload;
31 
36  public virtual void OnLoaded()
37  {
38  }
39 
43  public virtual void OnChanged()
44  {
45  }
46 
54  public virtual bool AcceptClientChanges(ModConfig pendingConfig, int whoAmI, ref string message)
55  {
56  return true;
57  }
58 
59  // TODO: Can we get rid of Clone and just load from disk? Don't think so yet.
64  public virtual ModConfig Clone() => (ModConfig)MemberwiseClone();
65 
71  public virtual bool NeedsReload(ModConfig pendingConfig)
72  {
73  foreach (PropertyFieldWrapper variable in ConfigManager.GetFieldsAndProperties(this))
74  {
75  ReloadRequiredAttribute reloadRequired = ConfigManager.GetCustomAttribute<ReloadRequiredAttribute>(variable, this, null);
76  if (reloadRequired != null)
77  {
78  // Do we need to implement nested ReloadRequired? Right now only top level fields will trigger it.
79  if (!ConfigManager.ObjectEquals(variable.GetValue(this), variable.GetValue(pendingConfig))) {
80  return true;
81  }
82  }
83  }
84  return false;
85  }
86  }
87 
91  public enum ConfigScope
92  {
96  ServerSide,
100  ClientSide,
101  // PlayerSpecific,
102  // WorldSpecific
103  }
104 }
virtual bool NeedsReload(ModConfig pendingConfig)
Whether or not a reload is required. The default implementation compares properties and fields annota...
Definition: ModConfig.cs:71
static bool ObjectEquals(object a, object b)
This attribute hints that changing the value of the annotated property or field will put the config i...
virtual void OnLoaded()
This method is called when the ModConfig has been loaded for the first time. This happens before regu...
Definition: ModConfig.cs:36
virtual bool AcceptClientChanges(ModConfig pendingConfig, int whoAmI, ref string message)
Called on the Server for ServerSide configs to determine if the changes asked for by the Client will ...
Definition: ModConfig.cs:54
This config is specific to the client. Use this for personalization options.
ModConfig provides a way for mods to be configurable. ModConfigs can either be Client specific or Ser...
Definition: ModConfig.cs:18
ModProperties Properties
Definition: Mod.cs:51
This config is shared between all clients and maintained by the server. Use this for game-play change...
static IEnumerable< PropertyFieldWrapper > GetFieldsAndProperties(object item)
bool Autoload
Whether or not this mod will autoload content by default. Autoloading content means you do not need t...
virtual void OnChanged()
This hook is called anytime new config values have been set and are ready to take effect...
Definition: ModConfig.cs:43
Mod is an abstract class that you will override. It serves as a central place from which the mod&#39;s co...
Definition: Mod.cs:23
ConfigScope
Each ModConfig class has a different scope. Failure to use the correct mode will lead to bugs...
Definition: ModConfig.cs:91