HostFactory
using Autodesk.DesignScript.Geometry.Properties;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Runtime;
using Dynamo.Events;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
namespace Autodesk.DesignScript.Geometry
{
[SupressImportIntoVM]
public class HostFactory
{
private static readonly HostFactory mSelf;
private readonly IGeometryFactory mGeometryFactory;
private readonly IPersistenceManager mPersistenceManager;
private readonly List<IExtensionApplication> mExtensionApplications = new List<IExtensionApplication>();
private readonly List<Type> mExtensionApplicationTypes = new List<Type>();
private string mErrorString = string.Empty;
private double scaleFactor = 1;
public static HostFactory Instance => mSelf;
internal double ScaleFactor {
get {
return scaleFactor;
}
private set {
scaleFactor = value;
}
}
internal static bool ExtensionApplicationStarted { get; set; }
public static IGeometryFactory Factory {
get {
if (Instance.mGeometryFactory == null) {
if (string.IsNullOrEmpty(Instance.mErrorString))
Instance.mErrorString = Resources.NoImplementationException;
throw new NotImplementedException(Instance.mErrorString);
}
return Instance.mGeometryFactory;
}
}
public static IPersistenceManager PersistenceManager => Instance.mPersistenceManager;
internal static List<IExtensionApplication> ExtensionApplications => Instance.mExtensionApplications;
static HostFactory()
{
mSelf = new HostFactory();
}
private unsafe HostFactory()
{
try {
WorkspaceEvents.add_WorkspaceSettingsChanged(new WorkspaceSettingsChangedEventHandler((object)this, (IntPtr)(void*)));
string factoryFilePath = null;
string persistentMgrFilePath = null;
bool flag = GetFactoryFileFromSessionConfig(ref factoryFilePath, ref persistentMgrFilePath);
if (!flag)
flag = GetFactoryFileFromGeometrySettings(ref factoryFilePath, ref persistentMgrFilePath);
if (!flag)
flag = GetFactoryFileFromExeConfig(ref factoryFilePath, ref persistentMgrFilePath);
if (!flag)
flag = GetFactoryFileFromGeometryConfig(ref factoryFilePath, ref persistentMgrFilePath);
if (flag) {
if (!string.IsNullOrEmpty(factoryFilePath))
factoryFilePath = GeometryExtension.LocateFile(factoryFilePath);
if (!string.IsNullOrEmpty(persistentMgrFilePath))
persistentMgrFilePath = GeometryExtension.LocateFile(persistentMgrFilePath);
}
if (mGeometryFactory == null && !string.IsNullOrEmpty(factoryFilePath))
mGeometryFactory = (CreateInstance(typeof(IGeometryFactory), factoryFilePath) as IGeometryFactory);
if (mPersistenceManager == null && !string.IsNullOrEmpty(persistentMgrFilePath))
mPersistenceManager = (CreateInstance(typeof(IPersistenceManager), persistentMgrFilePath) as IPersistenceManager);
if (mPersistenceManager != null && mGeometryFactory != null)
mPersistenceManager.set_GeometryFactory(mGeometryFactory);
if (!ExtensionApplicationStarted && mExtensionApplications.Count > 0) {
foreach (IExtensionApplication mExtensionApplication in mExtensionApplications) {
mExtensionApplication.OnBeginExecution(Application.Instance.Session);
}
ExtensionApplicationStarted = true;
}
} catch (Exception ex) {
Exception innerException = ex.InnerException;
mErrorString = ex.Message;
}
}
public void StartUp(double scaleFactor = 1)
{
this.scaleFactor = scaleFactor;
foreach (IExtensionApplication mExtensionApplication in mExtensionApplications) {
mExtensionApplication.StartUp();
}
}
public void ShutDown()
{
foreach (IExtensionApplication mExtensionApplication in mExtensionApplications) {
mExtensionApplication.ShutDown();
}
}
public void PreloadAsmLibraries(string baseDirectory)
{
foreach (IExtensionApplication mExtensionApplication in mExtensionApplications) {
mExtensionApplication.PreloadAsmLibraries(baseDirectory);
}
}
private void WorkspaceSettingsChanged(WorkspacesSettingsChangedEventArgs args)
{
ScaleFactor = args.get_ScaleFactor();
}
private static bool GetFactoryFileFromGeometryConfig(ref string factoryFilePath, ref string persistentMgrFilePath)
{
IProtoGeometryConfiguration settings = ProtoGeometryConfigurationManager.Settings;
if (settings != null) {
factoryFilePath = settings.GeometryFactoryFileName;
persistentMgrFilePath = settings.PersistentManagerFileName;
}
return !string.IsNullOrEmpty(factoryFilePath);
}
private static bool GetFactoryFileFromExeConfig(ref string factoryFilePath, ref string persistentMgrFilePath)
{
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (configuration == null)
return false;
KeyValueConfigurationElement keyValueConfigurationElement = configuration.AppSettings.Settings[ConfigurationKeys.GeometryFactory];
if (keyValueConfigurationElement != null)
factoryFilePath = keyValueConfigurationElement.Value;
KeyValueConfigurationElement keyValueConfigurationElement2 = configuration.AppSettings.Settings[ConfigurationKeys.PersistentManager];
if (keyValueConfigurationElement2 != null)
persistentMgrFilePath = keyValueConfigurationElement2.Value;
return !string.IsNullOrEmpty(factoryFilePath);
}
private static bool GetFactoryFileFromSessionConfig(ref string factoryFilePath, ref string persistentMgrFilePath)
{
IExecutionSession session = Application.Instance.Session;
if (session == null)
return false;
IConfiguration val = session.get_Configuration();
if (val == null)
return false;
factoryFilePath = (val.GetConfigValue(ConfigurationKeys.GeometryFactory) as string);
persistentMgrFilePath = (val.GetConfigValue(ConfigurationKeys.PersistentManager) as string);
return !string.IsNullOrEmpty(factoryFilePath);
}
private static bool GetFactoryFileFromGeometrySettings(ref string factoryFilePath, ref string persistentMgrFilePath)
{
if (!string.IsNullOrEmpty(GeometrySettings.GeometryFactoryFilePath)) {
factoryFilePath = GeometrySettings.GeometryFactoryFilePath;
if (!string.IsNullOrEmpty(GeometrySettings.PersistenceManagerFilePath))
persistentMgrFilePath = GeometrySettings.PersistenceManagerFilePath;
return true;
}
return false;
}
private object CreateInstance(Type type, string library)
{
object obj = null;
Type type2 = null;
Type typeFromHandle = typeof(IExtensionApplication);
Type[] exportedTypes = Assembly.LoadFrom(library).GetExportedTypes();
foreach (Type type3 in exportedTypes) {
if (!type3.IsAbstract) {
if (type.IsAssignableFrom(type3))
obj = Activator.CreateInstance(type3);
if (typeFromHandle.IsAssignableFrom(type3))
type2 = type3;
if ((Type)null != type2 && obj != null)
break;
}
}
if (obj != null && (Type)null != type2 && !mExtensionApplicationTypes.Contains(type2)) {
IExtensionApplication val = Activator.CreateInstance(type2) as IExtensionApplication;
if (val != null) {
mExtensionApplicationTypes.Add(type2);
mExtensionApplications.Add(val);
}
}
return obj;
}
}
}