Skip to content
Permalink
Browse files

Merge pull request #3839 from ArchonMegalon/master

ChummerUrl is now registered in every Installation.
  • Loading branch information...
ArchonMegalon committed Aug 26, 2019
2 parents 5342372 + 886c6ca commit 6d48e9072e8ad45a3fcc20e4ab217e4e2cd58ab6
@@ -21,14 +21,18 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.Win32;
using NLog;

namespace Chummer.Plugins
@@ -83,10 +87,96 @@ public PluginControl()
}
}

//the level-argument is only to absolutely make sure to not spawn processes uncontrolled
public static bool RegisterChummerProtocol()
{
var startupExe = System.Windows.Forms.Application.StartupPath;
startupExe = System.Reflection.Assembly.GetEntryAssembly()?.Location;
RegistryKey key = Registry.ClassesRoot.OpenSubKey("Chummer"); //open myApp protocol's subkey
bool reregisterKey = false;
if (key != null)
{
if (key.GetValue(string.Empty)?.ToString() != "URL: Chummer Protocol")
reregisterKey = true;
if (key.GetValue("URL Protocol")?.ToString() != string.Empty)
reregisterKey = true;
key = key.OpenSubKey(@"shell\open\command");
if (key == null)
reregisterKey = true;
else
{
if (key.GetValue(string.Empty)?.ToString() != startupExe + " " + "%1")
reregisterKey = true;
}
key.Close();
}
else
{
reregisterKey = true;
}

if (reregisterKey == false)
{
Log.Info("Url Protocol Handler for Chummer was already registered!");
return true;
}

try
{
System.AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
return RegisterMyProtocol(startupExe);
}
catch (System.Security.SecurityException se)
{
Log.Warn(se);
}
return true;
}


public static bool RegisterMyProtocol(string myAppPath) //myAppPath = full path to your application
{
RegistryKey Software = Registry.CurrentUser.OpenSubKey("Software"); //open myApp protocol's subkey
RegistryKey Classes = Software.OpenSubKey("Classes", true);
if (Classes == null)
{
try
{
Classes = Software.CreateSubKey("Classes", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
catch (System.UnauthorizedAccessException e)
{
Log.Error(e);
return false;
}
}
RegistryKey key = Classes.OpenSubKey("Chummer", true); //open myApp protocol's subkey

if (key == null) //if the protocol is not registered yet...we register it
{
key = Classes.CreateSubKey("Chummer", RegistryKeyPermissionCheck.ReadWriteSubTree);
}
key.SetValue(string.Empty, "URL: Chummer Protocol");
key.SetValue("URL Protocol", string.Empty);

RegistryKey shell = key.OpenSubKey(@"shell\open\command", RegistryKeyPermissionCheck.ReadWriteSubTree);
if (shell == null)
shell = key.CreateSubKey(@"shell\open\command", RegistryKeyPermissionCheck.ReadWriteSubTree);
shell.SetValue(string.Empty, myAppPath + " " + "%1");
//%1 represents the argument - this tells windows to open this program with an argument / parameter
shell.Close();
key.Close();
Classes.Close();
Software.Close();
Log.Info("Url Protocol Handler for Chummer registered!");
return true;
}

public void Initialize()
{
try
{
RegisterChummerProtocol();
if (GlobalOptions.PluginsEnabled == false)
{
Log.Info("Plugins are globally disabled - exiting PluginControl.Initialize()");
@@ -106,9 +196,40 @@ public void Initialize()
var plugindirectories = Directory.GetDirectories(path);
foreach (var plugindir in plugindirectories)
{
myDirectoryCatalog = new DirectoryCatalog(path: plugindir, searchPattern: "*.dll");
Log.Info("Searching for dlls in path " + myDirectoryCatalog?.FullPath);
catalog.Catalogs.Add(myDirectoryCatalog);
//search for a textfile, that tells me what dll to parse
string infofile = Path.Combine(plugindir, "plugin.txt");
if (File.Exists(infofile))
{
System.IO.StreamReader file =
new System.IO.StreamReader(infofile);
string line;
while ((line = file.ReadLine()) != null)
{
string plugindll = Path.Combine(plugindir, line);
if (File.Exists(plugindll))
{
FileInfo fi = new FileInfo(plugindll);
myDirectoryCatalog = new DirectoryCatalog(path: plugindir, searchPattern: fi.Name);
Log.Info("Searching for plugin in " + plugindll + ".");
catalog.Catalogs.Add(myDirectoryCatalog);
}
else
{
Log.Warn("Could not find dll from " + infofile + ": " + plugindll); myDirectoryCatalog = new DirectoryCatalog(path: plugindir, searchPattern: "*.dll");
myDirectoryCatalog = new DirectoryCatalog(path: plugindir, searchPattern: "*.dll");
Log.Info("Searching for dlls in path " + myDirectoryCatalog?.FullPath);
catalog.Catalogs.Add(myDirectoryCatalog);
}
}
file.Close();
}
else
{
myDirectoryCatalog = new DirectoryCatalog(path: plugindir, searchPattern: "*.dll");
Log.Info("Searching for dlls in path " + myDirectoryCatalog?.FullPath);
catalog.Catalogs.Add(myDirectoryCatalog);
}

}

container = new CompositionContainer(catalog);
@@ -168,12 +289,10 @@ public IEnumerable<IPlugin> MyActivePlugins
var list = MyPlugins.ToList();
foreach(var plugin in list)
{
bool enabled = false;
if (GlobalOptions.PluginsEnabledDic.TryGetValue(plugin.ToString(), out enabled))
{
if (enabled)
result.Add(plugin);
}
bool enabled = true;
GlobalOptions.PluginsEnabledDic.TryGetValue(plugin.ToString(), out enabled);
if (enabled)
result.Add(plugin);
}
return result;
}

0 comments on commit 6d48e90

Please sign in to comment.
You can’t perform that action at this time.