166 lines
4.3 KiB
C#
166 lines
4.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using AspClassic.Scripting.Runtime;
|
|
using AspClassic.Scripting.Utils;
|
|
|
|
namespace AspClassic.Scripting.Hosting.Configuration;
|
|
|
|
public class Section : ConfigurationSection
|
|
{
|
|
private const string _DebugMode = "debugMode";
|
|
|
|
private const string _PrivateBinding = "privateBinding";
|
|
|
|
private const string _Languages = "languages";
|
|
|
|
private const string _Options = "options";
|
|
|
|
public static readonly string SectionName = "AspClassic.Scripting";
|
|
|
|
private static ConfigurationPropertyCollection _Properties = new ConfigurationPropertyCollection
|
|
{
|
|
new ConfigurationProperty("debugMode", typeof(bool?), null),
|
|
new ConfigurationProperty("privateBinding", typeof(bool?), null),
|
|
new ConfigurationProperty("languages", typeof(LanguageElementCollection), null, ConfigurationPropertyOptions.IsDefaultCollection),
|
|
new ConfigurationProperty("options", typeof(OptionElementCollection), null)
|
|
};
|
|
|
|
protected override ConfigurationPropertyCollection Properties => _Properties;
|
|
|
|
public bool? DebugMode
|
|
{
|
|
get
|
|
{
|
|
return (bool?)base["debugMode"];
|
|
}
|
|
set
|
|
{
|
|
base["debugMode"] = value;
|
|
}
|
|
}
|
|
|
|
public bool? PrivateBinding
|
|
{
|
|
get
|
|
{
|
|
return (bool?)base["privateBinding"];
|
|
}
|
|
set
|
|
{
|
|
base["privateBinding"] = value;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<LanguageElement> GetLanguages()
|
|
{
|
|
if (!(base["languages"] is LanguageElementCollection languages))
|
|
{
|
|
yield break;
|
|
}
|
|
foreach (object languageConfig in languages)
|
|
{
|
|
yield return (LanguageElement)languageConfig;
|
|
}
|
|
}
|
|
|
|
public IEnumerable<OptionElement> GetOptions()
|
|
{
|
|
if (!(base["options"] is OptionElementCollection options))
|
|
{
|
|
yield break;
|
|
}
|
|
foreach (object option in options)
|
|
{
|
|
yield return (OptionElement)option;
|
|
}
|
|
}
|
|
|
|
private static Section LoadFromFile(Stream configFileStream)
|
|
{
|
|
Section section = new Section();
|
|
using XmlReader xmlReader = XmlReader.Create(configFileStream);
|
|
if (xmlReader.ReadToDescendant("configuration") && xmlReader.ReadToDescendant(SectionName))
|
|
{
|
|
section.DeserializeElement(xmlReader, serializeCollectionKey: false);
|
|
return section;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
internal static void LoadRuntimeSetup(ScriptRuntimeSetup setup, Stream configFileStream)
|
|
{
|
|
Section section = ((configFileStream == null) ? (ConfigurationManager.GetSection(SectionName) as Section) : LoadFromFile(configFileStream));
|
|
if (section == null)
|
|
{
|
|
return;
|
|
}
|
|
if (section.DebugMode.HasValue)
|
|
{
|
|
setup.DebugMode = section.DebugMode.Value;
|
|
}
|
|
if (section.PrivateBinding.HasValue)
|
|
{
|
|
setup.PrivateBinding = section.PrivateBinding.Value;
|
|
}
|
|
foreach (LanguageElement language in section.GetLanguages())
|
|
{
|
|
string type = language.Type;
|
|
string[] namesArray = language.GetNamesArray();
|
|
string[] extensionsArray = language.GetExtensionsArray();
|
|
string displayName = language.DisplayName ?? ((namesArray.Length > 0) ? namesArray[0] : language.Type);
|
|
bool flag = false;
|
|
foreach (LanguageSetup languageSetup in setup.LanguageSetups)
|
|
{
|
|
if (languageSetup.TypeName == type)
|
|
{
|
|
languageSetup.Names.Clear();
|
|
string[] array = namesArray;
|
|
foreach (string item in array)
|
|
{
|
|
languageSetup.Names.Add(item);
|
|
}
|
|
languageSetup.FileExtensions.Clear();
|
|
string[] array2 = extensionsArray;
|
|
foreach (string item2 in array2)
|
|
{
|
|
languageSetup.FileExtensions.Add(item2);
|
|
}
|
|
languageSetup.DisplayName = displayName;
|
|
flag = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!flag)
|
|
{
|
|
setup.LanguageSetups.Add(new LanguageSetup(type, displayName, namesArray, extensionsArray));
|
|
}
|
|
}
|
|
OptionElement option;
|
|
foreach (OptionElement option2 in section.GetOptions())
|
|
{
|
|
option = option2;
|
|
if (string.IsNullOrEmpty(option.Language))
|
|
{
|
|
setup.Options[option.Name] = option.Value;
|
|
continue;
|
|
}
|
|
bool flag2 = false;
|
|
foreach (LanguageSetup languageSetup2 in setup.LanguageSetups)
|
|
{
|
|
if (Utils.CollectionExtensions.Any(languageSetup2.Names,(string s) => DlrConfiguration.LanguageNameComparer.Equals(s, option.Language)))
|
|
{
|
|
languageSetup2.Options[option.Name] = option.Value;
|
|
flag2 = true;
|
|
break;
|
|
}
|
|
}
|
|
if (flag2)
|
|
{
|
|
continue;
|
|
}
|
|
throw new ConfigurationErrorsException($"Unknown language name: '{option.Language}'");
|
|
}
|
|
}
|
|
}
|