Skip to content

feat: add loading of toml support#804

Merged
roflmuffin merged 4 commits into
mainfrom
feature/toml-config-manager
Mar 25, 2025
Merged

feat: add loading of toml support#804
roflmuffin merged 4 commits into
mainfrom
feature/toml-config-manager

Conversation

@roflmuffin
Copy link
Copy Markdown
Owner

@roflmuffin roflmuffin commented Mar 17, 2025

Adds support for basic TOML config parsing for plugin configs.

  • If a PluginName.toml file is found, it will read that using Tomlyn.
  • If a PluginName.example.toml file is found, it will copy the example to PluginName.toml and then read it.
  • Otherwise, fall back to existing JSON behaviour (read .json, copy example, same as above).
  • If no existing example or real config file are found, it will default to saving a .json file with the defaults to maintain similar behaviour as before.
  • There is currently no way to provide TOML options to the config serializer/deserializer, so it will use our hard coded defaults which preserves names for property names.

Closes #773

@Mesharsky
Copy link
Copy Markdown

Mesharsky commented Mar 17, 2025

@roflmuffin

I would add support for Preserve PascalCase

var tomlContent = File.ReadAllText(configPath);

var tomlModelOptions = new TomlModelOptions
{
    ConvertPropertyName = name => name // Preserve PascalCase (disable auto conversion)
};

Config = Toml.ToModel<Config>(tomlContent, options: tomlModelOptions);

Other than that, finally <3

@roflmuffin
Copy link
Copy Markdown
Owner Author

@Mesharsky, I think this is good to go and I've tested it locally and everything appears to work correctly. Would you like to pull down the artifacts and test for yourself before merge or all good?

@roflmuffin roflmuffin merged commit c50213c into main Mar 25, 2025
@roflmuffin roflmuffin deleted the feature/toml-config-manager branch March 25, 2025 09:12
@Mesharsky
Copy link
Copy Markdown

@Mesharsky, I think this is good to go and I've tested it locally and everything appears to work correctly. Would you like to pull down the artifacts and test for yourself before merge or all good?

Apologies for not responding, did not see a message.
Will test everything right now and see.
But after reviewing the code, everything should be working properly. If there will be any problems, i give you a ping

@Mesharsky
Copy link
Copy Markdown

@roflmuffin Oh, i thought it's working, is my implementation wrong?
Inside:

/game/csgo/addons/counterstrikesharp/configs/plugins/AWPManager

I have AWPManager.toml
Despite that it's still generating JSON file, not reading values from TOML file, so i guess fallback mechanism is in place

Config class is as follows

using CounterStrikeSharp.API.Core;

namespace AWPManager
{
    public class AWPManagerConfig : BasePluginConfig
    {
        public DatabaseConfig Database { get; set; } = new();
        public CoreConfig Core { get; set; } = new();
        public AnimationConfig Animation { get; set; } = new();
        public Dictionary<string, GameModeConfig> GameModes { get; set; } = new();
        public StatisticsConfig Statistics { get; set; } = new();
        public WeaponConfig Weapons { get; set; } = new();
    }

Plugin.cs just registers it globally as:

public AWPManagerConfig Config { get; set; } = new();

Debugs i have is basically this:

[AWPManager] Configuration loaded and validated successfully.
[AWPManager] ERROR: Database connection test failed - Unable to connect to any of the specified MySQL hosts.
[AWPManager] ERROR: Could not establish database connection
[AWPManager] Registered game mode: Standard AWP (standard)
[AWPManager] Registered game mode: No-Scope Challenge (noscope)
[AWPManager] Registered 2 game modes
[AWPManager] StatisticsManager initialized

And it creates .json file every time, called: AWPManager.json which is wrong?

@roflmuffin
Copy link
Copy Markdown
Owner Author

That location looks right, it shouldn't try to create a json if the toml exists, are you definitely on 313+?

@Mesharsky
Copy link
Copy Markdown

@roflmuffin You are right, for some reason the package did not automatically update for me, what a headache, my bad.
Works as it should

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support TOML as a First-Class Configuration Format

2 participants