Validate config data immediately after it is loaded

This commit is contained in:
Anton Kasyanov
2016-08-18 21:05:43 +03:00
parent 130e543c2d
commit 2d4a5e0a4b
3 changed files with 32 additions and 0 deletions

View File

@@ -157,5 +157,32 @@ namespace EveOPreview.Configuration
{
this.ClientHotkey[currentClient] = (new KeysConverter()).ConvertToInvariantString(hotkey);
}
/// <summary>
/// Applies restrictions to different parameters of the config
/// </summary>
public void ApplyRestrictions()
{
this.ThumbnailRefreshPeriod = AppConfig.ApplyRestrictions(this.ThumbnailRefreshPeriod, 300, 1000);
this.ThumbnailSize = new Size(AppConfig.ApplyRestrictions(this.ThumbnailSize.Width, this.ThumbnailMinimumSize.Width, this.ThumbnailMaximumSize.Width),
AppConfig.ApplyRestrictions(this.ThumbnailSize.Height, this.ThumbnailMinimumSize.Height, this.ThumbnailMaximumSize.Height));
this.ThumbnailOpacity = AppConfig.ApplyRestrictions((int)(this.ThumbnailOpacity * 100.00), 20, 100) / 100.00;
this.ThumbnailZoomFactor = AppConfig.ApplyRestrictions(this.ThumbnailZoomFactor, 2, 10);
}
private static int ApplyRestrictions(int value, int minimum, int maximum)
{
if (value <= minimum)
{
return minimum;
}
if (value >= maximum)
{
return maximum;
}
return value;
}
}
}

View File

@@ -24,6 +24,9 @@ namespace EveOPreview.Configuration
string rawData = File.ReadAllText(ConfigurationStorage.ConfigurationFileName);
JsonConvert.PopulateObject(rawData, this._configuration);
// Validate data after loading it
this._configuration.ApplyRestrictions();
}
public void Save()

View File

@@ -38,5 +38,7 @@ namespace EveOPreview.Configuration
Keys GetClientHotkey(string currentClient);
void SetClientHotkey(string currentClient, Keys hotkey);
void ApplyRestrictions();
}
}