From 6b28ae44d6128c0925ce9c098182505532b14f6e Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Sat, 24 Oct 2020 19:49:30 -0400 Subject: [PATCH] feat: Added program entry point to path utils --- ICD.Common.Utils/AssemblyUtils.cs | 40 +++++++++++++++++++ .../ICD.Common.Utils_SimplSharp.csproj | 1 + ICD.Common.Utils/PathUtils.cs | 19 +++++++++ 3 files changed, 60 insertions(+) create mode 100644 ICD.Common.Utils/AssemblyUtils.cs diff --git a/ICD.Common.Utils/AssemblyUtils.cs b/ICD.Common.Utils/AssemblyUtils.cs new file mode 100644 index 0000000..6f770bf --- /dev/null +++ b/ICD.Common.Utils/AssemblyUtils.cs @@ -0,0 +1,40 @@ +using System.Linq; +using System.Text; +using ICD.Common.Properties; +using ICD.Common.Utils.IO; +#if SIMPLSHARP +using Crestron.SimplSharp; +using Crestron.SimplSharp.CrestronXmlLinq; +using Crestron.SimplSharp.Reflection; +#else +using System.Reflection; +#endif + +namespace ICD.Common.Utils +{ + public static class AssemblyUtils + { + /// + /// Gets the process executable in the default application domain. In other application domains, + /// this is the first executable that was executed by ExecuteAssembly(String). + /// + /// + [CanBeNull] + public static Assembly GetEntryAssembly() + { +#if SIMPLSHARP + string appDir = InitialParametersClass.ProgramDirectory.ToString(); + + if (CrestronEnvironment.RuntimeEnvironment == eRuntimeEnvironment.SIMPL) + return null; + + string proginfo = IcdFile.ReadToEnd(IcdPath.Combine(appDir, "ProgramInfo.config"), Encoding.UTF8); + XDocument doc = XDocument.Parse("\r\n" + proginfo); + XElement entry = doc.Descendants("EntryPoint").FirstOrDefault(); + return entry == null ? null : Assembly.Load(entry.Value); +#else + return Assembly.GetEntryAssembly(); +#endif + } + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index 1fb14f3..a224f89 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -75,6 +75,7 @@ + diff --git a/ICD.Common.Utils/PathUtils.cs b/ICD.Common.Utils/PathUtils.cs index 19d2d38..bce622e 100644 --- a/ICD.Common.Utils/PathUtils.cs +++ b/ICD.Common.Utils/PathUtils.cs @@ -1,6 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; +#if SIMPLSHARP +using Crestron.SimplSharp.Reflection; +#else +using System.Reflection; +#endif using ICD.Common.Properties; using ICD.Common.Utils.Extensions; using ICD.Common.Utils.IO; @@ -34,6 +39,20 @@ namespace ICD.Common.Utils [PublicAPI] public static string ProgramPath { get { return IcdDirectory.GetApplicationDirectory(); } } + /// + /// Gets the path to the program entry point assembly (i.e. ICD.Connect.Core_SimplSharp.dll) + /// + [PublicAPI] + [CanBeNull] + public static string ProgramFilePath + { + get + { + Assembly entry = AssemblyUtils.GetEntryAssembly(); + return entry == null ? null : entry.GetPath(); + } + } + /// /// Gets the path to the root config directory, /// which contains common and program-specific config directories.