This commit is contained in:
Jelle Luteijn 2022-05-15 11:19:49 +02:00
parent 16e76d6b31
commit 484dbfc9d9
529 changed files with 113694 additions and 0 deletions

View file

@ -0,0 +1,93 @@
using System;
using System.Configuration;
using AspClassic.Scripting.Utils;
namespace AspClassic.Scripting.Hosting.Configuration;
public class LanguageElement : ConfigurationElement
{
private const string _Names = "names";
private const string _Extensions = "extensions";
private const string _Type = "type";
private const string _DisplayName = "displayName";
private static ConfigurationPropertyCollection _Properties = new ConfigurationPropertyCollection
{
new ConfigurationProperty("names", typeof(string), null),
new ConfigurationProperty("extensions", typeof(string), null),
new ConfigurationProperty("type", typeof(string), null, ConfigurationPropertyOptions.IsRequired),
new ConfigurationProperty("displayName", typeof(string), null)
};
protected override ConfigurationPropertyCollection Properties => _Properties;
public string Names
{
get
{
return (string)base["names"];
}
set
{
base["names"] = value;
}
}
public string Extensions
{
get
{
return (string)base["extensions"];
}
set
{
base["extensions"] = value;
}
}
public string Type
{
get
{
return (string)base["type"];
}
set
{
base["type"] = value;
}
}
public string DisplayName
{
get
{
return (string)base["displayName"];
}
set
{
base["displayName"] = value;
}
}
public string[] GetNamesArray()
{
return Split(Names);
}
public string[] GetExtensionsArray()
{
return Split(Extensions);
}
private static string[] Split(string str)
{
if (str == null)
{
return ArrayUtils.EmptyStrings;
}
return str.Split(new char[2] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries);
}
}

View file

@ -0,0 +1,22 @@
using System.Configuration;
namespace AspClassic.Scripting.Hosting.Configuration;
public class LanguageElementCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.BasicMap;
protected override bool ThrowOnDuplicate => false;
protected override string ElementName => "language";
protected override ConfigurationElement CreateNewElement()
{
return new LanguageElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((LanguageElement)element).Type;
}
}

View file

@ -0,0 +1,105 @@
using System;
using System.Configuration;
using AspClassic.Scripting.Runtime;
namespace AspClassic.Scripting.Hosting.Configuration;
public class OptionElement : ConfigurationElement
{
internal sealed class Key : IEquatable<Key>
{
private readonly string _option;
private readonly string _language;
public string Option => _option;
public string Language => _language;
public Key(string option, string language)
{
_option = option;
_language = language;
}
public override bool Equals(object obj)
{
return Equals(obj as Key);
}
public bool Equals(Key other)
{
if (other != null && DlrConfiguration.OptionNameComparer.Equals(_option, other._option))
{
return DlrConfiguration.LanguageNameComparer.Equals(_language, other._language);
}
return false;
}
public override int GetHashCode()
{
return _option.GetHashCode() ^ (_language ?? string.Empty).GetHashCode();
}
public override string ToString()
{
return (string.IsNullOrEmpty(_language) ? string.Empty : (_language + ":")) + _option;
}
}
private const string _Option = "option";
private const string _Value = "value";
private const string _Language = "language";
private static ConfigurationPropertyCollection _Properties = new ConfigurationPropertyCollection
{
new ConfigurationProperty("option", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsKey),
new ConfigurationProperty("value", typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired),
new ConfigurationProperty("language", typeof(string), string.Empty, ConfigurationPropertyOptions.IsKey)
};
protected override ConfigurationPropertyCollection Properties => _Properties;
public string Name
{
get
{
return (string)base["option"];
}
set
{
base["option"] = value;
}
}
public string Value
{
get
{
return (string)base["value"];
}
set
{
base["value"] = value;
}
}
public string Language
{
get
{
return (string)base["language"];
}
set
{
base["language"] = value;
}
}
internal object GetKey()
{
return new Key(Name, Language);
}
}

View file

@ -0,0 +1,25 @@
using System.Configuration;
namespace AspClassic.Scripting.Hosting.Configuration;
public class OptionElementCollection : ConfigurationElementCollection
{
public override ConfigurationElementCollectionType CollectionType => ConfigurationElementCollectionType.AddRemoveClearMap;
protected override bool ThrowOnDuplicate => false;
public OptionElementCollection()
{
base.AddElementName = "set";
}
protected override ConfigurationElement CreateNewElement()
{
return new OptionElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((OptionElement)element).GetKey();
}
}

View file

@ -0,0 +1,166 @@
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}'");
}
}
}