|
| 1 | +using System; |
| 2 | +using System.CodeDom.Compiler; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.IO; |
| 5 | +using System.Reflection; |
| 6 | +using System.Linq; |
| 7 | + |
| 8 | +namespace PluginTests |
| 9 | +{ |
| 10 | + class Program |
| 11 | + { |
| 12 | + static void Main(string[] args) |
| 13 | + { |
| 14 | + var mc = new MainClass(); |
| 15 | + mc.LoadPlugin(new FileInfo("PluginA.cs")); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public class MainClass |
| 20 | + { |
| 21 | + public void Feature1() { } |
| 22 | + public void Feature2(int value) { int x = value + 10; var x2 = x.ToString().Split('1'); } |
| 23 | + public int Feature3() { return 42; } |
| 24 | + public MainClass Feature4() { return this; } |
| 25 | + |
| 26 | + private static readonly FileInfo ts3File = new FileInfo(typeof(IPlugin).Assembly.Location); |
| 27 | + public AppDomain domain; |
| 28 | + |
| 29 | + public PluginResponse LoadPlugin(FileInfo file) |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + if (file.Extension != ".cs" && file.Extension != ".dll" && file.Extension != ".exe") |
| 34 | + return PluginResponse.UnsupportedFile; |
| 35 | + |
| 36 | + domain = AppDomain.CreateDomain( |
| 37 | + "Plugin_" + file.Name, |
| 38 | + AppDomain.CurrentDomain.Evidence, |
| 39 | + new AppDomainSetup |
| 40 | + { |
| 41 | + ShadowCopyFiles = "true", |
| 42 | + ShadowCopyDirectories = ts3File.Directory.FullName, |
| 43 | + ApplicationBase = ts3File.Directory.FullName, |
| 44 | + PrivateBinPath = "Plugin/..;Plugin", |
| 45 | + PrivateBinPathProbe = "" |
| 46 | + }); |
| 47 | + domain.UnhandledException += (s, e) => { Console.WriteLine("Plugin unex: {0}", e.ExceptionObject); }; |
| 48 | + |
| 49 | + //domain.Load() |
| 50 | + |
| 51 | + Assembly result; |
| 52 | + if (file.Extension == ".cs") |
| 53 | + result = PrepareSource(file); |
| 54 | + else if (file.Extension == ".dll" || file.Extension == ".exe") |
| 55 | + result = PrepareBinary(file); |
| 56 | + else throw new InvalidProgramException(); |
| 57 | + |
| 58 | + return PluginResponse.Ok; |
| 59 | + } |
| 60 | + catch (Exception ex) |
| 61 | + { |
| 62 | + Console.WriteLine("Possible plugin failed to load: ", ex); |
| 63 | + return PluginResponse.Crash; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static CompilerParameters GenerateCompilerParameter() |
| 68 | + { |
| 69 | + var cp = new CompilerParameters(); |
| 70 | + Assembly[] aarr = AppDomain.CurrentDomain.GetAssemblies(); |
| 71 | + for (int i = 0; i < aarr.Length; i++) |
| 72 | + { |
| 73 | + if (aarr[i].IsDynamic) continue; |
| 74 | + cp.ReferencedAssemblies.Add(aarr[i].Location); |
| 75 | + } |
| 76 | + cp.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location); |
| 77 | + |
| 78 | + // set preferences |
| 79 | + cp.WarningLevel = 3; |
| 80 | + cp.CompilerOptions = "/target:library /optimize"; |
| 81 | + cp.GenerateExecutable = false; |
| 82 | + cp.GenerateInMemory = true; |
| 83 | + return cp; |
| 84 | + } |
| 85 | + |
| 86 | + private Assembly PrepareSource(FileInfo file) |
| 87 | + { |
| 88 | + var provider = CodeDomProvider.CreateProvider("CSharp"); |
| 89 | + var cp = GenerateCompilerParameter(); |
| 90 | + var result = provider.CompileAssemblyFromFile(cp, file.FullName); |
| 91 | + |
| 92 | + if (result.Errors.Count > 0) |
| 93 | + { |
| 94 | + bool containsErrors = false; |
| 95 | + foreach (CompilerError error in result.Errors) |
| 96 | + { |
| 97 | + containsErrors |= !error.IsWarning; |
| 98 | + Console.WriteLine("Plugin_{0}: {1} L{2}/C{3}: {4}\n", |
| 99 | + file.Name, |
| 100 | + error.IsWarning ? "Warning" : "Error", |
| 101 | + error.Line, |
| 102 | + error.Column, |
| 103 | + error.ErrorText); |
| 104 | + } |
| 105 | + |
| 106 | + if (containsErrors) |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + return result.CompiledAssembly; |
| 111 | + } |
| 112 | + |
| 113 | + private Assembly PrepareBinary(FileInfo file) |
| 114 | + { |
| 115 | + return null; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public interface IPlugin |
| 120 | + { |
| 121 | + void Initialize(MainClass mc); |
| 122 | + } |
| 123 | + |
| 124 | + public enum PluginResponse |
| 125 | + { |
| 126 | + Ok, |
| 127 | + UnsupportedFile, |
| 128 | + Crash, |
| 129 | + NoTypeMatch, |
| 130 | + TooManyPlugins, |
| 131 | + UnknownStatus, |
| 132 | + PluginNotFound, |
| 133 | + CompileError, |
| 134 | + } |
| 135 | +} |
0 commit comments