151 lines
3.7 KiB
C#
151 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using AspClassic.Scripting.Hosting.Configuration;
|
|
using AspClassic.Scripting.Runtime;
|
|
using AspClassic.Scripting.Utils;
|
|
|
|
namespace AspClassic.Scripting.Hosting;
|
|
|
|
[Serializable]
|
|
public sealed class ScriptRuntimeSetup
|
|
{
|
|
private Type _hostType;
|
|
|
|
private IList<object> _hostArguments;
|
|
|
|
private IList<LanguageSetup> _languageSetups;
|
|
|
|
private bool _debugMode;
|
|
|
|
private bool _privateBinding;
|
|
|
|
private IDictionary<string, object> _options;
|
|
|
|
private bool _frozen;
|
|
|
|
public IList<LanguageSetup> LanguageSetups => _languageSetups;
|
|
|
|
public bool DebugMode
|
|
{
|
|
get
|
|
{
|
|
return _debugMode;
|
|
}
|
|
set
|
|
{
|
|
CheckFrozen();
|
|
_debugMode = value;
|
|
}
|
|
}
|
|
|
|
public bool PrivateBinding
|
|
{
|
|
get
|
|
{
|
|
return _privateBinding;
|
|
}
|
|
set
|
|
{
|
|
CheckFrozen();
|
|
_privateBinding = value;
|
|
}
|
|
}
|
|
|
|
public Type HostType
|
|
{
|
|
get
|
|
{
|
|
return _hostType;
|
|
}
|
|
set
|
|
{
|
|
ContractUtils.RequiresNotNull(value, "value");
|
|
ContractUtils.Requires(typeof(ScriptHost).IsAssignableFrom(value), "value", "Must be ScriptHost or a derived type of ScriptHost");
|
|
CheckFrozen();
|
|
_hostType = value;
|
|
}
|
|
}
|
|
|
|
public IDictionary<string, object> Options => _options;
|
|
|
|
public IList<object> HostArguments
|
|
{
|
|
get
|
|
{
|
|
return _hostArguments;
|
|
}
|
|
set
|
|
{
|
|
ContractUtils.RequiresNotNull(value, "value");
|
|
CheckFrozen();
|
|
_hostArguments = value;
|
|
}
|
|
}
|
|
|
|
public ScriptRuntimeSetup()
|
|
{
|
|
_languageSetups = new List<LanguageSetup>();
|
|
_options = new Dictionary<string, object>();
|
|
_hostType = typeof(ScriptHost);
|
|
_hostArguments = ArrayUtils.EmptyObjects;
|
|
}
|
|
|
|
internal DlrConfiguration ToConfiguration()
|
|
{
|
|
ContractUtils.Requires(_languageSetups.Count > 0, "ScriptRuntimeSetup must have at least one LanguageSetup");
|
|
ReadOnlyCollection<LanguageSetup> readOnlyCollection = new ReadOnlyCollection<LanguageSetup>(ArrayUtils.MakeArray(_languageSetups));
|
|
ReadOnlyCollection<object> hostArguments = new ReadOnlyCollection<object>(ArrayUtils.MakeArray(_hostArguments));
|
|
AspClassic.Scripting.Utils.ReadOnlyDictionary<string, object> options = new AspClassic.Scripting.Utils.ReadOnlyDictionary<string, object>(new Dictionary<string, object>(_options));
|
|
DlrConfiguration dlrConfiguration = new DlrConfiguration(_debugMode, _privateBinding, options);
|
|
foreach (LanguageSetup item in readOnlyCollection)
|
|
{
|
|
dlrConfiguration.AddLanguage(item.TypeName, item.DisplayName, item.Names, item.FileExtensions, item.Options);
|
|
}
|
|
_languageSetups = readOnlyCollection;
|
|
_options = options;
|
|
_hostArguments = hostArguments;
|
|
Freeze(readOnlyCollection);
|
|
return dlrConfiguration;
|
|
}
|
|
|
|
private void Freeze(ReadOnlyCollection<LanguageSetup> setups)
|
|
{
|
|
foreach (LanguageSetup setup in setups)
|
|
{
|
|
setup.Freeze();
|
|
}
|
|
_frozen = true;
|
|
}
|
|
|
|
private void CheckFrozen()
|
|
{
|
|
if (_frozen)
|
|
{
|
|
throw new InvalidOperationException("Cannot modify ScriptRuntimeSetup after it has been used to create a ScriptRuntime");
|
|
}
|
|
}
|
|
|
|
public static ScriptRuntimeSetup ReadConfiguration()
|
|
{
|
|
ScriptRuntimeSetup scriptRuntimeSetup = new ScriptRuntimeSetup();
|
|
Section.LoadRuntimeSetup(scriptRuntimeSetup, null);
|
|
return scriptRuntimeSetup;
|
|
}
|
|
|
|
public static ScriptRuntimeSetup ReadConfiguration(Stream configFileStream)
|
|
{
|
|
ContractUtils.RequiresNotNull(configFileStream, "configFileStream");
|
|
ScriptRuntimeSetup scriptRuntimeSetup = new ScriptRuntimeSetup();
|
|
Section.LoadRuntimeSetup(scriptRuntimeSetup, configFileStream);
|
|
return scriptRuntimeSetup;
|
|
}
|
|
|
|
public static ScriptRuntimeSetup ReadConfiguration(string configFilePath)
|
|
{
|
|
ContractUtils.RequiresNotNull(configFilePath, "configFilePath");
|
|
using FileStream configFileStream = File.OpenRead(configFilePath);
|
|
return ReadConfiguration(configFileStream);
|
|
}
|
|
}
|