Attempts to address the issue of duplicate assembly .dlls being loaded via plugins

This commit is contained in:
Neil Dorin
2020-02-05 17:13:27 -07:00
parent af5699a0b0
commit d81bcfba9a
2 changed files with 62 additions and 22 deletions

View File

@@ -25,6 +25,8 @@ namespace PepperDash.Essentials
{ {
HttpLogoServer LogoServer; HttpLogoServer LogoServer;
List<string> LoadedAssemblies = new List<string>();
public ControlSystem() public ControlSystem()
: base() : base()
{ {
@@ -81,7 +83,7 @@ namespace PepperDash.Essentials
/// <summary> /// <summary>
/// Determines if the program is running on a processor (appliance) or server (VC-4). /// Determines if the program is running on a processor (appliance) or server (VC-4).
/// ///
/// Sets Global.FilePathPrefix based on platform /// Sets Global.FilePathPrefix and Global.ApplicationDirectoryPathPrefix based on platform
/// </summary> /// </summary>
public void DeterminePlatform() public void DeterminePlatform()
{ {
@@ -93,9 +95,8 @@ namespace PepperDash.Essentials
var dirSeparator = Global.DirectorySeparator; var dirSeparator = Global.DirectorySeparator;
string directoryPrefix; Global.SetApplicationDirectoryPathPrefix(Crestron.SimplSharp.CrestronIO.Directory.GetApplicationRootDirectory());
var directoryPrefix = Global.ApplicationDirectoryPathPrefix;
directoryPrefix = Crestron.SimplSharp.CrestronIO.Directory.GetApplicationRootDirectory();
var version = Crestron.SimplSharp.Reflection.Assembly.GetExecutingAssembly().GetName().Version; var version = Crestron.SimplSharp.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
@@ -106,7 +107,7 @@ namespace PepperDash.Essentials
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Starting Essentials v{0} on 3-series Appliance", Global.AssemblyVersion); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Starting Essentials v{0} on 3-series Appliance", Global.AssemblyVersion);
// Check if User/ProgramX exists // Check if User/ProgramX exists
if (Directory.Exists(directoryPrefix + dirSeparator + "User" if (Directory.Exists(Global.ApplicationDirectoryPathPrefix + dirSeparator + "User"
+ dirSeparator + string.Format("program{0}", InitialParametersClass.ApplicationNumber))) + dirSeparator + string.Format("program{0}", InitialParametersClass.ApplicationNumber)))
{ {
Debug.Console(0, @"User/program{0} directory found", InitialParametersClass.ApplicationNumber); Debug.Console(0, @"User/program{0} directory found", InitialParametersClass.ApplicationNumber);
@@ -156,6 +157,15 @@ namespace PepperDash.Essentials
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Starting Essentials load from configuration"); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Starting Essentials load from configuration");
// Get the loaded assembly filenames
var appDi = new DirectoryInfo(Global.ApplicationDirectoryPathPrefix);
var assemblyFiles = appDi.GetFiles("*.dll");
foreach (var file in assemblyFiles)
{
LoadedAssemblies.Add(file.Name);
}
var filesReady = SetupFilesystem(); var filesReady = SetupFilesystem();
if (filesReady) if (filesReady)
{ {
@@ -196,16 +206,13 @@ namespace PepperDash.Essentials
} }
/// <summary> /// <summary>
/// Initial simple implementation. Reads user/programXX/plugins folder and /// Loads plugins
/// use
/// </summary> /// </summary>
void LoadPlugins() void LoadPlugins()
{ {
var dir = Global.FilePathPrefix + "plugins"; var dir = Global.FilePathPrefix + "plugins";
if (Directory.Exists(dir)) if (Directory.Exists(dir))
{ {
// TODO Clear out or create localPlugins folder (maybe in program slot folder)
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Plugins directory found, checking for factory plugins"); Debug.Console(0, Debug.ErrorLogLevel.Notice, "Plugins directory found, checking for factory plugins");
var di = new DirectoryInfo(dir); var di = new DirectoryInfo(dir);
var zFiles = di.GetFiles("*.cplz"); var zFiles = di.GetFiles("*.cplz");
@@ -218,23 +225,27 @@ namespace PepperDash.Essentials
} }
var files = di.GetFiles("*.dll"); var files = di.GetFiles("*.dll");
Dictionary<string, Assembly> assyList = new Dictionary<string, Assembly>(); Dictionary<string, Assembly> assyList = new Dictionary<string, Assembly>();
foreach (FileInfo fi in files) foreach (FileInfo fi in files)
{ {
// TODO COPY plugin to loadedPlugins folder if (!CheckIfAssemblyExists(fi.Name))
// TODO LOAD that loadedPlugins dll file
try
{ {
var assy = Assembly.LoadFrom(fi.FullName); try
var ver = assy.GetName().Version; {
var verStr = string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision); var assy = Assembly.LoadFrom(fi.FullName);
assyList.Add(fi.FullName, assy); var ver = assy.GetName().Version;
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loaded plugin file '{0}', version {1}", fi.FullName, verStr); var verStr = string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);
} assyList.Add(fi.FullName, assy);
catch Debug.Console(0, Debug.ErrorLogLevel.Notice, "Loaded plugin file '{0}', version {1}", fi.FullName, verStr);
{ }
Debug.Console(2, "Assembly {0} is not a custom assembly", fi.FullName); catch
continue; //catching any load issues and continuing. There will be exceptions loading Crestron .dlls from the cplz Probably should do something different here {
Debug.Console(2, "Assembly {0} is not a custom assembly", fi.FullName);
continue; //catching any load issues and continuing. There will be exceptions loading Crestron .dlls from the cplz Probably should do something different here
}
} }
else
Debug.Console(0, Debug.ErrorLogLevel.Notice, "Skipping plugin: {0}. There is already an assembly with that name loaded.", fi.Name);
} }
foreach (var assy in assyList) foreach (var assy in assyList)
{ {
@@ -308,6 +319,21 @@ namespace PepperDash.Essentials
} }
} }
/// <summary>
/// Checks if the filename matches an already loaded assembly file's name
/// </summary>
/// <param name="filename"></param>
/// <returns>True if file already matches loaded assembly file.</returns>
bool CheckIfAssemblyExists(string filename)
{
var loadedAssembly = LoadedAssemblies.FirstOrDefault(s => s.Equals(filename));
if (loadedAssembly != null)
return true;
else
return false;
}
/// <summary> /// <summary>
/// Verifies filesystem is set up. IR, SGD, and programX folders /// Verifies filesystem is set up. IR, SGD, and programX folders
/// </summary> /// </summary>

View File

@@ -26,6 +26,11 @@ namespace PepperDash.Essentials.Core
/// </summary> /// </summary>
public static string FilePathPrefix { get; private set; } public static string FilePathPrefix { get; private set; }
/// <summary>
/// The file path prefix to the folder containing the running application files
/// </summary>
public static string ApplicationDirectoryPathPrefix { get; private set; }
/// <summary> /// <summary>
/// Returns the directory separator character based on the running OS /// Returns the directory separator character based on the running OS
/// </summary> /// </summary>
@@ -51,6 +56,15 @@ namespace PepperDash.Essentials.Core
FilePathPrefix = prefix; FilePathPrefix = prefix;
} }
/// <summary>
/// Sets the file path prefix
/// </summary>
/// <param name="prefix"></param>
public static void SetApplicationDirectoryPathPrefix(string prefix)
{
ApplicationDirectoryPathPrefix = prefix;
}
static string _AssemblyVersion; static string _AssemblyVersion;
/// <summary> /// <summary>