Hi everyone! This is my first time in creating a plugin for Rust, self teaching as I go, and it's been an ongoing process of learning with trial and error. I am in the final stages of development, and one of the issues I've been battling for a few days now is the structured layout of each of my [JsonProperty] not being followed as I defined.
I also have a "defaulted" layout defined under where I structured and indentified all my [JsonProperty], entirely filled with basic placeholder values so my Init() would not fail to call due to a value being null.
Upon freshly installing my plugin into my server, the configuration file gets generated with Oxide's default call "LoadDefaultConfig()", but the .json file is all mismatched and it seems to generated my [JsonProperty] in alphabetical order by name, and not the order I've structured it to be. I have defined each [JsonProperty] in an ordered layout I want, and my DefaultSettings() layout is exactly as it should look, with default values, given a fresh install and config generation.
I've tried numerous ways to get around this serialization mismatch, from using "Order = x", to even creating a custom method, "SaveConfig()" in order to force JsonSerialization to respect the order I have structured and defined, but have come up shorthanded, scratching my head and at the verge of defeat.
So I'm hoping you all, experienced plugin developers, can help me figure out why my configuration file is not generating in the order I want.
After some of my own research, I have however, been thinking that maybe Oxide's framework to serialize JsonProperties is overriding my defined layout, yet I am just so stuck on how to proceed.
See below for some snips of what my code looks like -
- I use a lot of // Comments to keep myself organized and understanding what I am doing :P
- No, my plugin is not called PluginAO, its a placeholder while I share with you my code snips
protected override void LoadDefaultConfig()
{
PrintWarning("Creating default config...");
_pluginConfig = GetDefaultSettings(); // Ensure _pluginConfig is set to the default settings
Config.WriteObject(_pluginConfig, true); // Write to config file
SaveConfig();
Puts("Default config created and saved.");
}
private void SaveConfig()
{
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented, // Ensure readable format with indentation
ContractResolver = new DefaultContractResolver() // Respects JsonProperty(Order)
};
// Manually serialize the _pluginConfig with the custom settings
var json = JsonConvert.SerializeObject(_pluginConfig, settings);
// Write the serialized JSON string to the config file
Interface.Oxide.DataFileSystem.WriteObject("PluginAO", _pluginConfig);
Config.Save();
}private class PluginConfig
{
[JsonProperty(Order = 1, PropertyName = "General Settings")]
public GeneralSettings General { get; set; }
[JsonProperty(Order = 2, PropertyName = "Code")]
public ACode Code { get; set; }