4
\$\begingroup\$

The reason I used fields instead of automatic properties is because I am using tryparse which can not use properties... I know the Title setting could have been an automatic property but I decided to go with consistency and create a field for it. Is it ok to do so?

Do you see anything else that could go wrong?

Is it ok to format string the way I did in this file?

I do plan on adding a lot more settings in this file.

internal class Configurator
{
    private Game1 _game;

    // The error message to display if the config file can not load settings properly.
    private string ErrorMessage = ("Config file corrupted, {0} could not be loaded properly. {0} will be set to default");

    private readonly XmlDocument _document = new XmlDocument();
    private XmlNode _node;

    private int _windowWidth;
    private int _windowHeight;
    private string _title;
    private bool _allowAltF4;
    private bool _isMouseVisible;

    // The default values will be loaded if the config file can not be loaded.
    public Configurator(Game1 game)
    {
        _game = game;
        _windowWidth = 960;
        _windowHeight = 540;
        _title = "Title";
        _allowAltF4 = true;
        _isMouseVisible = true;
    }

    public int WindowWidth
    {
        get => _windowWidth;
        set => _windowWidth = value;
    }

    public int WindowHeight
    {
        get => _windowHeight;
        set => _windowHeight = value;
    }

    public string Title
    {
        get => _title;
        set => _title = value;
    }

    public bool AllowAltF4
    {
        get => _allowAltF4;
        set => _allowAltF4 = value;
    }

    public bool IsMouseVisible
    {
        get => _isMouseVisible;
        set => _isMouseVisible = value;
    }

    public void LoadSettings(string configFilePath)
    {
        _document.Load(configFilePath);

        {
            // Window width
            _node = _document.SelectSingleNode("Settings/Window/Dimensions/Width");
            if (_node == null || !int.TryParse(_node?.InnerText, out _windowWidth))
                Logger.Log(string.Format(ErrorMessage, "WindowWidth"), LoggerType.Error);


            // Window height
            _node = _document.SelectSingleNode("Settings/Window/Dimensions/Height");
            if (_node == null || !int.TryParse(_node?.InnerText, out _windowHeight))
                Logger.Log(ErrorMessage, LoggerType.Error);

            // Title
            _node = _document.SelectSingleNode("Settings/Window/Title");
            if (_node != null)
                _title = _document.SelectSingleNode("Settings/Window/Title")?.InnerText;
            else Logger.Log(ErrorMessage, LoggerType.Error);


            // Allow Alt F4
            _node = _document.SelectSingleNode("Settings/Window/AllowAltF4");
            if (_node == null || !bool.TryParse(_node?.InnerText, out _allowAltF4))
                Logger.Log(ErrorMessage, LoggerType.Error);

            // Is mouse visible
            _node = _document.SelectSingleNode("Settings/Mouse/IsVisible");
            if (_node == null || !bool.TryParse(_node?.InnerText, out _isMouseVisible))
                Logger.Log(ErrorMessage, LoggerType.Error);
        }
    }

    public void ApplySettings()
    {
        _game.GraphicsDeviceManager.PreferredBackBufferWidth = _windowWidth;
        _game.GraphicsDeviceManager.PreferredBackBufferHeight = _windowHeight;
        _game.Window.Title = _title;
        _game.Window.AllowAltF4 = _allowAltF4;
        _game.IsMouseVisible = _isMouseVisible;
    }
}

}

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.