Added utility function that simplifies configuration loading of all addins.

sedebug
Diva Canto 2015-01-03 21:53:18 -08:00
parent b379eaa169
commit 53c8318124
1 changed files with 57 additions and 0 deletions

View File

@ -1191,6 +1191,63 @@ namespace OpenSim.Framework
return settingsClass;
}
/// <summary>
/// Reads a configuration file, configFile, merging it with the main configuration, config.
/// If the file doesn't exist, it copies a given exampleConfigFile onto configFile, and then
/// merges it.
/// </summary>
/// <param name="config">The main configuration data</param>
/// <param name="configFileName">The name of a configuration file in ConfigDirectory variable, no path</param>
/// <param name="exampleConfigFile">Full path to an example configuration file</param>
/// <param name="configFilePath">Full path ConfigDirectory/configFileName</param>
/// <param name="created">True if the file was created in ConfigDirectory, false if it existed</param>
/// <returns>True if success</returns>
public static bool MergeConfigurationFile(IConfigSource config, string configFileName, string exampleConfigFile, out string configFilePath, out bool created)
{
created = false;
configFilePath = string.Empty;
IConfig cnf = config.Configs["Startup"];
if (cnf == null)
{
m_log.WarnFormat("[UTILS]: Startup section doesn't exist");
return false;
}
string configDirectory = cnf.GetString("ConfigDirectory", ".");
string configFile = Path.Combine(configDirectory, configFileName);
if (!File.Exists(configFile) && !string.IsNullOrEmpty(exampleConfigFile))
{
// We need to copy the example onto it
if (!Directory.Exists(configDirectory))
Directory.CreateDirectory(configDirectory);
try
{
File.Copy(exampleConfigFile, configFile);
created = true;
}
catch (Exception e)
{
m_log.WarnFormat("[UTILS]: Exception copying configuration file {0} to {1}: {2}", configFile, exampleConfigFile, e.Message);
return false;
}
}
if (File.Exists(configFile))
{
// Merge
config.Merge(new IniConfigSource(configFile));
config.ExpandKeyValues();
configFilePath = configFile;
return true;
}
else
return false;
}
#endregion
public static float Clip(float x, float min, float max)