using System; using System.ComponentModel; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Xml; using Decal.Interop.Core; using Decal.Interop.Inject; using Decal.Interop.Render; namespace Decal.Adapter.Wrappers; [CLSCompliant(true)] public sealed class PluginHost : HostBase { private IPluginSite2 mySite; private IPluginSite myOldSite; [CLSCompliant(false)] [EditorBrowsable(EditorBrowsableState.Never)] public IPluginSite2 Underlying => mySite; /// /// Wrapped Decal OM /// public DecalWrapper Decal { get { if (base.MyDecal == null) { base.MyDecal = new DecalWrapper(mySite.Decal); } return base.MyDecal; } } /// /// Wrapped version of the ACHooks interface /// public HooksWrapper Actions => CoreManager.Current.Actions; /// /// Wrapped version of the DecalRenderService /// public RenderServiceWrapper Render { get { if (base.MyRender == null) { base.MyRender = new RenderServiceWrapper((RenderService)GetObject("services\\DecalRender.RenderService")); } return base.MyRender; } } internal PluginHost(PluginSite2 pSite) { mySite = pSite; base.MyDecal = new DecalWrapper(mySite.Decal); myOldSite = (IPluginSite)mySite.PluginSite; } protected override void Dispose(bool disposing) { try { _ = base.IsDisposed; if (myOldSite != null) { Marshal.ReleaseComObject(myOldSite); myOldSite = null; } if (mySite != null) { Marshal.ReleaseComObject(mySite); mySite = null; } } finally { base.Dispose(disposing); } } /// /// Function to get a COM object by path from Decal. /// /// The decal services path to resolve the COM object /// instance of the object requested public object GetObject(string path) { return mySite.get_Object(path); } /// /// Get Mapped Keyboard Key /// /// Name to retrive mapping for. /// Mapped Key public int GetKeyboardMapping(string name) { return myOldSite.QueryKeyboardMap(name); } /// /// Initializes an already exisint viewhandler (plugins) /// /// the handler to init internal static void LoadViewHandler(IViewHandler handler) { if (ViewWrapper.ScanViews(handler)) { ViewWrapper.ScanControls(handler); ViewWrapper.ScanReferences(handler); } } /// /// Load a view handler of the specified type /// /// type of handler to load /// the new handler public ViewHandler LoadViewHandler(Type handlerType) { ViewHandler obj = (ViewHandler)Activator.CreateInstance(handlerType, this); LoadViewHandler(obj); obj.LoadComplete(); return obj; } /// /// Load a view in the current assembly using the specified resource /// /// path of the embedded view xml resource /// the new view public ViewWrapper LoadViewResource(string resourcePath) { Assembly callingAssembly = Assembly.GetCallingAssembly(); return LoadViewResource(resourcePath, callingAssembly); } /// /// Load a view in the specified assembly using the specified resource /// /// path of the embedded view xml resource /// assembly containing the resource /// the new view public ViewWrapper LoadViewResource(string resourcePath, Assembly resourceAssembly) { if (null == resourceAssembly) { throw new ArgumentNullException("resourceAssembly"); } Stream manifestResourceStream = resourceAssembly.GetManifestResourceStream(resourcePath); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(manifestResourceStream); return LoadView(xmlDocument.OuterXml); } /// /// Load a view from the specified XML element /// /// XmlElement containing the view schema /// the new view public ViewWrapper LoadView(XmlElement viewSchema) { if (viewSchema == null) { throw new ArgumentNullException("viewSchema"); } return LoadView(viewSchema.OuterXml); } /// /// Load a view from the specified XML string /// /// string containing the view schema /// the new view public ViewWrapper LoadView(string viewSchema) { View view = myOldSite.LoadView(viewSchema); if (view != null) { return new ViewWrapper(view); } return null; } /// /// Get a COM based filter object. /// (Similar to GetObject, but requires only ProgID instead of full path) /// /// /// public object ComFilter(string progId) { return mySite.get_Object("services\\DecalNet.NetService\\" + progId); } }