I'm trying to add a Config to one of my plugins and had a bit of trouble finding the right stuff how to do it. That's why I looked in the LustyMap Plugin and tried out their approach and it worked. I still wanted to improve more & removed unnecessary methods while overriding the default ones.
I ended up with this code:
Everytime I try to load it i get following error: "(NullReferenceException: Object reference not set to an instance of an object". When I rename or comment out the "LoadConfig" line it loads fine. Why can't i override this function when it's virtual?Code:// Overwrite all three virtual methods for read/write configuration file protected override void SaveConfig() => Config.WriteObject(pluginConfig, true); protected override void LoadConfig() => pluginConfig = Config.ReadObject<PluginConfig>(); protected override void LoadDefaultConfig() // Creates default configuration file { var defaultConfig = new PluginConfig { ... }; Config.WriteObject(defaultConfig, true); }
C#: Override LoadConfig
Discussion in 'Rust Development' started by deusprox, Feb 15, 2017.
-
Wulf Community Admin
Sounds like your PluginConfig object is not setup properly or doesn't contain anything.
-
Okay I created a small example to better understand whats happening:
Code:using Oxide.Core.Plugins; using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Reflection; using Oxide.Game.Rust.Libraries.Covalence; using Oxide.Core.Libraries.Covalence; using System.Text; using Newtonsoft.Json.Linq;namespace Oxide.Plugins { [Info("Fail", "DeusProx", "0.1.1", ResourceId = 0000)] [Description("FailCase for LoadConfig")] public class Fail : RustPlugin { private PluginConfig pluginConfig; #region Hooks void Loaded() { Debug.Log("[Fail] Loading Config File"); //LoadConfig(); LoadConfig2(); } #endregion #region Configuration class PluginConfig { public General General { get; set; } } class General { public string ChatPrefix { get; set; } public bool UsePermission { get; set; } public string PermissionName { get; set; } } // Overwrite all three virtual methods for read/write configuration file //protected override void SaveConfig() => Config.WriteObject(pluginConfig, true); //protected override void LoadConfig() => pluginConfig = Config.ReadObject<PluginConfig>(); protected void LoadConfig2() => pluginConfig = Config.ReadObject<PluginConfig>(); protected override void LoadDefaultConfig() // Creates default configuration file { Debug.Log("[Fail] Creating Config File"); var defaultConfig = new PluginConfig { General = new General { ChatPrefix = "<color=cyan>[Fail]</color>", UsePermission = false, PermissionName = "share" } }; Debug.Log("[Fail] " + defaultConfig.General.ChatPrefix); Debug.Log("[Fail] " + defaultConfig.General.UsePermission); Debug.Log("[Fail] " + defaultConfig.General.PermissionName); Config.WriteObject(defaultConfig, true); } #endregion } }
Strange Thing A: When I comment in the "protected override void SaveConfig()"-line my config file will only contain "null".
Strange Thing B: When I comment in the "protected override void LoadConfig()"-line I will always get "NullReferenceException: Object reference not set to an instance of an object". Doesn't matter whether I use the SaveConfig-line or not.
So I thought i could overwrite them because the Definition given by VS2015 showed that they are virtual but it seems that this was a very very very bad idea and they somehow won't work when I override them. Would be nice to have this somewehere with a big warning if i'm guessing right here. -
Wulf Community Admin
When you override a method, you're basically just replacing the functionality that happens normally in the original method.
I'd suggest with a more simplistic config file method to start with though. Example:
Code:namespace Oxide.Plugins { [Info("ConfigExample", "Wulf/lukespragg", "0.1.0")] [Description("Just a basic config example")] class ConfigExample : RustPlugin { protected override void LoadDefaultConfig() { Config["Example String"] = "Hello world"; Config["Example Bool"] = true; } void Init() { Puts((string)Config["Example String"]); } } }